refactor(server): throw Unauthorized instead if user is not signed in (#5746)

This commit is contained in:
liuyi
2024-01-31 02:12:21 +00:00
parent 5f3c04b51e
commit db8e49b046

View File

@@ -4,6 +4,7 @@ import {
Inject, Inject,
Injectable, Injectable,
SetMetadata, SetMetadata,
UnauthorizedException,
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { Reflector } from '@nestjs/core'; import { Reflector } from '@nestjs/core';
@@ -69,6 +70,10 @@ class AuthGuard implements CanActivate {
'isPublic', 'isPublic',
context.getHandler() context.getHandler()
); );
// FIXME(@forehalo): @Publicable() is duplicated with @CurrentUser() user?: User
// ^ optional
// we can prefetch user session in each request even before this `Guard`
// api can be public, but if user is logged in, we can get user info // api can be public, but if user is logged in, we can get user info
const isPublicable = this.reflector.get<boolean>( const isPublicable = this.reflector.get<boolean>(
'isPublicable', 'isPublicable',
@@ -94,7 +99,7 @@ class AuthGuard implements CanActivate {
const { body = {}, cookies, status = 200 } = session; const { body = {}, cookies, status = 200 } = session;
if (!body && !isPublicable) { if (!body && !isPublicable) {
return false; throw new UnauthorizedException('You are not signed in.');
} }
// @ts-expect-error body is user here // @ts-expect-error body is user here