70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
|
import {
|
|
Controller,
|
|
Post,
|
|
Body,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Req,
|
|
BadRequestException,
|
|
} from '@nestjs/common';
|
|
import { AuthService } from './auth.service';
|
|
import type { Request } from 'express';
|
|
import { IsString, Allow, IsOptional } from 'class-validator';
|
|
|
|
class AppleLoginDto {
|
|
@IsString()
|
|
identityToken: string;
|
|
|
|
@IsString()
|
|
authorizationCode: string;
|
|
|
|
@Allow()
|
|
@IsOptional()
|
|
user?: any;
|
|
}
|
|
|
|
class RefreshDto {
|
|
@IsString()
|
|
refreshToken: string;
|
|
}
|
|
|
|
@ApiTags('auth')
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(private readonly authService: AuthService) {}
|
|
|
|
@Post('apple')
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: 'Apple 登录' })
|
|
@ApiResponse({ status: 200, description: '登录成功' })
|
|
@ApiResponse({ status: 401, description: '身份验证失败' })
|
|
async appleLogin(@Body() body: AppleLoginDto) {
|
|
return this.authService.appleLogin(body);
|
|
}
|
|
|
|
@Post('refresh')
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: '刷新令牌' })
|
|
@ApiResponse({ status: 200, description: '刷新成功' })
|
|
@ApiResponse({ status: 401, description: '刷新令牌无效' })
|
|
async refresh(@Body() body: RefreshDto) {
|
|
if (!body.refreshToken) {
|
|
throw new BadRequestException('缺少 refreshToken');
|
|
}
|
|
return this.authService.refresh(body.refreshToken);
|
|
}
|
|
|
|
@Post('logout')
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: '退出登录' })
|
|
@ApiResponse({ status: 200, description: '退出成功' })
|
|
async logout(@Req() req: Request) {
|
|
const user = (req as any).user;
|
|
if (user?.id) {
|
|
await this.authService.logout(user.id);
|
|
}
|
|
return { success: true, message: '已退出登录' };
|
|
}
|
|
}
|