refreshToken
This commit is contained in:
parent
8b20f7019e
commit
b9f18ccc5d
@ -3,6 +3,7 @@ import { AuthService } from './auth.service'
|
||||
import { CreateUserDto } from 'src/users/dto/create-user.dto'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { LoginInputDto } from './dto/login-input.dto'
|
||||
import { RefreshToken } from './dto/token.dto'
|
||||
|
||||
@ApiTags('Auth')
|
||||
@Controller('api/auth')
|
||||
@ -18,4 +19,9 @@ export class AuthController {
|
||||
async login(@Body() user: LoginInputDto) {
|
||||
return this.authService.login(user.email, user.password)
|
||||
}
|
||||
|
||||
@Post('token')
|
||||
async refreshToken(@Body() payload: RefreshToken) {
|
||||
return this.authService.refreshToken(payload.refreshToken)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
import { Injectable, BadRequestException } from '@nestjs/common'
|
||||
import {
|
||||
Injectable,
|
||||
BadRequestException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common'
|
||||
import { PasswordService } from 'src/users/password.service'
|
||||
import { Token, TokenPayload } from './dto/token.dto'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
@ -34,6 +38,21 @@ export class AuthService {
|
||||
return this.generateTokens({ userId: user.id })
|
||||
}
|
||||
|
||||
async refreshToken(token: string) {
|
||||
try {
|
||||
const { userId } = this.jwtService.verify(token, {
|
||||
secret: this.configService.get<string>(
|
||||
'JWT_REFRESH_SECRET',
|
||||
'JWT_REFRESH_SECRET',
|
||||
),
|
||||
})
|
||||
return this.generateTokens({ userId })
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
throw new UnauthorizedException(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
private generateTokens(payload: TokenPayload): Token {
|
||||
const accessToken = this.jwtService.sign(payload, {
|
||||
secret: this.configService.get<string>(
|
||||
|
@ -1,5 +1,6 @@
|
||||
import '@nestjs/mapped-types'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class Token {
|
||||
@ApiProperty()
|
||||
@ -8,6 +9,11 @@ export class Token {
|
||||
refreshToken: string
|
||||
}
|
||||
|
||||
export class RefreshToken {
|
||||
@IsString()
|
||||
refreshToken: string
|
||||
}
|
||||
|
||||
export class TokenPayload {
|
||||
userId: string
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user