import { Controller , Post , Body , Param , Get , Response } from '@nestjs/common' ; import { AuthService } from './auth.service' ; import { LoginUserDto } from '../users/dto/login-user.dto' ; import { ApiUseTags , ApiResponse } from '@nestjs/swagger' ; import { CreateUserDto } from '../users/dto/create-user.dto' ; import * as express from 'express' ; @ ApiUseTags ( 'auth' ) @ Controller ( 'auth' ) export class AuthController { constructor ( private authService ) { } @ ApiResponse ( { status : 201 , description : 'Successfully created user' } ) @ ApiResponse ( { status : 400 , description : 'Bad request' } ) @ Post ( '/email/register' ) async createUser ( @ Body ( ) user ) { const response = await this . authService . create ( user ) ; const newUser = response . userResponse ; await this . authService . createEmailToken ( newUser . email ) ; const state = await this . authService . sendEmailVerification ( newUser . email ) ; return { ... response , ... state , } ; } @ ApiResponse ( { status : 200 , description : 'Successfully logged in' } ) @ ApiResponse ( { status : 401 , description : 'Wrong credentials' } ) @ Post ( '/email/login' ) async login ( @ Body ( ) loginUserDto ) { return await this . authService . validateUserByPassword ( loginUserDto ) ; } @ ApiResponse ( { status : 200 , description : 'Successfully send verification code' , } ) @ ApiResponse ( { status : 403 , description : 'User not found' } ) @ Get ( 'email/resend-verification/:email' ) async sendEmailVerification ( @ Param ( 'email' ) email ) { await this . authService . createEmailToken ( email ) ; return await this . authService . sendEmailVerification ( email ) ; } @ ApiResponse ( { status : 200 , description : 'Successfully verified email' } ) @ ApiResponse ( { status : 403 , description : 'Invalid token' } ) @ Get ( 'email/verify/:token' ) async verifyEmail ( @ Param ( 'token' ) token , @ Response ( ) response ) { const verified = await this . authService . verifyEmail ( token ) ; if ( verified ) { response . redirect ( 'http://localhost:8080' ) ; } } }