Skip to content

Commit

Permalink
feat(auth): 내 정보조회 api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
IsthisLee committed Aug 14, 2023
1 parent ce74980 commit c6ecc5e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
53 changes: 49 additions & 4 deletions src/apis/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Controller, Param, ParseEnumPipe, Body, Res, Post, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger';
import { Controller, Param, ParseEnumPipe, Body, Res, Post, UseGuards, Get } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiParam, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { Provider } from '@prisma/client';
import { Response } from 'express';
import { LoginDto } from './dtos/login.dto';
import { AuthGuard } from '@nestjs/passport';
import { JwtUserPayload } from 'src/common/decorators/jwt-user.decorator';
import { JwtPayloadInfo } from 'src/common/types';
import { JwtPayloadInfo, GetUserInfo } from 'src/common/types';
import { UsersService } from '../users/users.service';

@Controller('auth')
@ApiTags('Auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
constructor(private readonly authService: AuthService, private readonly usersService: UsersService) {}

@Post('/login/:login_type')
@ApiOperation({
Expand Down Expand Up @@ -54,6 +55,50 @@ export class AuthController {
return { accessToken };
}

@Get('/me')
@UseGuards(AuthGuard('jwt-access'))
@ApiBearerAuth('access-token')
@ApiOperation({
summary: '내 정보 조회 API',
description: `
로그인 상태인 경우 내 정보를 조회합니다.
로그인 상태가 아닌 경우 401 에러가 발생합니다.`,
})
@ApiResponse({
status: 200,
description: '내 정보 조회 성공',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
userId: {
type: 'number',
},
email: {
type: 'string',
},
name: {
type: 'string',
},
profileImageUrl: {
type: 'string',
},
},
},
},
},
})
async getMyInfo(@JwtUserPayload() jwtUser: JwtPayloadInfo): Promise<GetUserInfo> {
const user = await this.usersService.findOneById(jwtUser.userId);
return {
userId: user.id,
email: user.email,
name: user.name,
profileImageUrl: user.profileImage,
};
}

@Post('/silent-refresh')
@UseGuards(AuthGuard('jwt-refresh'))
@ApiOperation({
Expand Down
7 changes: 7 additions & 0 deletions src/common/types/auth.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ export enum AuthGuardType {
ACCESS = 'access',
REFRESH = 'refresh',
}

export interface GetUserInfo {
userId: number;
email: string;
name: string;
profileImageUrl: string;
}

0 comments on commit c6ecc5e

Please sign in to comment.