create use cases for auth

This commit is contained in:
andres
2023-06-15 23:10:07 +02:00
parent 612b2326f9
commit 131fec67de
20 changed files with 315 additions and 239 deletions

View File

@@ -0,0 +1,46 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { UsersRepository } from '../../users/infrastructure/users.repository'
import { CreateUserInput, UserViewType } from '../../../types/types'
import { addHours } from 'date-fns'
import { v4 as uuidv4 } from 'uuid'
import { UsersService } from '../../users/services/users.service'
export class CreateUserCommand {
constructor(public readonly user: { name: string; password: string; email: string }) {}
}
@CommandHandler(CreateUserCommand)
export class CreateUserHandler implements ICommandHandler<CreateUserCommand> {
constructor(
private readonly usersRepository: UsersRepository,
private readonly usersService: UsersService
) {}
async execute(command: CreateUserCommand): Promise<UserViewType | null> {
const { name, password, email } = command.user
const passwordHash = await this.usersService.generateHash(password)
const verificationToken = uuidv4()
const newUser: CreateUserInput = {
name: name || email.split('@')[0],
email: email,
password: passwordHash,
verificationToken,
verificationTokenExpiry: addHours(new Date(), 24),
isEmailVerified: false,
}
const createdUser = await this.usersRepository.createUser(newUser)
if (!createdUser) {
return null
}
await this.usersService.sendConfirmationEmail({
email: createdUser.email,
name: createdUser.name,
verificationToken: verificationToken,
})
return {
id: createdUser.id,
name: createdUser.name,
email: createdUser.email,
}
}
}

View File

@@ -0,0 +1,22 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { UserViewType } from '../../../types/types'
import { UnauthorizedException } from '@nestjs/common'
import { pick } from 'remeda'
import { UsersRepository } from '../../users/infrastructure/users.repository'
export class GetCurrentUserDataCommand {
constructor(public readonly userId: string) {}
}
@CommandHandler(GetCurrentUserDataCommand)
export class GetCurrentUserDataHandler implements ICommandHandler<GetCurrentUserDataCommand> {
constructor(private readonly usersRepository: UsersRepository) {}
async execute(command: GetCurrentUserDataCommand): Promise<UserViewType | null> {
const user = await this.usersRepository.findUserById(command.userId)
if (!user) throw new UnauthorizedException()
return pick(user, ['email', 'name', 'id', 'isEmailVerified'])
}
}

View File

@@ -0,0 +1,6 @@
export * from './create-user-use-case'
export * from './get-current-user-data-use-case'
export * from './logout-use-case'
export * from './resend-verification-email-use-case'
export * from './refresh-token-use-case'
export * from './verify-email-use-case'

View File

@@ -0,0 +1,30 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { Logger } from '@nestjs/common'
import { UsersRepository } from '../../users/infrastructure/users.repository'
import jwt from 'jsonwebtoken'
export class LogoutCommand {
constructor(public readonly refreshToken: string) {}
}
@CommandHandler(LogoutCommand)
export class LogoutHandler implements ICommandHandler<LogoutCommand> {
constructor(private readonly usersRepository: UsersRepository) {}
private readonly logger = new Logger(LogoutHandler.name)
async execute(command: LogoutCommand) {
const token = command.refreshToken
const secretKey = process.env.JWT_SECRET_KEY
if (!secretKey) throw new Error('JWT_SECRET_KEY is not defined')
try {
const decoded: any = jwt.verify(token, secretKey)
return this.usersRepository.revokeToken(decoded.userId, token)
} catch (e) {
this.logger.log(`Decoding error: ${e}`)
return null
}
}
}

View File

@@ -0,0 +1,35 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { AuthRepository } from '../infrastructure/auth.repository'
import jwt from 'jsonwebtoken'
import { addDays } from 'date-fns'
export class RefreshTokenCommand {
constructor(public readonly userId: string) {}
}
@CommandHandler(RefreshTokenCommand)
export class RefreshTokenHandler implements ICommandHandler<RefreshTokenCommand> {
constructor(private readonly authRepository: AuthRepository) {}
async execute(command: RefreshTokenCommand) {
const { userId } = command
const accessSecretKey = process.env.ACCESS_JWT_SECRET_KEY
const refreshSecretKey = process.env.REFRESH_JWT_SECRET_KEY
const payload: { userId: string; date: Date } = {
userId,
date: new Date(),
}
const accessToken = jwt.sign(payload, accessSecretKey, { expiresIn: '10m' })
const refreshToken = jwt.sign(payload, refreshSecretKey, {
expiresIn: '30d',
})
const expiresIn = addDays(new Date(), 30)
await this.authRepository.createRefreshToken(userId, refreshToken, expiresIn)
return {
accessToken,
refreshToken,
}
}
}

View File

@@ -0,0 +1,41 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { BadRequestException, NotFoundException } from '@nestjs/common'
import { UsersRepository } from '../../users/infrastructure/users.repository'
import { UsersService } from '../../users/services/users.service'
export class ResendVerificationEmailCommand {
constructor(public readonly userId: string) {}
}
@CommandHandler(ResendVerificationEmailCommand)
export class ResendVerificationEmailHandler
implements ICommandHandler<ResendVerificationEmailCommand>
{
constructor(
private readonly usersRepository: UsersRepository,
private readonly usersService: UsersService
) {}
async execute(command: ResendVerificationEmailCommand) {
const user = await this.usersRepository.findUserById(command.userId)
console.log(user)
if (!user) {
throw new NotFoundException('User not found')
}
if (user.isEmailVerified) {
throw new BadRequestException('Email has already been verified')
}
const updatedUser = await this.usersRepository.updateVerificationToken(user.id)
await this.usersService.sendConfirmationEmail({
email: updatedUser.user.email,
name: updatedUser.user.name,
verificationToken: updatedUser.verificationToken,
})
if (!updatedUser) {
throw new NotFoundException('User not found')
}
return null
}
}

View File

@@ -0,0 +1,35 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { BadRequestException, NotFoundException } from '@nestjs/common'
import { isBefore } from 'date-fns'
import { UsersRepository } from '../../users/infrastructure/users.repository'
export class VerifyEmailCommand {
constructor(public readonly token: string) {}
}
@CommandHandler(VerifyEmailCommand)
export class VerifyEmailHandler implements ICommandHandler<VerifyEmailCommand> {
constructor(private readonly usersRepository: UsersRepository) {}
async execute(command: VerifyEmailCommand) {
const token = command.token
const verificationWithUser = await this.usersRepository.findUserByVerificationToken(token)
if (!verificationWithUser) throw new NotFoundException('User not found')
if (verificationWithUser.isEmailVerified)
throw new BadRequestException('Email has already been verified')
const dbToken = verificationWithUser.verificationToken
const isTokenExpired = isBefore(verificationWithUser.verificationTokenExpiry, new Date())
if (dbToken !== token || isTokenExpired) {
return false
}
const result = await this.usersRepository.updateEmailVerification(verificationWithUser.userId)
if (!result) {
throw new NotFoundException('User not found')
}
return null
}
}