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 { CreateUserDto } from 'src/users/dto/create-user.dto'
|
||||||
import { ApiTags } from '@nestjs/swagger'
|
import { ApiTags } from '@nestjs/swagger'
|
||||||
import { LoginInputDto } from './dto/login-input.dto'
|
import { LoginInputDto } from './dto/login-input.dto'
|
||||||
|
import { RefreshToken } from './dto/token.dto'
|
||||||
|
|
||||||
@ApiTags('Auth')
|
@ApiTags('Auth')
|
||||||
@Controller('api/auth')
|
@Controller('api/auth')
|
||||||
@ -18,4 +19,9 @@ export class AuthController {
|
|||||||
async login(@Body() user: LoginInputDto) {
|
async login(@Body() user: LoginInputDto) {
|
||||||
return this.authService.login(user.email, user.password)
|
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 { PasswordService } from 'src/users/password.service'
|
||||||
import { Token, TokenPayload } from './dto/token.dto'
|
import { Token, TokenPayload } from './dto/token.dto'
|
||||||
import { JwtService } from '@nestjs/jwt'
|
import { JwtService } from '@nestjs/jwt'
|
||||||
@ -34,6 +38,21 @@ export class AuthService {
|
|||||||
return this.generateTokens({ userId: user.id })
|
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 {
|
private generateTokens(payload: TokenPayload): Token {
|
||||||
const accessToken = this.jwtService.sign(payload, {
|
const accessToken = this.jwtService.sign(payload, {
|
||||||
secret: this.configService.get<string>(
|
secret: this.configService.get<string>(
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import '@nestjs/mapped-types'
|
import '@nestjs/mapped-types'
|
||||||
import { ApiProperty } from '@nestjs/swagger'
|
import { ApiProperty } from '@nestjs/swagger'
|
||||||
|
import { IsString } from 'class-validator'
|
||||||
|
|
||||||
export class Token {
|
export class Token {
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@ -8,6 +9,11 @@ export class Token {
|
|||||||
refreshToken: string
|
refreshToken: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class RefreshToken {
|
||||||
|
@IsString()
|
||||||
|
refreshToken: string
|
||||||
|
}
|
||||||
|
|
||||||
export class TokenPayload {
|
export class TokenPayload {
|
||||||
userId: string
|
userId: string
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user