diff --git a/src/app/Domain/Auth/Auth.ts b/src/app/Domain/Auth/Auth.ts index b7a9f76..97ca10d 100644 --- a/src/app/Domain/Auth/Auth.ts +++ b/src/app/Domain/Auth/Auth.ts @@ -1,9 +1,9 @@ export type Session = { username: string credential?: { - token: string, - refresh: string, - expiresAt: number | string | undefined, + token: string + refresh: string + expiresAt: number | string | undefined type: string } abilities?: string[] diff --git a/src/app/Domain/Contracts.ts b/src/app/Domain/Contracts.ts index 8a943b7..ced6fbd 100644 --- a/src/app/Domain/Contracts.ts +++ b/src/app/Domain/Contracts.ts @@ -6,8 +6,8 @@ export enum Status { export interface Content { status: Status - data?: Data + data?: Data | string | null message?: string } -export type Data = Record | string | null +export type Data = { [property: string]: Data | unknown } diff --git a/src/app/Infrastructure/Backend/HttpAuthRepository.ts b/src/app/Infrastructure/Backend/HttpAuthRepository.ts index 418fd79..f345b53 100644 --- a/src/app/Infrastructure/Backend/HttpAuthRepository.ts +++ b/src/app/Infrastructure/Backend/HttpAuthRepository.ts @@ -1,7 +1,8 @@ import AuthRepository from '../../Domain/Auth/AuthRepository.ts' -import { Content } from '../../Domain/Contracts.ts' +import { Data, Status } from '../../Domain/Contracts.ts' import { HttpClientDriverContract } from '../Http/Contracts.ts' import HttpClientFactory from '../Http/HttpClientFactory.ts' +import { Session } from '../../Domain/Auth/Auth.ts' export default class HttpAuthRepository implements AuthRepository { private http: HttpClientDriverContract @@ -14,15 +15,54 @@ export default class HttpAuthRepository implements AuthRepository { return new this(new HttpClientFactory()) } - async signInWithOtp (email: string): Promise { - return this.http.post('auth/otp', { email }) + async signInWithOtp (email: string): Promise { + const response = await this.http.post('/auth/otp', { email }) + if (response.status !== Status.success) { + throw new Error(response.message) + } + const data = response.data as Data + const user = data.user as Data + return { + username: user?.username as string, + abilities: [] + } } - async signIn (email: string, password: string): Promise { - return this.http.post('auth/signin', { email, password }) + async signIn (email: string, password: string): Promise { + const response = await this.http.post('/auth/sign-in', { email, password }) + if (response.status !== Status.success) { + throw new Error(response.message) + } + const data = response.data as Data + const user = data.user as Data + const credential = data.token as Data + return { + username: user?.username as string, + credential: { + token: credential?.token as string, + refresh: credential?.refresh as string, + expiresAt: credential?.expiresAt as string, + type: credential?.type as string, + }, + abilities: [] + } } - async signOut (): Promise { - return this.http.post('auth/signout') + async signOut (): Promise { + const { status } = await this.http.post('/auth/sign-out') + return status === Status.success + } + + async restore (): Promise { + const response = await this.http.get('/auth/me') + if (response.status !== Status.success) { + throw new Error(response.message) + } + const data = response.data as Data + const user = data.user as Data + return { + username: user?.username as string, + abilities: [] + } } } diff --git a/src/app/Infrastructure/Http/HttpClient.ts b/src/app/Infrastructure/Http/HttpClient.ts index 7ad8d50..e1d6916 100644 --- a/src/app/Infrastructure/Http/HttpClient.ts +++ b/src/app/Infrastructure/Http/HttpClient.ts @@ -77,7 +77,7 @@ export default class HttpClient implements HttpClientContract { return headers } - protected configureBody (data: Data | null): Data | null { + protected configureBody (data: Data | null): unknown { return data } } diff --git a/src/app/Infrastructure/Http/JsonHttpClient.ts b/src/app/Infrastructure/Http/JsonHttpClient.ts index cd154e9..35077ca 100644 --- a/src/app/Infrastructure/Http/JsonHttpClient.ts +++ b/src/app/Infrastructure/Http/JsonHttpClient.ts @@ -20,7 +20,7 @@ export default class JsonHttpClient extends HttpClient { return headers } - protected configureBody (data: Data | null): Data | null { + protected configureBody (data: Data | null): unknown { return JSON.stringify(data) } }