session.ts (1231B)
1 import { EntityLocked, EntityNotFound } from "../repository_error.ts"; 2 3 import { AuthRepository } from "#core/application/authn/auth_repository.ts"; 4 import { ExpiredSessionToken } from "#core/domain/session_token.ts"; 5 import { InvalidToken, Token } from "#core/domain/token.ts"; 6 7 export type AuthSessionRequest = { 8 sessionToken: string; 9 }; 10 11 export type AuthSessionResponse = { 12 status: "authenticated" | "expired"; 13 uuid: string | null; 14 }; 15 16 export class AuthSessionUseCase { 17 constructor( 18 private readonly repo: AuthRepository, 19 ) { 20 } 21 22 async execute(request: AuthSessionRequest): Promise<AuthSessionResponse> { 23 try { 24 const sessionToken = new Token(request.sessionToken); 25 const auth = await this.repo.findBySessionToken(sessionToken); 26 auth.authenticate(sessionToken); 27 await this.repo.store(auth); 28 return { 29 status: "authenticated", 30 uuid: auth.id.toString(), 31 }; 32 } catch (error) { 33 if ( 34 error instanceof InvalidToken || 35 error instanceof ExpiredSessionToken || 36 error instanceof EntityNotFound || 37 error instanceof EntityLocked 38 ) { 39 return { status: "expired", uuid: null }; 40 } 41 throw error; 42 } 43 } 44 }