|
| 1 | +import { |
| 2 | + Controller, |
| 3 | + Post, |
| 4 | + Get, |
| 5 | + Patch, |
| 6 | + Body, |
| 7 | + UseGuards, |
| 8 | + Req, |
| 9 | + Res, |
| 10 | + HttpCode, |
| 11 | + HttpStatus, |
| 12 | +} from '@nestjs/common'; |
| 13 | +import { AuthGuard } from '@nestjs/passport'; |
| 14 | +import { Request, Response } from 'express'; |
| 15 | +import { AuthService } from './auth.service'; |
| 16 | +import { JwtAuthGuard } from './guards/jwt-auth.guard'; |
| 17 | +import { CurrentDeveloper } from './decorators/current-developer.decorator'; |
| 18 | + |
| 19 | +@Controller('auth') |
| 20 | +export class AuthController { |
| 21 | + constructor(private readonly authService: AuthService) {} |
| 22 | + |
| 23 | + @Post('register') |
| 24 | + async register(@Body() body: { email: string; password: string; name: string }) { |
| 25 | + return this.authService.register(body.email, body.password, body.name); |
| 26 | + } |
| 27 | + |
| 28 | + @Post('login') |
| 29 | + @HttpCode(HttpStatus.OK) |
| 30 | + @UseGuards(AuthGuard('local')) |
| 31 | + async login(@Req() req: Request, @Res({ passthrough: true }) res: Response) { |
| 32 | + const result = await this.authService.login(req.user as any); |
| 33 | + res.cookie('refresh_token', result.refreshToken, { |
| 34 | + httpOnly: true, |
| 35 | + secure: process.env.NODE_ENV === 'production', |
| 36 | + sameSite: 'lax', |
| 37 | + maxAge: 30 * 24 * 60 * 60 * 1000, |
| 38 | + path: '/auth/refresh', |
| 39 | + }); |
| 40 | + return { accessToken: result.accessToken, developer: result.developer }; |
| 41 | + } |
| 42 | + |
| 43 | + @Post('refresh') |
| 44 | + @HttpCode(HttpStatus.OK) |
| 45 | + async refresh(@Req() req: Request, @Res({ passthrough: true }) res: Response) { |
| 46 | + const token = req.cookies?.refresh_token || req.body?.refreshToken; |
| 47 | + if (!token) { |
| 48 | + return { error: 'No refresh token provided' }; |
| 49 | + } |
| 50 | + const result = await this.authService.refresh(token); |
| 51 | + res.cookie('refresh_token', result.refreshToken, { |
| 52 | + httpOnly: true, |
| 53 | + secure: process.env.NODE_ENV === 'production', |
| 54 | + sameSite: 'lax', |
| 55 | + maxAge: 30 * 24 * 60 * 60 * 1000, |
| 56 | + path: '/auth/refresh', |
| 57 | + }); |
| 58 | + return { accessToken: result.accessToken, developer: result.developer }; |
| 59 | + } |
| 60 | + |
| 61 | + @Post('logout') |
| 62 | + @HttpCode(HttpStatus.OK) |
| 63 | + async logout(@Req() req: Request, @Res({ passthrough: true }) res: Response) { |
| 64 | + const token = req.cookies?.refresh_token || req.body?.refreshToken; |
| 65 | + if (token) { |
| 66 | + await this.authService.logout(token); |
| 67 | + } |
| 68 | + res.clearCookie('refresh_token', { path: '/auth/refresh' }); |
| 69 | + return { message: 'Logged out' }; |
| 70 | + } |
| 71 | + |
| 72 | + @Get('google') |
| 73 | + @UseGuards(AuthGuard('google')) |
| 74 | + googleAuth() { |
| 75 | + // Passport redirects to Google |
| 76 | + } |
| 77 | + |
| 78 | + @Get('google/callback') |
| 79 | + @UseGuards(AuthGuard('google')) |
| 80 | + async googleCallback(@Req() req: Request, @Res() res: Response) { |
| 81 | + const result = await this.authService.login(req.user as any); |
| 82 | + const consoleUrl = process.env.CONSOLE_URL || 'http://localhost:3000'; |
| 83 | + res.cookie('refresh_token', result.refreshToken, { |
| 84 | + httpOnly: true, |
| 85 | + secure: process.env.NODE_ENV === 'production', |
| 86 | + sameSite: 'lax', |
| 87 | + maxAge: 30 * 24 * 60 * 60 * 1000, |
| 88 | + path: '/auth/refresh', |
| 89 | + }); |
| 90 | + res.redirect(`${consoleUrl}/auth/callback?token=${result.accessToken}`); |
| 91 | + } |
| 92 | + |
| 93 | + @Get('github') |
| 94 | + @UseGuards(AuthGuard('github')) |
| 95 | + githubAuth() { |
| 96 | + // Passport redirects to GitHub |
| 97 | + } |
| 98 | + |
| 99 | + @Get('github/callback') |
| 100 | + @UseGuards(AuthGuard('github')) |
| 101 | + async githubCallback(@Req() req: Request, @Res() res: Response) { |
| 102 | + const result = await this.authService.login(req.user as any); |
| 103 | + const consoleUrl = process.env.CONSOLE_URL || 'http://localhost:3000'; |
| 104 | + res.cookie('refresh_token', result.refreshToken, { |
| 105 | + httpOnly: true, |
| 106 | + secure: process.env.NODE_ENV === 'production', |
| 107 | + sameSite: 'lax', |
| 108 | + maxAge: 30 * 24 * 60 * 60 * 1000, |
| 109 | + path: '/auth/refresh', |
| 110 | + }); |
| 111 | + res.redirect(`${consoleUrl}/auth/callback?token=${result.accessToken}`); |
| 112 | + } |
| 113 | + |
| 114 | + @Post('forgot-password') |
| 115 | + @HttpCode(HttpStatus.OK) |
| 116 | + async forgotPassword(@Body() body: { email: string }) { |
| 117 | + return this.authService.forgotPassword(body.email); |
| 118 | + } |
| 119 | + |
| 120 | + @Post('reset-password') |
| 121 | + @HttpCode(HttpStatus.OK) |
| 122 | + async resetPassword(@Body() body: { token: string; password: string }) { |
| 123 | + return this.authService.resetPassword(body.token, body.password); |
| 124 | + } |
| 125 | + |
| 126 | + @Get('me') |
| 127 | + @UseGuards(JwtAuthGuard) |
| 128 | + async me(@CurrentDeveloper() dev: { id: string }) { |
| 129 | + return this.authService.getProfile(dev.id); |
| 130 | + } |
| 131 | + |
| 132 | + @Patch('me') |
| 133 | + @UseGuards(JwtAuthGuard) |
| 134 | + async updateMe( |
| 135 | + @CurrentDeveloper() dev: { id: string }, |
| 136 | + @Body() body: { name?: string; avatarUrl?: string }, |
| 137 | + ) { |
| 138 | + return this.authService.updateProfile(dev.id, body); |
| 139 | + } |
| 140 | +} |
0 commit comments