auth.controller.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Body, Controller, Get, Param, Post, Req, Res, UseGuards, Session } from '@nestjs/common';
  2. import { AuthService } from './auth.service';
  3. import { JwtGuard, LocalGuard, PreviewGuard } from '@/common/guards';
  4. import { ChangePasswordDto, RegisterUserDto, LoginUserDto } from './dto';
  5. import { UserService } from '@/modules/user/user.service';
  6. import * as svgCaptcha from 'svg-captcha';
  7. import { CustomException, ErrorCode } from '@/common/exceptions/custom.exception';
  8. import { ConfigService } from '@nestjs/config';
  9. import { ApiBearerAuth } from '@nestjs/swagger';
  10. @Controller('auth')
  11. export class AuthController {
  12. constructor(
  13. private readonly authService: AuthService,
  14. private userService: UserService,
  15. private configService: ConfigService,
  16. ) {}
  17. @UseGuards(LocalGuard)
  18. @Post('login')
  19. async login(
  20. @Session() session: Record<string, any>,
  21. @Req() req: any,
  22. @Body() body: LoginUserDto,
  23. ) {
  24. // 预览环境下可快速登录,不用验证码
  25. // if (this.configService.get('IS_PREVIEW') === 'true' && body.isQuick) {
  26. // return this.authService.login(req.user, req.session?.code);
  27. // }
  28. // 判断验证码是否正确
  29. // console.log('session', req.user, session, req, body.captcha);
  30. if (session.code?.toLocaleLowerCase() !== body.captcha?.toLocaleLowerCase()) {
  31. throw new CustomException(ErrorCode.ERR_10003);
  32. }
  33. return this.authService.login(req.user, req.session?.code);
  34. }
  35. @Post('register')
  36. @UseGuards(PreviewGuard)
  37. async register(@Body() user: RegisterUserDto) {
  38. return this.userService.create(user);
  39. }
  40. @Get('refresh/token')
  41. @ApiBearerAuth('JWT')
  42. @UseGuards(JwtGuard)
  43. async refreshToken(@Req() req: any) {
  44. return this.authService.generateToken(req.user);
  45. }
  46. @Post('current-role/switch/:roleCode')
  47. @ApiBearerAuth('JWT')
  48. @UseGuards(JwtGuard)
  49. async switchCurrentRole(@Req() req: any, @Param('roleCode') roleCode: string) {
  50. return this.authService.switchCurrentRole(req.user, roleCode);
  51. }
  52. @Post('logout')
  53. @ApiBearerAuth('JWT')
  54. @UseGuards(JwtGuard)
  55. async logout(@Req() req: any) {
  56. return this.authService.logout(req.user);
  57. }
  58. @Get('captcha')
  59. async createCaptcha(@Req() req, @Res() res) {
  60. const captcha = svgCaptcha.create({
  61. size: 4,
  62. fontSize: 40,
  63. width: 80,
  64. height: 40,
  65. background: '#fff',
  66. color: true,
  67. });
  68. req.session.code = captcha.text || '';
  69. res.type('image/svg+xml');
  70. res.send(captcha.data);
  71. }
  72. @Post('password')
  73. @ApiBearerAuth('JWT')
  74. @UseGuards(JwtGuard, PreviewGuard)
  75. async changePassword(@Req() req: any, @Body() body: ChangePasswordDto) {
  76. const ret = await this.authService.validateUser(req.user.username, body.oldPassword);
  77. if (!ret) {
  78. throw new CustomException(ErrorCode.ERR_10004);
  79. }
  80. // 修改密码
  81. await this.userService.resetPassword(req.user.id, body.newPassword);
  82. // 修改密码后退出登录
  83. await this.authService.logout(req.user);
  84. return true;
  85. }
  86. }