29 lines
827 B
TypeScript
29 lines
827 B
TypeScript
import { api, setTokens } from './client';
|
|
import type { AuthResponse, LoginRequiresTwoFactor, User } from './types';
|
|
|
|
export const authApi = {
|
|
async login(email: string, password: string) {
|
|
const res = await api.post<AuthResponse | LoginRequiresTwoFactor>('/auth/login', {
|
|
email,
|
|
password,
|
|
});
|
|
const d = res.data;
|
|
if (d && 'token' in d && d.token && 'refreshToken' in d) {
|
|
await setTokens(d.token, d.refreshToken);
|
|
}
|
|
return res;
|
|
},
|
|
|
|
async login2fa(twoFactorToken: string, token: string) {
|
|
const res = await api.post<AuthResponse>('/auth/login/2fa', { twoFactorToken, token });
|
|
if (res.data?.token && res.data.refreshToken) {
|
|
await setTokens(res.data.token, res.data.refreshToken);
|
|
}
|
|
return res;
|
|
},
|
|
|
|
me() {
|
|
return api.get<User>('/auth/me');
|
|
},
|
|
};
|