Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: login using google #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/api/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ DATABASE_URL = "postgres://postgres:postgres@localhost:5432/disworse"
POSTGRES_DB = 'disworse'
POSTGRES_USER = 'postgres'
POSTGRES_PASSWORD = 'postgres'
GOOGLE_CLIENT_ID = ''
GOOGLE_CLIENT_SECRET = ''
GOOGLE_CALLBACK_URL = 'http://localhost:3333/api/auth/google/redirect'
6 changes: 5 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0",
"@nestjs/graphql": "^12.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@types/passport-google-oauth20": "^2.0.16",
"argon2": "^0.41.1",
"nestjs-zod": "^3.0.0",
"drizzle-orm": "^0.33.0",
"graphql": "^16.9.0",
"nestjs-zod": "^3.0.0",
"passport": "^0.7.0",
"passport-google-oauth20": "^2.0.0",
"pg": "^8.13.0",
"postgres": "^3.4.4",
"reflect-metadata": "^0.2.0",
Expand Down
16 changes: 14 additions & 2 deletions apps/api/src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { Body, Controller, Post } from "@nestjs/common";
import { Body, Controller, Post, Get, Req, UseGuards } from "@nestjs/common";
import { SerializedUser } from "../../common/serialized-types/user";
import { AuthService } from "./auth.service";
import { SignupDto } from "./dto/signup.dto";
import { AuthGuard } from '@nestjs/passport';

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

@Post("signup")
async signup(@Body() signupDto: SignupDto) {
const user = await this.authService.signup(signupDto);

return new SerializedUser(user);
}

@Get('google')
@UseGuards(AuthGuard('google'))
async googleAuth(@Req() req: Request) {
}

@Get('google/redirect')
@UseGuards(AuthGuard('google'))
googleAuthRedirect(@Req() req: Request) {
return this.authService.googleLogin(req);
}
}
3 changes: 2 additions & 1 deletion apps/api/src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from "@nestjs/common";
import { AuthController } from "./auth.controller";
import { AuthService } from "./auth.service";
import { GoogleStrategy } from './google.strategy';

@Module({
providers: [AuthService],
providers: [AuthService, GoogleStrategy],
controllers: [AuthController],
})
export class AuthModule {}
13 changes: 11 additions & 2 deletions apps/api/src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SignupDto } from "./dto/signup.dto";
export class AuthService {
private readonly logger = new Logger(AuthService.name);

constructor() {}
constructor() { }

async signup(signupDto: SignupDto) {
const user = signupDto; // TODO: fake signup till we have user model
Expand All @@ -24,5 +24,14 @@ export class AuthService {
);
return user;
}

googleLogin(req: any) {
if (!req.user) {
return 'No user from google'
}
return {
message: 'User information from Google',
user: req.user,
}
}
}
1;
32 changes: 32 additions & 0 deletions apps/api/src/modules/auth/google.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, VerifyCallback } from 'passport-google-oauth20';

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
constructor() {
super({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK_URL,
scope: ['email', 'profile'],
});
}

async validate(
accessToken: string,
refreshToken: string,
profile: any,
done: VerifyCallback,
): Promise<any> {
const { name, emails, photos } = profile;
const user = {
email: emails[0].value,
firstName: name.givenName,
lastName: name.familyName,
picture: photos[0].value,
accessToken,
};
done(null, user);
}
}
Loading