mirror of
https://github.com/logto-io/logto.git
synced 2025-02-17 22:04:19 -05:00
feat(sign-in): replace id with username
This commit is contained in:
parent
261efbab63
commit
cb7d53b05d
3 changed files with 14 additions and 7 deletions
|
@ -5,6 +5,13 @@ import { convertToIdentifiers, insertInto } from '@/database/utils';
|
||||||
|
|
||||||
const { table, fields } = convertToIdentifiers(Users);
|
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) =>
|
export const findUserById = async (id: string) =>
|
||||||
pool.one<UserDBEntry>(sql`
|
pool.one<UserDBEntry>(sql`
|
||||||
select ${sql.join(Object.values(fields), sql`,`)}
|
select ${sql.join(Object.values(fields), sql`,`)}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import assert from 'assert';
|
||||||
import Router from 'koa-router';
|
import Router from 'koa-router';
|
||||||
import { object, string } from 'zod';
|
import { object, string } from 'zod';
|
||||||
import { encryptPassword } from '@/utils/password';
|
import { encryptPassword } from '@/utils/password';
|
||||||
import { findUserById } from '@/queries/user';
|
import { findUserByUsername } from '@/queries/user';
|
||||||
import { Provider } from 'oidc-provider';
|
import { Provider } from 'oidc-provider';
|
||||||
import { conditional } from '@logto/essentials';
|
import { conditional } from '@logto/essentials';
|
||||||
import koaGuard from '@/middleware/koa-guard';
|
import koaGuard from '@/middleware/koa-guard';
|
||||||
|
@ -13,20 +13,20 @@ export default function signInRoutes(provider: Provider) {
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/sign-in',
|
'/sign-in',
|
||||||
koaGuard({ body: object({ id: string().optional(), password: string().optional() }) }),
|
koaGuard({ body: object({ username: string().optional(), password: string().optional() }) }),
|
||||||
async (ctx) => {
|
async (ctx) => {
|
||||||
const {
|
const {
|
||||||
prompt: { name },
|
prompt: { name },
|
||||||
} = await provider.interactionDetails(ctx.req, ctx.res);
|
} = await provider.interactionDetails(ctx.req, ctx.res);
|
||||||
|
|
||||||
if (name === 'login') {
|
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 {
|
try {
|
||||||
const { passwordEncrypted, passwordEncryptionMethod, passwordEncryptionSalt } =
|
const { id, passwordEncrypted, passwordEncryptionMethod, passwordEncryptionSalt } =
|
||||||
await findUserById(id);
|
await findUserByUsername(username);
|
||||||
|
|
||||||
assert(
|
assert(
|
||||||
passwordEncrypted && passwordEncryptionMethod && passwordEncryptionSalt,
|
passwordEncrypted && passwordEncryptionMethod && passwordEncryptionSalt,
|
||||||
|
|
|
@ -7,7 +7,7 @@ export const signInBasic = async (username: string, password: string) => {
|
||||||
return ky
|
return ky
|
||||||
.post('/api/sign-in', {
|
.post('/api/sign-in', {
|
||||||
json: {
|
json: {
|
||||||
id: username,
|
username,
|
||||||
password,
|
password,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue