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

auth.service.ts 파일 변경사항 체크 #93

Open
9utty opened this issue Mar 21, 2023 · 1 comment
Open

auth.service.ts 파일 변경사항 체크 #93

9utty opened this issue Mar 21, 2023 · 1 comment

Comments

@9utty
Copy link
Member

9utty commented Mar 21, 2023

// 기존 파일 129번줄부터
  private getUserEmailFromGoogleUser(request: Request): string {
    const { user } = request as any // 기존 Request 구조에 email 이 없어서 any 로 받아야합니다.
    return user.email
  }

  private async getUserFromGoogleUser(request: Request): Promise<User> {
    const userName = this.getUserEmailFromGoogleUser(request)
    const userFound: User = await this.userRepository.findOne({
      where: {
        userName: userName,
      },
    })
    if (userFound === undefined || userFound === null) {
      throw new UnauthorizedException('invalid token')
    }
    return userFound
  }
// 변경 후
  private getUserEmailFromGoogleUser(request: Request): string {
    const { user } = request as any // 기존 Request 구조에 email 이 없어서 any 로 받아야합니다.
    return user.email
  }

  private async getUserFromGoogleUser(request: Request): Promise<User> {
    const userName = this.getUserEmailFromGoogleUser(request)
    const userFound: User = await this.userRepository.findOne({
      where: {
        userName: userName,
      },
    })
    if (userFound === undefined || userFound === null) {
      return null // 이부분 수정
    }
    return userFound
  }
// 변경 전 202번줄
  async rotateTokens(request, body: any): Promise<TokenDto> {
    const accessToken: string = this.getAccessTokenFromRequest(request)
    const refreshToken: string = body.refreshToken
    let userId: number
    let exp: number
    // check token is valid
    try {
      const accessTokenPayload = this.jwtService.verify(accessToken, {
        ignoreExpiration: true,
        secret: this.configService.get('JWT_SECRET'),
      })
      const refreshTokenPayload = this.jwtService.verify(refreshToken, {
        secret: this.configService.get('JWT_SECRET'),
      })
      userId = accessTokenPayload.userId
      exp = refreshTokenPayload.exp
    } catch (err) {
      throw new UnauthorizedException('token verify failed')
    }
    await this.checkValidTokenInSession(userId, accessToken, refreshToken)
    // set new token
    const newAccessToken: string = this.getNewAccessToken(userId)
    const newRefreshToken: string = this.rotateToken(refreshToken, exp, userId)
    // update session and send it
    await this.updateSession(userId, newAccessToken, newRefreshToken)
    return {
      accessToken: newAccessToken,
      refreshToken: newRefreshToken,
    }
  }
}
// 변경 후 
async rotateTokens(request, body: any): Promise<TokenDto> {
    const accessToken: string = body.accessToken // 이부분 수정
    const refreshToken: string = body.refreshToken
    let userId: number
    let exp: number
    // check token is valid
    try {
      const accessTokenPayload = this.jwtService.verify(accessToken, {
        ignoreExpiration: true,
        secret: this.configService.get('JWT_SECRET'),
      })
      const refreshTokenPayload = this.jwtService.verify(refreshToken, {
        secret: this.configService.get('JWT_SECRET'),
      })
      userId = accessTokenPayload.userId
      exp = refreshTokenPayload.exp
    } catch (err) {
      throw new UnauthorizedException('token verify failed')
    }
    await this.checkValidTokenInSession(userId, accessToken, refreshToken)
    // set new token
    const newAccessToken: string = this.getNewAccessToken(userId)
    const newRefreshToken: string = this.rotateToken(refreshToken, exp, userId)
    // update session and send it
    await this.updateSession(userId, newAccessToken, newRefreshToken)
    return {
      accessToken: newAccessToken,
      refreshToken: newRefreshToken,
    }
  }
@9utty
Copy link
Member Author

9utty commented Mar 21, 2023

@koreanddinghwan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant