Skip to content

Commit

Permalink
feat: 구글 로그인 url API추가 #81
Browse files Browse the repository at this point in the history
  • Loading branch information
Miensoap committed Nov 9, 2024
1 parent 7407bdf commit 74987f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 6 additions & 1 deletion backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Post } from '@nestjs/common';
import { Body, Controller, Post, Get } from '@nestjs/common';
import { AuthService } from './auth.service';
import { UserService } from '../user/user.service';
import { CreateUserRequest } from '../user/dto/CreateUserRequest';
Expand All @@ -10,6 +10,11 @@ export class AuthController {
private readonly userService: UserService,
) {}

@Get('google/signIn')
async googleSignIn() {
return this.authService.getGoogleAuthUrl();
}

@Post('google/signIn')
async googleCallback(@Body('code') code: string) {
const tokens = await this.authService.getGoogleToken(code);
Expand Down
13 changes: 9 additions & 4 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as jwt from 'jsonwebtoken';
import { googleTokenResponse, googleUserResponse } from './authType';
import { googleTokenResponse, googleUserResponse } from './auth.type';

@Injectable()
export class AuthService {
Expand All @@ -19,9 +19,14 @@ export class AuthService {
this.jwtExpiration = this.configService.get<string>('JWT_EXPIRATION');
}

async getGoogleToken(
code: string,
): Promise<{ accessToken: string; refreshToken: string }> {
getGoogleAuthUrl() {
const url = new URL('https://accounts.google.com/o/oauth2/v2/auth');
url.searchParams.append('client_id', this.clientId);
url.searchParams.append('redirect_uri', this.redirectUri);
url.searchParams.append('response_type', 'code');
url.searchParams.append('scope', 'openid profile');
return url.toString();
}
return fetch('https://oauth2.googleapis.com/token', {
method: 'POST',
headers: {
Expand Down

0 comments on commit 74987f5

Please sign in to comment.