feat: 修复退出登录登录信息未失效

This commit is contained in:
2025-09-06 20:42:52 +08:00
parent cd99485c9a
commit f0e1a649ee
7 changed files with 52 additions and 14 deletions

View File

@@ -17,11 +17,17 @@ export class AuthController {
@Post('/login')
async login(ctx: Context) {
if (!ctx.session.isNew) {
if (ctx.session.user) {
return ctx.session.user;
}
const { code } = ctx.request.body as LoginRequestBody;
const { access_token } = await gitea.getToken(code);
const { access_token, refresh_token, expires_in } =
await gitea.getToken(code);
const giteaAuth = {
access_token,
refresh_token,
expires_at: Date.now() + expires_in * 1000,
};
const giteaUser = await gitea.getUserInfo(access_token);
log.debug(this.TAG, 'gitea user: %o', giteaUser);
const exist = await prisma.user.findFirst({
@@ -61,9 +67,15 @@ export class AuthController {
log.debug(this.TAG, '更新用户信息成功 %o', updatedUser);
ctx.session.user = updatedUser;
}
ctx.session.gitea = giteaAuth;
return ctx.session.user;
}
@Get('logout')
async logout(ctx: Context) {
ctx.session.user = null;
}
@Get('info')
async info(ctx: Context) {
return ctx.session?.user;

View File

@@ -10,11 +10,10 @@ export class Authorization implements Middleware {
apply(app: Koa) {
app.use(async (ctx: Koa.Context, next: Koa.Next) => {
console.log('ctx.path', ctx.path)
if (this.ignoreAuth.includes(ctx.path)) {
return next();
}
if (ctx.session.isNew) {
if (ctx.session.user == null) {
ctx.throw(401, 'Unauthorized');
}
await next();

Binary file not shown.