Files
aurora-moments/web/src/api/auth.ts
T

26 lines
717 B
TypeScript
Raw Normal View History

import { api, tokenStorage } from './client'
import type { User } from './types'
interface TokenPair {
access: string
refresh: string
}
export async function login(email: string, password: string): Promise<void> {
const tokens = await api.post<TokenPair>('/api/auth/token/', { email, password })
tokenStorage.set(tokens.access, tokens.refresh)
}
export async function register(email: string, username: string, password: string): Promise<void> {
await api.post<User>('/api/accounts/register/', { email, username, password })
await login(email, password)
}
export function logout(): void {
tokenStorage.clear()
}
export function getMe(): Promise<User> {
return api.get<User>('/api/accounts/me/')
}