Skip to content

Commit

Permalink
feat: Add auth controller, google auth #73
Browse files Browse the repository at this point in the history
  • Loading branch information
bit172 committed Nov 22, 2020
1 parent a8aebfa commit d83c8df
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 5 deletions.
18 changes: 18 additions & 0 deletions apps/mull-api/src/app/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthController } from './auth.controller';

describe('Auth Controller', () => {
let controller: AuthController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
}).compile();

controller = module.get<AuthController>(AuthController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
21 changes: 21 additions & 0 deletions apps/mull-api/src/app/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Controller, Get, Req, UseGuards } from '@nestjs/common';
import { AuthService } from './auth.service';
import { Request } from 'express';
import { AuthGuard } from '@nestjs/passport';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Get('google')
@UseGuards(AuthGuard('google'))
async googleAuth() {
// noop
}

@Get('google/redirect')
@UseGuards(AuthGuard('google'))
googleAuthRedirect(@Req() req: Request) {
return this.authService.googleLogin(req);
}
}
5 changes: 4 additions & 1 deletion apps/mull-api/src/app/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { UserModule } from '../user';
import { PassportModule } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { LocalStrategy } from './local.strategy';
import { AuthController } from './auth.controller';
import { GoogleStrategy } from './google.strategy';

@Module({
imports: [UserModule, PassportModule],
providers: [AuthService, LocalStrategy],
providers: [AuthService, LocalStrategy, GoogleStrategy],
exports: [AuthService],
controllers: [AuthController],
})
export class AuthModule {}
7 changes: 6 additions & 1 deletion apps/mull-api/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Injectable } from '@nestjs/common';
import { User } from '../entities';
import { UserService } from '../user/user.service';

import { Request } from 'express';
@Injectable()
export class AuthService {
constructor(private userService: UserService) {}

googleLogin(req: Request) {
if (!req.user) return null;
return { user: req.user };
}

async validateUser(email: string, pass: string): Promise<Partial<User>> {
const user = await this.userService.findByEmail(email)[0];
if (user && user.password === pass) {
Expand Down
32 changes: 32 additions & 0 deletions apps/mull-api/src/app/auth/google.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PassportStrategy } from '@nestjs/passport';
import { Profile, Strategy, VerifyCallback } from 'passport-google-oauth20';
import { environment } from '../../environments/environment';
import { Injectable } from '@nestjs/common';

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
constructor() {
super({
clientID: environment.auth.google.clientId,
clientSecret: environment.auth.google.clientSecret,
callbackURL: 'http://localhost:3333/api/auth/google/redirect',
scope: ['email', 'profile'],
});
}

async validate(
accessToken: string,
refreshToken: string,
profile: Profile,
done: VerifyCallback
): Promise<void> {
const { name, emails } = profile;
const user = {
email: emails[0].value,
firstName: name.givenName,
lastName: name.familyName,
accessToken,
};
done(null, user);
}
}
45 changes: 42 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"mysql": "^2.18.1",
"passport": "^0.4.1",
"passport-facebook": "^3.0.0",
"passport-google-oauth20": "^2.0.0",
"passport-local": "^1.0.0",
"passport-twitter": "^1.0.4",
"react": "16.13.1",
Expand Down Expand Up @@ -90,10 +91,12 @@
"@testing-library/react": "10.4.1",
"@testing-library/user-event": "^12.2.2",
"@types/bcrypt": "^3.0.0",
"@types/express": "^4.17.9",
"@types/jest": "26.0.8",
"@types/lodash": "^4.14.165",
"@types/node": "~8.9.4",
"@types/passport-facebook": "^2.1.10",
"@types/passport-google-oauth20": "^2.0.4",
"@types/passport-local": "^1.0.33",
"@types/passport-twitter": "^1.0.36",
"@types/react": "16.9.38",
Expand Down

0 comments on commit d83c8df

Please sign in to comment.