This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
p61-project/domain/use-cases/Authentication.ts

58 lines
1.7 KiB
TypeScript

import {
UserRegisterSchema,
type User,
UserLoginSchema,
} from "../entities/User"
import type { AuthenticationRepository } from "../repositories/Authentication"
export interface AuthenticationUseCaseDependencyOptions {
authenticationRepository: AuthenticationRepository
}
export class AuthenticationUseCase
implements AuthenticationUseCaseDependencyOptions
{
public authenticationRepository: AuthenticationRepository
public constructor(options: AuthenticationUseCaseDependencyOptions) {
this.authenticationRepository = options.authenticationRepository
}
/**
* Register a new user.
* @throws {ZodError} if the data is invalid.
* @throws {Error} if user already exists.
* @param data
* @returns
*/
public async register(data: unknown): Promise<User> {
const userData = await UserRegisterSchema.parseAsync(data)
return await this.authenticationRepository.register(userData)
}
/**
* Login a user.
* @throws {ZodError} if the data is invalid.
* @throws {Error} if invalid credentials.
* @param data
* @returns
*/
public async login(data: unknown): Promise<User> {
const userData = await UserLoginSchema.parseAsync(data)
return await this.authenticationRepository.login(userData)
}
public async logout(): Promise<void> {
return await this.authenticationRepository.logout()
}
public getUser: AuthenticationRepository["getUser"] = async (...args) => {
return await this.authenticationRepository.getUser(...args)
}
public onUserStateChange: AuthenticationRepository["onUserStateChange"] =
async (...args) => {
return this.authenticationRepository.onUserStateChange(...args)
}
}