diff --git a/packages/api/src/routers/auth/mobile/mobile_auth_router.ts b/packages/api/src/routers/auth/mobile/mobile_auth_router.ts index eb85649dd..138aae9c8 100644 --- a/packages/api/src/routers/auth/mobile/mobile_auth_router.ts +++ b/packages/api/src/routers/auth/mobile/mobile_auth_router.ts @@ -6,7 +6,10 @@ import { createMobileSignInResponse, createMobileEmailSignInResponse, } from './sign_in' -import { createMobileSignUpResponse } from './sign_up' +import { + createMobileSignUpResponse, + createMobileEmailSignUpResponse, +} from './sign_up' import { createMobileAccountCreationResponse } from './account_creation' export function mobileAuthRouter() { @@ -24,6 +27,17 @@ export function mobileAuthRouter() { res.status(payload.statusCode).json(payload.json) }) + router.post('/email-sign-up', async (req, res) => { + const { email, password, username, name } = req.body + const payload = await createMobileEmailSignUpResponse( + email, + password, + username, + name + ) + res.status(payload.statusCode).json(payload.json) + }) + router.post('/sign-up', async (req, res) => { const { token, provider, name } = req.body const payload = await createMobileSignUpResponse(token, provider, name) diff --git a/packages/api/src/routers/auth/mobile/sign_up.ts b/packages/api/src/routers/auth/mobile/sign_up.ts index 6cda2bf3f..ef36f481c 100644 --- a/packages/api/src/routers/auth/mobile/sign_up.ts +++ b/packages/api/src/routers/auth/mobile/sign_up.ts @@ -9,6 +9,8 @@ import { } from '../auth_types' import { createPendingUserToken, suggestedUsername } from '../jwt_helpers' import UserModel from '../../../datalayer/user' +import { hashPassword } from '../../../utils/auth' +import { createUser } from '../../../services/create_user' export async function createMobileSignUpResponse( token?: string, @@ -41,6 +43,38 @@ export async function createMobileSignUpResponse( } } +export async function createMobileEmailSignUpResponse( + email?: string, + password?: string, + username?: string, + name?: string +): Promise { + try { + if (!email || !password || !username || !name) { + throw new Error('Missing username, password, name, or username') + } + + const hashedPassword = await hashPassword(password) + + await createUser({ + email, + provider: 'EMAIL', + sourceUserId: email, + name, + username: username.toLowerCase(), + password: hashedPassword, + pendingConfirmation: true, + }) + + return { + statusCode: 200, + json: {}, + } + } catch (e) { + return signUpFailedPayload + } +} + const signUpFailedPayload = { statusCode: 403, json: { errorCodes: ['AUTH_FAILED'] },