πŸ“¦ Kimsoo0119 / map_finder_backend

πŸ“„ auth.controller.ts Β· 63 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63import { Controller, Delete, Get, Query, Res, UseGuards } from '@nestjs/common';
import { AuthService } from './auth.service';
import { User } from 'src/common/interface/common-interface';
import { Response } from 'express';
import { RefreshTokenGuard } from 'src/common/guard/refresh-token.guard';
import { GetAuthorizedUser } from 'src/common/decorator/get-user.decorator';
import { AccessTokenGuard } from 'src/common/guard/access-token.guard';

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

  @Get('/signin/kakao')
  async signInKakao(
    @Query('authorizationCode') authorizationCode: string,
    @Res({ passthrough: true }) response: Response,
  ) {
    const { email: unregisteredUserEmail, token } =
      await this.authService.signInWithKakao(authorizationCode);

    if (!unregisteredUserEmail) {
      response.cookie('refreshToken', token.refreshToken, {
        httpOnly: true,
      });

      return { accessToken: token.accessToken };
    }

    return { unregisteredUserEmail, signUpType: 'KAKAO' };
  }

  @Get('/token')
  @UseGuards(RefreshTokenGuard)
  async refreshJwtToken(
    @GetAuthorizedUser() authorizedUser: User,
    @Res({ passthrough: true }) response: Response,
  ) {
    const { accessToken, refreshToken } =
      await this.authService.generateJwtToken({
        id: authorizedUser.id,
        nickname: authorizedUser.nickname,
      });

    response.cookie('refreshToken', refreshToken, {
      httpOnly: true,
    });

    return { accessToken, msg: '토큰 μž¬λ°œκΈ‰ μ™„λ£Œ' };
  }

  @Delete('/logout')
  @UseGuards(AccessTokenGuard)
  async logOut(
    @GetAuthorizedUser() authorizedUser: User,
    @Res({ passthrough: true }) response: Response,
  ) {
    await this.authService.deleteRefreshToken(authorizedUser.id);
    response.clearCookie('refreshToken');

    return { success: true, msg: 'λ‘œκ·Έμ•„μ›ƒ μ™„λ£Œ' };
  }
}