0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

feat(core): get /session/register/:username/existence (#275)

* feat(core): add username existence check when resgitering

* feat(core): add username existence check

* feat(core): update username guard to avoid empty strings
This commit is contained in:
Darcy Ye 2022-02-24 18:30:34 +08:00 committed by GitHub
parent 75d2bb3e9b
commit bd6dc4261c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View file

@ -36,6 +36,7 @@ import {
findUserByIdentity, findUserByIdentity,
} from '@/queries/user'; } from '@/queries/user';
import assertThat from '@/utils/assert-that'; import assertThat from '@/utils/assert-that';
import { usernameRegEx } from '@/utils/regex';
import { AnonymousRouter } from './types'; import { AnonymousRouter } from './types';
@ -308,6 +309,18 @@ export default function sessionRoutes<T extends AnonymousRouter>(router: T, prov
} }
); );
router.get(
'/session/register/:username/existence',
koaGuard({ params: object({ username: string().regex(usernameRegEx) }) }),
async (ctx, next) => {
const { username } = ctx.guard.params;
ctx.body = { existence: await hasUser(username) };
return next();
}
);
router.post( router.post(
'/session/register/passwordless/phone/send-passcode', '/session/register/passwordless/phone/send-passcode',
koaGuard({ body: object({ phone: string() }) }), koaGuard({ body: object({ phone: string() }) }),

View file

@ -1,2 +1,3 @@
export const emailRegEx = /^\S+@\S+\.\S+$/; export const emailRegEx = /^\S+@\S+\.\S+$/;
export const phoneRegEx = /^[1-9]\d{10}$/; export const phoneRegEx = /^[1-9]\d{10}$/;
export const usernameRegEx = /^[A-Za-z]\w*$/;