Skip to content

Commit

Permalink
feature: improve auth
Browse files Browse the repository at this point in the history
  • Loading branch information
wilcorrea committed Apr 9, 2024
1 parent f17ada9 commit 1989305
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/app/Domain/Auth/Auth.ts
Original file line number Diff line number Diff line change
@@ -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[]
Expand Down
4 changes: 2 additions & 2 deletions src/app/Domain/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, unknown> | string | null
export type Data = { [property: string]: Data | unknown }
54 changes: 47 additions & 7 deletions src/app/Infrastructure/Backend/HttpAuthRepository.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,15 +15,54 @@ export default class HttpAuthRepository implements AuthRepository {
return new this(new HttpClientFactory())
}

async signInWithOtp (email: string): Promise<Content> {
return this.http.post('auth/otp', { email })
async signInWithOtp (email: string): Promise<Session> {
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<Content> {
return this.http.post('auth/signin', { email, password })
async signIn (email: string, password: string): Promise<Session> {
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<Content> {
return this.http.post('auth/signout')
async signOut (): Promise<boolean> {
const { status } = await this.http.post('/auth/sign-out')
return status === Status.success
}

async restore (): Promise<Session> {
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: []
}
}
}
2 changes: 1 addition & 1 deletion src/app/Infrastructure/Http/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion src/app/Infrastructure/Http/JsonHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 1989305

Please sign in to comment.