115 lines
3.8 KiB
TypeScript
115 lines
3.8 KiB
TypeScript
|
|
import {
|
|||
|
|
Controller,
|
|||
|
|
Post,
|
|||
|
|
Body,
|
|||
|
|
Req,
|
|||
|
|
HttpCode,
|
|||
|
|
HttpStatus,
|
|||
|
|
UseGuards,
|
|||
|
|
Get
|
|||
|
|
} from '@nestjs/common';
|
|||
|
|
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
|
|||
|
|
import type { Request } from 'express';
|
|||
|
|
import { AuthService } from '../services/AuthService';
|
|||
|
|
import { LoginDto, RefreshTokenDto, LogoutDto } from '../dto/AuthDto';
|
|||
|
|
import { JwtAuthGuard } from '../guards/JwtAuthGuard';
|
|||
|
|
import type { RequestWithUser } from '../interfaces/user.interface';
|
|||
|
|
|
|||
|
|
@ApiTags('认证管理')
|
|||
|
|
@Controller('auth')
|
|||
|
|
export class AuthController {
|
|||
|
|
constructor(private readonly authService: AuthService) {}
|
|||
|
|
|
|||
|
|
@Post('admin/login')
|
|||
|
|
@ApiOperation({ summary: '管理员登录' })
|
|||
|
|
@ApiResponse({ status: 200, description: '登录成功' })
|
|||
|
|
@ApiResponse({ status: 401, description: '用户名或密码错误' })
|
|||
|
|
@HttpCode(HttpStatus.OK)
|
|||
|
|
async adminLogin(
|
|||
|
|
@Body() loginDto: LoginDto,
|
|||
|
|
@Req() req: Request
|
|||
|
|
) {
|
|||
|
|
const ipAddress = req.ip || req.connection.remoteAddress || 'unknown';
|
|||
|
|
const userAgent = req.headers['user-agent'] || 'unknown';
|
|||
|
|
|
|||
|
|
return await this.authService.adminLogin(loginDto, ipAddress, userAgent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Post('member/login')
|
|||
|
|
@ApiOperation({ summary: '会员登录' })
|
|||
|
|
@ApiResponse({ status: 200, description: '登录成功' })
|
|||
|
|
@ApiResponse({ status: 401, description: '用户名或密码错误' })
|
|||
|
|
@HttpCode(HttpStatus.OK)
|
|||
|
|
async memberLogin(
|
|||
|
|
@Body() loginDto: LoginDto,
|
|||
|
|
@Req() req: Request
|
|||
|
|
) {
|
|||
|
|
const ipAddress = req.ip || req.connection.remoteAddress || 'unknown';
|
|||
|
|
const userAgent = req.headers['user-agent'] || 'unknown';
|
|||
|
|
|
|||
|
|
return await this.authService.memberLogin(loginDto, ipAddress, userAgent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Post('refresh')
|
|||
|
|
@ApiOperation({ summary: '刷新Token' })
|
|||
|
|
@ApiResponse({ status: 200, description: 'Token刷新成功' })
|
|||
|
|
@ApiResponse({ status: 401, description: '刷新Token无效或已过期' })
|
|||
|
|
@HttpCode(HttpStatus.OK)
|
|||
|
|
async refreshToken(@Body() refreshTokenDto: RefreshTokenDto) {
|
|||
|
|
return await this.authService.refreshToken(refreshTokenDto);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Post('logout')
|
|||
|
|
@ApiOperation({ summary: '用户登出' })
|
|||
|
|
@ApiResponse({ status: 200, description: '登出成功' })
|
|||
|
|
@HttpCode(HttpStatus.OK)
|
|||
|
|
async logout(@Body() logoutDto: LogoutDto) {
|
|||
|
|
return await this.authService.logout(logoutDto);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Get('profile')
|
|||
|
|
@UseGuards(JwtAuthGuard)
|
|||
|
|
@ApiOperation({ summary: '获取当前用户信息' })
|
|||
|
|
@ApiResponse({ status: 200, description: '获取用户信息成功' })
|
|||
|
|
@ApiResponse({ status: 401, description: '未授权' })
|
|||
|
|
@ApiBearerAuth()
|
|||
|
|
async getProfile(@Req() req: RequestWithUser) {
|
|||
|
|
// 用户信息已经在JWT中,通过守卫验证后可以直接返回
|
|||
|
|
return {
|
|||
|
|
userId: req.user.userId,
|
|||
|
|
username: req.user.username,
|
|||
|
|
userType: req.user.userType,
|
|||
|
|
siteId: req.user.siteId,
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Post('admin/logout')
|
|||
|
|
@UseGuards(JwtAuthGuard)
|
|||
|
|
@ApiOperation({ summary: '管理员登出' })
|
|||
|
|
@ApiResponse({ status: 200, description: '登出成功' })
|
|||
|
|
@ApiResponse({ status: 401, description: '未授权' })
|
|||
|
|
@ApiBearerAuth()
|
|||
|
|
@HttpCode(HttpStatus.OK)
|
|||
|
|
async adminLogout(@Req() req: Request) {
|
|||
|
|
const token = req.headers.authorization?.replace('Bearer ', '');
|
|||
|
|
if (token) {
|
|||
|
|
return await this.authService.logout({ token });
|
|||
|
|
}
|
|||
|
|
return { message: '登出成功' };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Post('member/logout')
|
|||
|
|
@UseGuards(JwtAuthGuard)
|
|||
|
|
@ApiOperation({ summary: '会员登出' })
|
|||
|
|
@ApiResponse({ status: 200, description: '登出成功' })
|
|||
|
|
@ApiResponse({ status: 401, description: '未授权' })
|
|||
|
|
@ApiBearerAuth()
|
|||
|
|
@HttpCode(HttpStatus.OK)
|
|||
|
|
async memberLogout(@Req() req: Request) {
|
|||
|
|
const token = req.headers.authorization?.replace('Bearer ', '');
|
|||
|
|
if (token) {
|
|||
|
|
return await this.authService.logout({ token });
|
|||
|
|
}
|
|||
|
|
return { message: '登出成功' };
|
|||
|
|
}
|
|||
|
|
}
|