|
1 | 1 | import { Injectable } from '@nestjs/common';
|
| 2 | +import { ConfigService } from '@nestjs/config'; |
2 | 3 | import { OAuth2Client } from 'google-auth-library';
|
| 4 | +import { JwtService } from '@nestjs/jwt'; |
3 | 5 |
|
4 | 6 | @Injectable()
|
5 | 7 | export class AuthService {
|
6 |
| - private oAuth2Client: OAuth2Client; |
| 8 | + private oAuth2Client: OAuth2Client; |
| 9 | + private clientID: string; |
| 10 | + private clientSecret: string; |
7 | 11 |
|
8 |
| - constructor() { |
9 |
| - this.oAuth2Client = new OAuth2Client('350585476549-mild3b1ggbtd57t4sduk8qlc7d8suq66.apps.googleusercontent.com', |
10 |
| - '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', 'http://localhost:3000'); |
| 12 | + constructor(private configService: ConfigService, private jwtService: JwtService) { |
| 13 | + this.clientID = configService.get<string>('GOOGLE_CLIENT_ID'); |
| 14 | + this.clientSecret = configService.get<string>('GOOGLE_CLIENT_SECRET'); |
| 15 | + this.oAuth2Client = new OAuth2Client(this.clientID, this.clientSecret, 'http://localhost:3000'); |
11 | 16 | }
|
12 | 17 |
|
13 |
| - async exchangeCodeForTokens(code: string) { |
14 |
| - try { |
15 |
| - const { tokens } = await this.oAuth2Client.getToken(code); |
16 |
| - |
17 |
| - return tokens; // Contains access_token, refresh_token, and id_token |
18 |
| - } catch (error) { |
19 |
| - |
20 |
| - } |
| 18 | + async verifyJWTToken(jwtToken: string) { |
| 19 | + const loginTicket = await this.oAuth2Client.verifyIdToken({ |
| 20 | + idToken: jwtToken, |
| 21 | + audience: this.clientID |
| 22 | + }) |
| 23 | + const payload = loginTicket.getPayload(); |
| 24 | + console.log("PAYLOAD") |
| 25 | + console.log(payload); |
| 26 | + const tokenData = { sub: payload['sub'], email: payload['email'], name: payload['name'], picture: payload['picture'] } |
| 27 | + const accessToken = await this.jwtService.signAsync(tokenData, { expiresIn: 60 }); |
| 28 | + console.log("THIS IS THE TOKEN ", accessToken); |
| 29 | + return {jwtToken: accessToken}; |
21 | 30 | }
|
22 | 31 |
|
23 | 32 | }
|
0 commit comments