-
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 #21 from mathiasberggren/feature/auth-passport
[Backend] Implement Google OAuth
- Loading branch information
Showing
39 changed files
with
976 additions
and
20 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
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 was deleted.
Oops, something went wrong.
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,4 @@ | ||
DATABASE_URL="postgresql://test:test@localhost:5433/tests" | ||
|
||
OAUTH_GOOGLE_CLIENT_ID="google-client-id" | ||
OAUTH_GOOGLE_CLIENT_SECRET="google-client-secret" |
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
File renamed without changes.
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,46 @@ | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
import { TestBed } from '@automock/jest' | ||
import { createMock } from '@golevelup/ts-jest' | ||
import { Request, Response } from 'express' | ||
|
||
import { AuthController } from './auth.controller' | ||
import { AuthService } from './auth.service' | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController | ||
let authService: jest.Mocked<AuthService> | ||
|
||
beforeEach(async () => { | ||
const { unit, unitRef } = TestBed.create(AuthController).compile() | ||
|
||
controller = unit | ||
authService = unitRef.get(AuthService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined() | ||
}) | ||
|
||
it('should set cookies on google login callback', async () => { | ||
const req = createMock<Request>({ | ||
user: { | ||
email: 'test-email', | ||
name: 'test-name', | ||
picture: 'test-picture' | ||
} | ||
}) | ||
|
||
const res = createMock<Response>() | ||
jest.spyOn(authService, 'signIn').mockResolvedValue('token') | ||
|
||
await controller.googleLoginCallback(req, res) | ||
|
||
expect(authService.signIn).toHaveBeenCalledWith({ | ||
email: 'test-email', | ||
name: 'test-name', | ||
picture: 'test-picture' | ||
}) | ||
|
||
expect(res.cookie).toHaveBeenCalledWith('access_token', 'token') | ||
}) | ||
}) |
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,25 @@ | ||
import { Controller, Req, Get, UseGuards, Res } from '@nestjs/common' | ||
import { Request, Response } from 'express' | ||
|
||
import { GoogleOauthGuard } from './guards/google.guard' | ||
import { AuthService } from './auth.service' | ||
import { Profile } from './interfaces/profile' | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor (private readonly authService: AuthService) {} | ||
@Get('google') | ||
@UseGuards(GoogleOauthGuard) | ||
async googleLogin () { | ||
} | ||
|
||
@Get('google/callback') | ||
@UseGuards(GoogleOauthGuard) | ||
async googleLoginCallback (@Req() req: Request, @Res({ passthrough: true }) res: Response) { | ||
const token = await this.authService.signIn(req.user as Profile) | ||
|
||
res.cookie('access_token', token) | ||
|
||
return null | ||
} | ||
} |
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,28 @@ | ||
import { Module } from '@nestjs/common' | ||
import { PassportModule } from '@nestjs/passport' | ||
import { JwtModule } from '@nestjs/jwt' | ||
import { ConfigService } from '@nestjs/config' | ||
|
||
import { UsersModule } from '../users/users.module' | ||
|
||
import { AuthService } from './auth.service' | ||
import { AuthController } from './auth.controller' | ||
import { GoogleStrategy } from './strategies/google.strategy' | ||
|
||
@Module({ | ||
imports: [ | ||
UsersModule, | ||
PassportModule, | ||
JwtModule.registerAsync({ | ||
useFactory: (config: ConfigService) => ({ | ||
global: true, | ||
secret: config.get<string>('JWT_SECRET'), | ||
signOptions: { expiresIn: config.get<string>('JWT_EXPIRES_IN') } | ||
}), | ||
inject: [ConfigService] | ||
}) | ||
], | ||
providers: [AuthService, GoogleStrategy], | ||
controllers: [AuthController] | ||
}) | ||
export class AuthModule {} |
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,60 @@ | ||
import { TestBed } from '@automock/jest' | ||
import { JwtService } from '@nestjs/jwt' | ||
|
||
import { UsersService } from '../users/users.service' | ||
|
||
import { AuthService } from './auth.service' | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService | ||
|
||
beforeEach(async () => { | ||
const { unit } = TestBed.create(AuthService) | ||
.mock(UsersService) | ||
.using({ | ||
findOrCreate: jest.fn().mockResolvedValue({ id: 1, email: '[email protected]' }) | ||
}) | ||
.mock(JwtService) | ||
.using({ | ||
sign: jest.fn().mockReturnValue('jwt') | ||
}) | ||
.compile() | ||
|
||
service = unit | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
|
||
it('should return a user and token', async () => { | ||
const user = await service.signIn({ | ||
email: 'test', | ||
name: 'test', | ||
picture: 'test' | ||
}) | ||
expect(user).toEqual('jwt') | ||
}) | ||
|
||
it('should throw an error if no profile is provided', async () => { | ||
try { | ||
// @ts-expect-error: Testing OAuth provider does not return profile | ||
await service.signIn(null) | ||
} catch (e) { | ||
expect(e.message).toEqual('Unauthenticated') | ||
} | ||
}) | ||
|
||
it('should throw an error if no email is provided', async () => { | ||
try { | ||
await service.signIn({ | ||
// @ts-expect-error: Testing when OAuth provider does not return email | ||
email: null, | ||
name: 'test', | ||
picture: 'test' | ||
}) | ||
} catch (e) { | ||
expect(e.message).toEqual('Email not found') | ||
} | ||
}) | ||
}) |
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 { BadRequestException, Injectable } from '@nestjs/common' | ||
import { JwtService } from '@nestjs/jwt' | ||
|
||
import { UsersService } from '../users/users.service' | ||
|
||
import { Profile } from './interfaces/profile' | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor (private readonly usersService: UsersService, private readonly jwtService: JwtService) { | ||
} | ||
|
||
generateJwt (payload: object) { | ||
return this.jwtService.sign(payload) | ||
} | ||
|
||
// TODO: Implement ProfileDTO for improved validation | ||
async signIn (profile: Profile) { | ||
if (!profile) { | ||
throw new BadRequestException('Unauthenticated') | ||
} | ||
|
||
if (!profile.email) { | ||
throw new BadRequestException('Email not found') | ||
} | ||
|
||
const user = await this.usersService.findOrCreate({ | ||
email: profile.email, | ||
name: profile.name, | ||
picture: profile.picture | ||
}) | ||
|
||
return this.generateJwt({ | ||
sub: user.id, | ||
email: user.email | ||
}) | ||
} | ||
} |
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,5 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { AuthGuard } from '@nestjs/passport' | ||
|
||
@Injectable() | ||
export class GoogleOauthGuard extends AuthGuard('google') {} |
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,5 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { AuthGuard } from '@nestjs/passport' | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') {} |
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,4 @@ | ||
export interface JwtPayload { | ||
sub: number | ||
email: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface Profile { | ||
name: string | ||
email: string | ||
picture?: string | ||
provider?: string | ||
} |
Oops, something went wrong.