mirror of
https://github.com/logto-io/logto.git
synced 2025-01-27 21:39:16 -05:00
refactor: implement koaUser middleware
* refactor: extract code block of getting detailed user info in koaAuth as middleware koaUser * refactor: fix according to comments * refactor: fix methods' and properties' naming
This commit is contained in:
parent
18a142ab65
commit
6d5dd55e12
3 changed files with 33 additions and 7 deletions
|
@ -1,20 +1,17 @@
|
|||
import { IncomingHttpHeaders } from 'http';
|
||||
|
||||
import { UserInfo, userInfoSelectFields } from '@logto/schemas';
|
||||
import { jwtVerify } from 'jose/jwt/verify';
|
||||
import { MiddlewareType, Request } from 'koa';
|
||||
import { IRouterParamContext } from 'koa-router';
|
||||
import pick from 'lodash.pick';
|
||||
|
||||
import { developmentUserId, isProduction } from '@/env/consts';
|
||||
import RequestError from '@/errors/RequestError';
|
||||
import { publicKey, issuer, adminResource } from '@/oidc/consts';
|
||||
import { findUserById } from '@/queries/user';
|
||||
import assertThat from '@/utils/assert-that';
|
||||
|
||||
export type WithAuthContext<ContextT extends IRouterParamContext = IRouterParamContext> =
|
||||
ContextT & {
|
||||
user: UserInfo;
|
||||
auth: string;
|
||||
};
|
||||
|
||||
const bearerTokenIdentifier = 'Bearer';
|
||||
|
@ -57,8 +54,7 @@ export default function koaAuth<
|
|||
return async (ctx, next) => {
|
||||
try {
|
||||
const userId = await getUserIdFromRequest(ctx.request);
|
||||
const user = await findUserById(userId);
|
||||
ctx.user = pick(user, ...userInfoSelectFields);
|
||||
ctx.auth = userId;
|
||||
} catch {
|
||||
throw new RequestError({ code: 'auth.unauthorized', status: 401 });
|
||||
}
|
||||
|
|
29
packages/core/src/middleware/koa-user-info.ts
Normal file
29
packages/core/src/middleware/koa-user-info.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { UserInfo, userInfoSelectFields } from '@logto/schemas';
|
||||
import { MiddlewareType } from 'koa';
|
||||
import pick from 'lodash.pick';
|
||||
|
||||
import RequestError from '@/errors/RequestError';
|
||||
import { WithAuthContext } from '@/middleware/koa-auth';
|
||||
import { findUserById } from '@/queries/user';
|
||||
|
||||
export type WithUserInfoContext<ContextT extends WithAuthContext = WithAuthContext> = ContextT & {
|
||||
userInfo: UserInfo;
|
||||
};
|
||||
|
||||
export default function koaUserInfo<
|
||||
StateT,
|
||||
ContextT extends WithAuthContext,
|
||||
ResponseBodyT
|
||||
>(): MiddlewareType<StateT, WithUserInfoContext<ContextT>, ResponseBodyT> {
|
||||
return async (ctx, next) => {
|
||||
try {
|
||||
const { auth: userId } = ctx;
|
||||
const userInfo = await findUserById(userId);
|
||||
ctx.userInfo = pick(userInfo, ...userInfoSelectFields);
|
||||
} catch {
|
||||
throw new RequestError({ code: 'auth.unauthorized', status: 401 });
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
}
|
|
@ -2,6 +2,7 @@ import Router from 'koa-router';
|
|||
|
||||
import { WithAuthContext } from '@/middleware/koa-auth';
|
||||
import { WithI18nContext } from '@/middleware/koa-i18next';
|
||||
import { WithUserInfoContext } from '@/middleware/koa-user-info';
|
||||
|
||||
export type AnonymousRouter = Router<unknown, WithI18nContext>;
|
||||
export type AuthedRouter = Router<unknown, WithAuthContext<WithI18nContext>>;
|
||||
export type AuthedRouter = Router<unknown, WithUserInfoContext<WithAuthContext<WithI18nContext>>>;
|
||||
|
|
Loading…
Add table
Reference in a new issue