import { Body , Controller , Delete , Get , Post , Query , Scope , } from '@nestjs/common' ; import { EventEmitter2 } from '@nestjs/event-emitter' ; import { ApiBearerAuth , ApiOperation } from '@nestjs/swagger' ; import { Transform } from 'class-transformer' ; import { IsDate , isMongoId , IsNotEmpty , IsOptional , IsString , } from 'class-validator' ; import { Auth } from '~/common/decorator/auth.decorator' ; import { ApiName } from '~/common/decorator/openapi.decorator' ; import { IsMaster as Master } from '~/common/decorator/role.decorator' ; import { EventBusEvents } from '~/constants/event.constant' ; import { MongoIdDto } from '~/shared/dto/id.dto' ; import { AuthService } from './auth.service' ; export class TokenDto { @ IsDate ( ) @ IsOptional ( ) @ Transform ( ( { value : v } ) => new Date ( v ) ) expired ? : Date ; @ IsString ( ) @ IsNotEmpty ( ) name : string ; } @ Controller ( { path : 'auth' , scope : Scope . REQUEST , } ) @ ApiName export class AuthController { constructor ( private readonly authService , private readonly eventEmitter ) { } @ Get ( ) @ ApiOperation ( { summary : '判断当前 Token 是否有效 ' } ) @ ApiBearerAuth ( ) checkLogged ( @ Master ( ) isMaster ) { return { ok : ~ ~ isMaster , isGuest : ! isMaster } ; } @ Get ( 'token' ) @ Auth ( ) async getOrVerifyToken ( @ Query ( 'token' ) token ? , @ Query ( 'id' ) id ? ) { if ( typeof token === 'string' ) { return await this . authService . verifyCustomToken ( token ) ; } if ( id && typeof id === 'string' && isMongoId ( id ) ) { return await this . authService . getTokenSecret ( id ) ; } return await this . authService . getAllAccessToken ( ) ; } @ Post ( 'token' ) @ Auth ( ) async generateToken ( @ Body ( ) body ) { const { expired , name } = body ; const token = await this . authService . generateAccessToken ( ) ; const model = { expired , token , name , } ; await this . authService . saveToken ( model ) ; return model ; } @ Delete ( 'token' ) @ Auth ( ) async deleteToken ( @ Query ( ) query ) { const { id } = query ; await this . authService . deleteToken ( id ) ; this . eventEmitter . emit ( EventBusEvents . TokenExpired , id ) ; return 'OK' ; } }