|
1 | | -import { Controller, Get } from '@nestjs/common'; |
| 1 | +import { Controller, Get, Post, Request, UseGuards } from '@nestjs/common'; |
2 | 2 | import { AppService } from './app.service'; |
| 3 | +import { LocalAuthGuard } from './auth/local-auth.guard'; |
| 4 | +import { AuthService } from './auth/auth.service'; |
| 5 | +import { GoogleStrategy } from './auth/google.strategy'; |
| 6 | +import { GoogleAuthGuard } from './auth/google-auth.guard'; |
3 | 7 |
|
4 | 8 | @Controller() |
5 | 9 | export class AppController { |
6 | | - constructor(private readonly appService: AppService) {} |
| 10 | + constructor( |
| 11 | + private readonly appService: AppService, |
| 12 | + private authService: AuthService, |
| 13 | + private googleStrategy: GoogleStrategy, |
| 14 | + ) {} |
7 | 15 |
|
8 | 16 | @Get() |
9 | 17 | getHello(): string { |
10 | 18 | return this.appService.getHello(); |
11 | 19 | } |
| 20 | + |
| 21 | + @UseGuards(LocalAuthGuard) |
| 22 | + @Post('auth/login') |
| 23 | + async login(@Request() req) { |
| 24 | + return this.authService.login(req.user); |
| 25 | + } |
| 26 | + |
| 27 | + @UseGuards(LocalAuthGuard) |
| 28 | + @Post('auth/logout') |
| 29 | + async logout(@Request() req) { |
| 30 | + return req.logout(); |
| 31 | + } |
| 32 | + |
| 33 | + @UseGuards(GoogleAuthGuard) |
| 34 | + @Get('auth/google-login') |
| 35 | + async googleLogin(@Request() req) { |
| 36 | + console.log('request', req); |
| 37 | + return this.googleStrategy.authenticate(req); |
| 38 | + } |
| 39 | + |
| 40 | + @UseGuards(GoogleAuthGuard) |
| 41 | + @Get('auth/google/google-redirect') |
| 42 | + async googleLoginCallback(@Request() req) { |
| 43 | + console.log(req); |
| 44 | + console.log('REDIrect'); |
| 45 | + return req.user; |
| 46 | + // return this.googleStrategy.validate(req); |
| 47 | + } |
12 | 48 | } |
0 commit comments