-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Passwordless Signup backend (#1)
- Loading branch information
1 parent
bba3c9d
commit ff066c4
Showing
16 changed files
with
254 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { PasswordlessClient, type PasswordlessOptions } from '@passwordlessdev/passwordless-nodejs'; | ||
import { Accounts } from 'meteor/accounts-base'; | ||
|
||
import { settings } from '../../app/settings/server'; | ||
|
||
Accounts.registerLoginHandler('passwordless-dev', async (options: Record<string, any>) => { | ||
if (!options.passwordless || !settings.get<boolean>('Passwordless_dev')) { | ||
return; | ||
} | ||
|
||
const { token } = options; | ||
|
||
const apiUrl = settings.get<string>('Passwordless_Dev_Url'); | ||
|
||
if (!token || !apiUrl) { | ||
return; | ||
} | ||
|
||
const clientOptions: PasswordlessOptions = { | ||
baseUrl: settings.get<string>('Passwordless_Dev_Url'), | ||
}; | ||
const passwordlessClient = new PasswordlessClient(settings.get<string>('Passwordless_Dev_ApiSecret'), clientOptions); | ||
|
||
const verifiedUser = await passwordlessClient.verifyToken(token); | ||
|
||
if (verifiedUser) { | ||
console.log('Successfully verified sign-in for user.', verifiedUser); | ||
} else { | ||
console.warn('Sign in failed', verifiedUser); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { registerModel } from '@rocket.chat/models'; | ||
|
||
import { db } from '../database/utils'; | ||
import { PendingUsersRaw } from './raw/PendingUsers'; | ||
|
||
registerModel('IPendingUsersModel', new PendingUsersRaw(db)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { IPendingUser, RocketChatRecordDeleted } from '@rocket.chat/core-typings'; | ||
import type { IPendingUsersModel } from '@rocket.chat/model-typings'; | ||
import type { Db, Collection, IndexDescription } from 'mongodb'; | ||
|
||
import { BaseRaw } from './BaseRaw'; | ||
|
||
export class PendingUsersRaw extends BaseRaw<IPendingUser> implements IPendingUsersModel { | ||
constructor(db: Db, trash?: Collection<RocketChatRecordDeleted<IPendingUser>>) { | ||
super(db, 'pending_users', trash); | ||
} | ||
|
||
protected modelIndexes(): IndexDescription[] { | ||
return [ | ||
{ key: { credentialToken: 1 }, unique: false }, | ||
{ key: { email: 1 }, unique: false }, | ||
]; | ||
} | ||
|
||
public async findOneByToken(token: string): Promise<IPendingUser | null> { | ||
return this.findOne({ credentialToken: token }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { settingsRegistry } from '../../app/settings/server'; | ||
|
||
export const createPasswordlessSettings = () => | ||
settingsRegistry.addGroup('Passwordless_Dev', async function () { | ||
await this.add('Passwordless_Dev_Enable', false, { | ||
type: 'boolean', | ||
}); | ||
|
||
const enableQuery = { | ||
_id: 'Passwordless_Dev_Enable', | ||
value: true, | ||
}; | ||
|
||
await this.add('Passwordless_Dev_Url', 'https://v4.passwordless.dev', { | ||
type: 'string', | ||
enableQuery, | ||
}); | ||
|
||
await this.add('Passwordless_Dev_ApiKey', '', { | ||
type: 'string', | ||
enableQuery, | ||
}); | ||
|
||
await this.add('Passwordless_Dev_ApiSecret', '', { | ||
type: 'string', | ||
enableQuery, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { IRocketChatRecord } from './IRocketChatRecord'; | ||
|
||
export interface IPendingUser extends IRocketChatRecord { | ||
username: string; | ||
name: string; | ||
email: string; | ||
token: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { IPendingUser } from '@rocket.chat/core-typings'; | ||
import type { InsertOneResult } from 'mongodb'; | ||
|
||
import type { IBaseModel, InsertionModel } from './IBaseModel'; | ||
|
||
export interface IPendingUsersModel extends IBaseModel<IPendingUser> { | ||
insertOne(user: InsertionModel<Omit<IPendingUser, 'token'>>): Promise<InsertOneResult<IPendingUser>>; | ||
|
||
findOneByToken(token: string): Promise<IPendingUser | null>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/rest-typings/src/v1/users/UserRegisterPasswordlessDevParamsPOST.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Ajv from 'ajv'; | ||
|
||
const ajv = new Ajv({ | ||
coerceTypes: true, | ||
}); | ||
|
||
export type UserRegisterPasswordlessDevParamsPOST = { | ||
username: string; | ||
name: string; | ||
email: string; | ||
}; | ||
|
||
const UserRegisterPasswordlessDevParamsPostSchema = { | ||
type: 'object', | ||
properties: { | ||
username: { | ||
type: 'string', | ||
}, | ||
name: { | ||
type: 'string', | ||
}, | ||
email: { | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['username', 'name', 'email'], | ||
additionalProperties: false, | ||
}; | ||
|
||
export const isUserRegisterPasswordlessDevParamsPOST = ajv.compile<UserRegisterPasswordlessDevParamsPOST>( | ||
UserRegisterPasswordlessDevParamsPostSchema, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters