[UPD] add google auth route

This commit is contained in:
Quentin TREHEUX
2023-04-16 08:39:00 +02:00
parent 99f253e8b4
commit f6ec33b416
+26
View File
@@ -1,3 +1,5 @@
// users/users.controller.ts
import {
Controller,
Get,
@@ -7,7 +9,11 @@ import {
Param,
Delete,
NotFoundException,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
@@ -52,4 +58,24 @@ export class UsersController {
remove(@Param('id') id: string): Promise<User> {
return this.usersService.deleteUser({ id: +id });
}
// Google OAuth - Call this endpoint to start the OAuth flow
@Get('google')
@UseGuards(AuthGuard('google'))
async googleAuth() {}
// Google OAuth - Callback endpoint
@Get('google/callback')
@UseGuards(AuthGuard('google'))
async googleAuthCallback(@Req() req, @Res() res) {
try {
const user = req.user;
// Redirect to frontend with auth information
res.redirect(''); // TODO: Add frontend url format example : http://localhost:3000/success?user=${JSON.stringify(user)}`
} catch (err) {
// Redirect to frontend with error information
console.error(err);
res.redirect(''); // TODO: Add frontend url format example : http://localhost:3000/error?error=${err}`
}
}
}