2022-11-16 00:32:16 -05:00
|
|
|
import { emailRegEx, passwordRegEx, usernameRegEx } from '@logto/core-kit';
|
2022-11-10 21:50:20 -05:00
|
|
|
import { userInfoSelectFields } from '@logto/schemas';
|
2022-11-11 01:29:37 -05:00
|
|
|
import { argon2Verify } from 'hash-wasm';
|
2022-11-10 21:50:20 -05:00
|
|
|
import pick from 'lodash.pick';
|
|
|
|
import type { Provider } from 'oidc-provider';
|
|
|
|
import { object, string } from 'zod';
|
|
|
|
|
|
|
|
import RequestError from '@/errors/RequestError';
|
|
|
|
import { checkSessionHealth } from '@/lib/session';
|
2022-11-11 01:29:37 -05:00
|
|
|
import { encryptUserPassword } from '@/lib/user';
|
2022-11-10 21:50:20 -05:00
|
|
|
import koaGuard from '@/middleware/koa-guard';
|
|
|
|
import { findUserById, updateUserById } from '@/queries/user';
|
|
|
|
import assertThat from '@/utils/assert-that';
|
|
|
|
|
|
|
|
import type { AnonymousRouter } from '../types';
|
|
|
|
import { verificationTimeout } from './consts';
|
|
|
|
import { checkSignUpIdentifierCollision } from './utils';
|
|
|
|
|
|
|
|
export const profileRoute = '/session/profile';
|
|
|
|
|
|
|
|
export default function profileRoutes<T extends AnonymousRouter>(router: T, provider: Provider) {
|
|
|
|
router.get(profileRoute, async (ctx, next) => {
|
|
|
|
const { accountId } = await provider.Session.get(ctx);
|
|
|
|
|
|
|
|
if (!accountId) {
|
|
|
|
throw new RequestError('auth.unauthorized');
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await findUserById(accountId);
|
|
|
|
|
|
|
|
ctx.body = pick(user, ...userInfoSelectFields);
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
router.patch(
|
|
|
|
`${profileRoute}/username`,
|
|
|
|
koaGuard({
|
|
|
|
body: object({ username: string().regex(usernameRegEx) }),
|
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const userId = await checkSessionHealth(ctx, provider, verificationTimeout);
|
|
|
|
|
|
|
|
assertThat(userId, new RequestError('auth.unauthorized'));
|
|
|
|
|
|
|
|
const { username } = ctx.guard.body;
|
|
|
|
|
|
|
|
await checkSignUpIdentifierCollision({ username }, userId);
|
|
|
|
|
|
|
|
const user = await updateUserById(userId, { username }, 'replace');
|
|
|
|
|
|
|
|
ctx.body = pick(user, ...userInfoSelectFields);
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
2022-11-11 01:29:37 -05:00
|
|
|
|
2022-11-16 00:32:16 -05:00
|
|
|
router.patch(
|
2022-11-11 01:29:37 -05:00
|
|
|
`${profileRoute}/password`,
|
|
|
|
koaGuard({
|
|
|
|
body: object({ password: string().regex(passwordRegEx) }),
|
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const userId = await checkSessionHealth(ctx, provider, verificationTimeout);
|
|
|
|
|
|
|
|
assertThat(userId, new RequestError('auth.unauthorized'));
|
|
|
|
|
|
|
|
const { password } = ctx.guard.body;
|
|
|
|
const { passwordEncrypted: oldPasswordEncrypted } = await findUserById(userId);
|
|
|
|
|
|
|
|
assertThat(
|
|
|
|
!oldPasswordEncrypted || !(await argon2Verify({ password, hash: oldPasswordEncrypted })),
|
|
|
|
new RequestError({ code: 'user.same_password', status: 422 })
|
|
|
|
);
|
|
|
|
|
|
|
|
const { passwordEncrypted, passwordEncryptionMethod } = await encryptUserPassword(password);
|
|
|
|
|
|
|
|
await updateUserById(userId, { passwordEncrypted, passwordEncryptionMethod });
|
|
|
|
|
|
|
|
ctx.status = 204;
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
2022-11-16 00:32:16 -05:00
|
|
|
|
|
|
|
router.patch(
|
|
|
|
`${profileRoute}/email`,
|
|
|
|
koaGuard({
|
|
|
|
body: object({ primaryEmail: string().regex(emailRegEx) }),
|
|
|
|
}),
|
|
|
|
async (ctx, next) => {
|
|
|
|
const userId = await checkSessionHealth(ctx, provider, verificationTimeout);
|
|
|
|
|
|
|
|
assertThat(userId, new RequestError('auth.unauthorized'));
|
|
|
|
|
|
|
|
const { primaryEmail } = ctx.guard.body;
|
|
|
|
|
|
|
|
await checkSignUpIdentifierCollision({ primaryEmail });
|
|
|
|
await updateUserById(userId, { primaryEmail });
|
|
|
|
|
|
|
|
ctx.status = 204;
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
router.delete(`${profileRoute}/email`, async (ctx, next) => {
|
|
|
|
const userId = await checkSessionHealth(ctx, provider, verificationTimeout);
|
|
|
|
|
|
|
|
assertThat(userId, new RequestError('auth.unauthorized'));
|
|
|
|
|
|
|
|
const { primaryEmail } = await findUserById(userId);
|
|
|
|
|
|
|
|
assertThat(primaryEmail, new RequestError({ code: 'user.email_not_exists', status: 422 }));
|
|
|
|
|
|
|
|
await updateUserById(userId, { primaryEmail: null });
|
|
|
|
|
|
|
|
ctx.status = 204;
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
2022-11-10 21:50:20 -05:00
|
|
|
}
|