Skip to content

Commit

Permalink
Merge pull request #58 from UwUClub/AW-21-Delete-task
Browse files Browse the repository at this point in the history
Change google auth
  • Loading branch information
Valegox authored Jan 14, 2024
2 parents c415c7f + 9290f6e commit 3355d94
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions api/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { Body, Controller, Post } from '@nestjs/common';
import { Body, Controller, Post, Headers, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';
import { SignInDto } from './_utils/dto/request/sign-in.dto';
import { CreateUserDto } from '../users/_utils/dto/request/create-user.dto';
import { ApiTags } from '@nestjs/swagger';
import { ApiHeader, ApiTags } from '@nestjs/swagger';
import { GoogleLoginDto } from './_utils/dto/request/google-login.dto';
import { UsersService } from '../users/users.service';
import * as jwt from 'jsonwebtoken';
import { ConfigService } from '@nestjs/config';
import { EnvironmentVariables } from '../_utils/config';
import { JwtStrategy } from './jwt/jwt.startegy';

@ApiTags('Auth')
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
constructor(
private readonly authService: AuthService,
private readonly usersService: UsersService,
private readonly configService: ConfigService<EnvironmentVariables, true>,
private readonly jwtStrategy: JwtStrategy,
) {}

/**
* Endpoint for user login.
Expand All @@ -26,7 +36,15 @@ export class AuthController {
}

@Post('google-login')
googleLogin(@Body() googleLoginDto: GoogleLoginDto) {
async googleLogin(@Headers('Authorization') authorizationHeader: string, @Body() googleLoginDto: GoogleLoginDto) {
if (authorizationHeader) {
const token = authorizationHeader.split(' ')[1];
const decodeToken = jwt.verify(token, this.configService.get('JWT_SECRET'));
if (!decodeToken) throw new UnauthorizedException('Bad token');
const user = await this.jwtStrategy.validate(decodeToken);
console.log(user);
return this.usersService.updateGoogleToken(user, googleLoginDto.accessToken);
}
return this.authService.connectWithGoogle(googleLoginDto);
}
}

0 comments on commit 3355d94

Please sign in to comment.