mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
feat(sign-in): replace id with username
This commit is contained in:
parent
bbb6be1e24
commit
94c5115bb0
3 changed files with 14 additions and 7 deletions
|
@ -5,6 +5,13 @@ import { convertToIdentifiers, insertInto } from '@/database/utils';
|
|||
|
||||
const { table, fields } = convertToIdentifiers(Users);
|
||||
|
||||
export const findUserByUsername = async (username: string) =>
|
||||
pool.one<UserDBEntry>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`,`)}
|
||||
from ${table}
|
||||
where ${fields.username}=${username}
|
||||
`);
|
||||
|
||||
export const findUserById = async (id: string) =>
|
||||
pool.one<UserDBEntry>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`,`)}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -7,7 +7,7 @@ export const signInBasic = async (username: string, password: string) => {
|
|||
return ky
|
||||
.post('/api/sign-in', {
|
||||
json: {
|
||||
id: username,
|
||||
username,
|
||||
password,
|
||||
},
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue