mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 12:33:22 +00:00
resend email in progress
This commit is contained in:
@@ -11,13 +11,16 @@ import {
|
||||
BadRequestException,
|
||||
Res,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common'
|
||||
import { AuthService } from './auth.service'
|
||||
import { RegistrationDto } from './dto/registration.dto'
|
||||
import { LocalAuthGuard } from './guards/local-auth.guard'
|
||||
import { UsersService } from '../users/services/users.service'
|
||||
import { JwtAuthGuard } from './guards/jwt-auth.guard'
|
||||
|
||||
import { Response as ExpressResponse } from 'express'
|
||||
import { JwtRefreshGuard } from './guards/jwt-refresh.guard'
|
||||
import { Cookies } from '../../infrastructure/decorators/cookie.decorator'
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(
|
||||
@@ -28,7 +31,7 @@ export class AuthController {
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('me')
|
||||
async getUserData(@Request() req) {
|
||||
const userId = req.user.userId
|
||||
const userId = req.user.id
|
||||
const user = await this.usersService.getUserById(userId)
|
||||
|
||||
if (!user) throw new UnauthorizedException()
|
||||
@@ -42,12 +45,13 @@ export class AuthController {
|
||||
|
||||
@HttpCode(200)
|
||||
@UseGuards(LocalAuthGuard)
|
||||
@Post('sign-in')
|
||||
async login(@Request() req, @Res({ passthrough: true }) res) {
|
||||
@Post('login')
|
||||
async login(@Request() req, @Res({ passthrough: true }) res: ExpressResponse) {
|
||||
const userData = req.user.data
|
||||
res.cookie('refreshToken', userData.refreshToken, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
// secure: true,
|
||||
path: '/v1/auth/refresh-token',
|
||||
})
|
||||
return { accessToken: req.user.data.accessToken }
|
||||
}
|
||||
@@ -62,7 +66,8 @@ export class AuthController {
|
||||
)
|
||||
}
|
||||
|
||||
@Post('registration-confirmation')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post('verify-email')
|
||||
async confirmRegistration(@Body('code') confirmationCode) {
|
||||
const result = await this.authService.confirmEmail(confirmationCode)
|
||||
if (!result) {
|
||||
@@ -71,12 +76,12 @@ export class AuthController {
|
||||
return null
|
||||
}
|
||||
|
||||
@Post('registration-email-resending')
|
||||
async resendRegistrationEmail(@Body('email') email: string) {
|
||||
const isResented = await this.authService.resendCode(email)
|
||||
if (!isResented)
|
||||
@Post('resend-verification-email')
|
||||
async resendRegistrationEmail(@Body('userId') userId: string) {
|
||||
const isResent = await this.authService.resendCode(userId)
|
||||
if (!isResent)
|
||||
throw new BadRequestException({
|
||||
message: 'email already confirmed or such email not found',
|
||||
message: 'Email already confirmed or such email was not found',
|
||||
field: 'email',
|
||||
})
|
||||
return null
|
||||
@@ -84,24 +89,31 @@ export class AuthController {
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('logout')
|
||||
async logout(@Request() req) {
|
||||
if (!req.cookie?.refreshToken) throw new UnauthorizedException()
|
||||
await this.usersService.addRevokedToken(req.cookie.refreshToken)
|
||||
async logout(
|
||||
@Cookies('refreshToken') refreshToken: string,
|
||||
@Res({ passthrough: true }) res: ExpressResponse
|
||||
) {
|
||||
if (!refreshToken) throw new UnauthorizedException()
|
||||
await this.usersService.addRevokedToken(refreshToken)
|
||||
res.clearCookie('refreshToken')
|
||||
return null
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('refresh-token')
|
||||
async refreshToken(@Request() req, @Response() res) {
|
||||
if (!req.cookie?.refreshToken) throw new UnauthorizedException()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@UseGuards(JwtRefreshGuard)
|
||||
@Get('refresh-token')
|
||||
async refreshToken(@Request() req, @Response({ passthrough: true }) res: ExpressResponse) {
|
||||
if (!req.cookies?.refreshToken) throw new UnauthorizedException()
|
||||
const userId = req.user.id
|
||||
const newTokens = await this.authService.createJwtTokensPair(userId)
|
||||
res.cookie('refreshToken', newTokens.refreshToken, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
path: '/refresh',
|
||||
// secure: true,
|
||||
path: '/v1/auth/refresh-token',
|
||||
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
|
||||
})
|
||||
return { accessToken: newTokens.accessToken }
|
||||
return {
|
||||
accessToken: newTokens.accessToken,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user