30 lines
653 B
TypeScript
30 lines
653 B
TypeScript
const BASE_URL = import.meta.env.VITE_API_URL + '/auth'
|
|
|
|
export interface User {
|
|
id: number
|
|
username: string
|
|
createdAt: Date
|
|
isLocked: boolean
|
|
isAdmin: boolean
|
|
}
|
|
|
|
export function getAuthHeaders(basicAuth: string) {
|
|
return {
|
|
Authorization: 'Basic ' + basicAuth,
|
|
}
|
|
}
|
|
|
|
export async function login(username: string, password: string): Promise<User | null> {
|
|
const basicAuth = btoa(username + ':' + password)
|
|
const response = await fetch(BASE_URL + '/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Basic ' + basicAuth,
|
|
},
|
|
})
|
|
if (!response.ok) {
|
|
return null
|
|
}
|
|
return (await response.json()) as User
|
|
}
|