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/presentation/presenters/Authentication.ts

164 lines
3.8 KiB
TypeScript
Raw Normal View History

import { ZodError } from "zod"
import type {
User,
UserLoginData,
UserRegisterData,
} from "@/domain/entities/User"
import type { AuthenticationUseCase } from "@/domain/use-cases/Authentication"
import type { ErrorGlobal, FetchState } from "./_Presenter"
import { Presenter } from "./_Presenter"
2024-03-24 23:41:23 +01:00
import { getErrorsFieldsFromZodError } from "./utils/zod"
export interface AuthenticationPresenterState {
user: User | null
/**
* `true` if the initial authentication state has been loaded.
*/
hasLoaded: boolean
register: {
state: FetchState
2024-03-24 23:41:23 +01:00
errors: {
fields: Array<keyof UserRegisterData>
global: ErrorGlobal
}
}
login: {
state: FetchState
2024-03-24 23:41:23 +01:00
errors: {
fields: Array<keyof UserLoginData>
global: ErrorGlobal
}
}
logout: {
state: FetchState
}
}
export interface AuthenticationPresenterOptions {
authenticationUseCase: AuthenticationUseCase
}
export class AuthenticationPresenter
extends Presenter<AuthenticationPresenterState>
implements AuthenticationPresenterOptions
{
public authenticationUseCase: AuthenticationUseCase
public constructor(options: AuthenticationPresenterOptions) {
const { authenticationUseCase } = options
super({
user: null,
hasLoaded: true,
register: {
state: "idle",
2024-03-24 23:41:23 +01:00
errors: {
fields: [],
global: null,
},
},
login: {
state: "idle",
2024-03-24 23:41:23 +01:00
errors: {
fields: [],
global: null,
},
},
logout: {
state: "idle",
},
})
this.authenticationUseCase = authenticationUseCase
}
public async register(data: unknown): Promise<void> {
try {
this.setState((state) => {
state.register.state = "loading"
2024-03-24 23:41:23 +01:00
state.register.errors = {
fields: [],
global: null,
}
})
const user = await this.authenticationUseCase.register(data)
this.setState((state) => {
state.register.state = "success"
state.user = user
})
} catch (error) {
this.setState((state) => {
state.register.state = "error"
if (error instanceof ZodError) {
2024-03-24 23:41:23 +01:00
state.register.errors.fields =
getErrorsFieldsFromZodError<UserRegisterData>(error)
} else {
2024-03-24 23:41:23 +01:00
state.register.errors.global = "unknown"
}
})
}
}
public async login(data: unknown): Promise<void> {
try {
this.setState((state) => {
state.login.state = "loading"
2024-03-24 23:41:23 +01:00
state.login.errors = {
fields: [],
global: null,
}
})
const user = await this.authenticationUseCase.login(data)
this.setState((state) => {
state.login.state = "success"
state.user = user
})
} catch (error) {
this.setState((state) => {
state.login.state = "error"
if (error instanceof ZodError) {
2024-03-24 23:41:23 +01:00
state.login.errors.fields =
getErrorsFieldsFromZodError<UserLoginData>(error)
} else {
2024-03-24 23:41:23 +01:00
state.login.errors.global = "unknown"
}
})
}
}
public async logout(): Promise<void> {
try {
this.setState((state) => {
state.logout.state = "loading"
})
await this.authenticationUseCase.logout()
this.setState((state) => {
state.logout.state = "success"
state.user = null
})
} catch (error) {
this.setState((state) => {
state.user = null
state.logout.state = "error"
})
}
}
public async initialAuthStateListener(): Promise<void> {
const user = await this.authenticationUseCase.getUser()
this.setState((state) => {
state.user = user
state.hasLoaded = false
})
this.authenticationUseCase.onUserStateChange((user) => {
this.setState((state) => {
state.user = user
})
})
}
}