diff --git a/packages/core/src/queries/user.ts b/packages/core/src/queries/user.ts index d2fc7526e..cc33cccfe 100644 --- a/packages/core/src/queries/user.ts +++ b/packages/core/src/queries/user.ts @@ -5,6 +5,13 @@ import { convertToIdentifiers, insertInto } from '@/database/utils'; const { table, fields } = convertToIdentifiers(Users); +export const findUserByUsername = async (username: string) => + pool.one(sql` + select ${sql.join(Object.values(fields), sql`,`)} + from ${table} + where ${fields.username}=${username} +`); + export const findUserById = async (id: string) => pool.one(sql` select ${sql.join(Object.values(fields), sql`,`)} diff --git a/packages/core/src/routes/sign-in.ts b/packages/core/src/routes/sign-in.ts index bf63df523..c7196a72e 100644 --- a/packages/core/src/routes/sign-in.ts +++ b/packages/core/src/routes/sign-in.ts @@ -2,7 +2,7 @@ import assert from 'assert'; import Router from 'koa-router'; import { object, string } from 'zod'; import { encryptPassword } from '@/utils/password'; -import { findUserById } from '@/queries/user'; +import { findUserByUsername } from '@/queries/user'; import { Provider } from 'oidc-provider'; import { conditional } from '@logto/essentials'; import koaGuard from '@/middleware/koa-guard'; @@ -13,20 +13,20 @@ export default function signInRoutes(provider: Provider) { router.post( '/sign-in', - koaGuard({ body: object({ id: string().optional(), password: string().optional() }) }), + koaGuard({ body: object({ username: string().optional(), password: string().optional() }) }), async (ctx) => { const { prompt: { name }, } = await provider.interactionDetails(ctx.req, ctx.res); if (name === 'login') { - const { id, password } = ctx.guard.body; + const { username, password } = ctx.guard.body; - assert(id && password, new RequestError(SignInErrorCode.InsufficientInfo)); + assert(username && password, new RequestError(SignInErrorCode.InsufficientInfo)); try { - const { passwordEncrypted, passwordEncryptionMethod, passwordEncryptionSalt } = - await findUserById(id); + const { id, passwordEncrypted, passwordEncryptionMethod, passwordEncryptionSalt } = + await findUserByUsername(username); assert( passwordEncrypted && passwordEncryptionMethod && passwordEncryptionSalt, diff --git a/packages/ui/src/apis/sign-in.ts b/packages/ui/src/apis/sign-in.ts index 97120ca44..777858094 100644 --- a/packages/ui/src/apis/sign-in.ts +++ b/packages/ui/src/apis/sign-in.ts @@ -7,7 +7,7 @@ export const signInBasic = async (username: string, password: string) => { return ky .post('/api/sign-in', { json: { - id: username, + username, password, }, })