-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from Brints/google_auth
auth: Add Google OAuth
- Loading branch information
Showing
27 changed files
with
3,546 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,3 +129,6 @@ dist | |
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
# documenation | ||
documentation/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
brints-estate-api/src/auth/interfaces/auth-service.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { User } from 'src/users/entities/user.entity'; | ||
|
||
/** | ||
* @description LoginUser Interface | ||
* @interface ILoginUser | ||
*/ | ||
export interface ILoginUser { | ||
user: User; | ||
tokens: { access_token: string; refresh_token: string }; | ||
} | ||
|
||
/** | ||
* @description RefreshToken Interface | ||
* @interface IRefreshToken | ||
*/ | ||
export interface IRefreshToken { | ||
refresh_token: string; | ||
} | ||
|
||
export interface IAccessTokens { | ||
access_token: string; | ||
refresh_token: string; | ||
} |
6 changes: 6 additions & 0 deletions
6
brints-estate-api/src/auth/interfaces/google-token-payload.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface IGooglePayload { | ||
email: string; | ||
sub: string; | ||
given_name: string; | ||
family_name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export class GoogleTokenDto { | ||
@IsNotEmpty() | ||
@IsString() | ||
token: string; | ||
} |
38 changes: 38 additions & 0 deletions
38
brints-estate-api/src/auth/socials/google-authentication.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
Body, | ||
ClassSerializerInterceptor, | ||
Controller, | ||
HttpStatus, | ||
Post, | ||
UseFilters, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { GoogleAuthenticationService } from './providers/google-authentication.service'; | ||
import { AuthType } from '../enum/auth-type.enum'; | ||
import { Auth } from '../decorators/auth.decorator'; | ||
import { HttpExceptionFilter } from 'src/exceptions/http-exception.filter'; | ||
import { GoogleTokenDto } from './dto/google-token.dto'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
|
||
@Controller('auth') | ||
@ApiTags('Authentication') | ||
export class GoogleAuthenticationController { | ||
constructor( | ||
private readonly googleAuthenticationService: GoogleAuthenticationService, | ||
) {} | ||
|
||
@Post('google-auth') | ||
@Auth(AuthType.None) | ||
@UseInterceptors(ClassSerializerInterceptor) | ||
@UseFilters(HttpExceptionFilter) | ||
public async authenticate(@Body() googleTokenDto: GoogleTokenDto) { | ||
const payload = | ||
await this.googleAuthenticationService.authenticate(googleTokenDto); | ||
|
||
return { | ||
message: 'Google Login Successful', | ||
status_code: HttpStatus.OK, | ||
payload, | ||
}; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
brints-estate-api/src/auth/socials/providers/google-authentication.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
forwardRef, | ||
HttpStatus, | ||
Inject, | ||
Injectable, | ||
OnModuleInit, | ||
} from '@nestjs/common'; | ||
import { ConfigType } from '@nestjs/config'; | ||
import { OAuth2Client } from 'google-auth-library'; | ||
import jwtConfig from 'src/auth/config/jwt.config'; | ||
import { GoogleTokenDto } from '../dto/google-token.dto'; | ||
import { UsersService } from 'src/users/providers/users.service'; | ||
import { CustomException } from 'src/exceptions/custom.exception'; | ||
import { GenerateTokensProvider } from 'src/auth/providers/generate-tokens.provider'; | ||
import { IGooglePayload } from 'src/auth/interfaces/google-token-payload.interface'; | ||
import { IAccessTokens } from 'src/auth/interfaces/auth-service.interface'; | ||
|
||
/** | ||
* GoogleAuthenticationService Class handles the business logic of the google authentication module. | ||
* @class GoogleAuthenticationService | ||
* @exports GoogleAuthenticationService | ||
*/ | ||
@Injectable() | ||
export class GoogleAuthenticationService implements OnModuleInit { | ||
private oAuthClient: OAuth2Client; | ||
|
||
/** | ||
* Constructor GoogleAuthenticationService | ||
*/ | ||
constructor( | ||
@Inject(forwardRef(() => UsersService)) | ||
private readonly usersService: UsersService, | ||
|
||
@Inject(jwtConfig.KEY) | ||
private readonly jwtConfiguration: ConfigType<typeof jwtConfig>, | ||
|
||
private readonly generateTokensProvider: GenerateTokensProvider, | ||
) {} | ||
|
||
/** | ||
* onModuleInit method initializes the google authentication module. | ||
*/ | ||
onModuleInit(): void { | ||
this.oAuthClient = new OAuth2Client({ | ||
clientId: this.jwtConfiguration.google_client_id, | ||
clientSecret: this.jwtConfiguration.google_client_secret, | ||
}); | ||
} | ||
|
||
/** | ||
* getOAuthClient method returns the google oAuth client. | ||
* @returns {OAuth2Client} | ||
*/ | ||
public async authenticate( | ||
googleTokenDto: GoogleTokenDto, | ||
): Promise<IAccessTokens> { | ||
const loginTicket = await this.oAuthClient.verifyIdToken({ | ||
idToken: googleTokenDto.token, | ||
}); | ||
|
||
if (!loginTicket) | ||
throw new CustomException( | ||
HttpStatus.UNAUTHORIZED, | ||
'Google login denied.', | ||
); | ||
|
||
const { | ||
email, | ||
sub: google_id, | ||
given_name: first_name, | ||
family_name: last_name, | ||
} = loginTicket.getPayload() as IGooglePayload; | ||
|
||
const user = await this.usersService.findOneByGoogleId(google_id); | ||
|
||
if (user) { | ||
return this.generateTokensProvider.generateTokens(user); | ||
} | ||
|
||
const newUser = await this.usersService.createGoogleUser({ | ||
email, | ||
first_name, | ||
last_name, | ||
google_id, | ||
}); | ||
|
||
return this.generateTokensProvider.generateTokens(newUser); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export enum UserGender { | ||
MALE = 'male', | ||
FEMALE = 'female', | ||
BINARY = 'binary', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,13 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
import { CreateUserDto } from '../../auth/dto/create-user.dto'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsOptional, IsString } from 'class-validator'; | ||
import { PartialType, PickType } from '@nestjs/swagger'; | ||
|
||
export class UpdateUserDto extends PartialType(CreateUserDto) { | ||
@ApiProperty({ | ||
description: 'The id of the user', | ||
example: '60b7f3a8d8e9a7e4d8f9b1a7', | ||
}) | ||
@IsNotEmpty() | ||
@IsString() | ||
id: string; | ||
|
||
@ApiProperty({ | ||
description: "The user's country code", | ||
example: '+234', | ||
}) | ||
@IsOptional() | ||
@IsString() | ||
country_code?: string; | ||
} | ||
export class UpdateUserDto extends PartialType( | ||
PickType(CreateUserDto, [ | ||
'first_name', | ||
'last_name', | ||
'country_code', | ||
'phone_number', | ||
'gender', | ||
'marketing', | ||
]), | ||
) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
brints-estate-api/src/users/interface/google-user.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface GoogleUser { | ||
email: string; | ||
first_name: string; | ||
last_name: string; | ||
google_id: string; | ||
} |
Oops, something went wrong.