47 lines
825 B
TypeScript
47 lines
825 B
TypeScript
|
|
// service to interact with the api/auth endpoints
|
|
// handles user registration, user logins, tokens, password reset, etc.
|
|
|
|
import { api, authStorage } from "./axios.ts"
|
|
import type { UserDto, RegisterDto, LoginDto } from "../models/User.ts";
|
|
|
|
const API_URL: string = "/auth";
|
|
|
|
export const register = async (user: RegisterDto) => {
|
|
|
|
try {
|
|
|
|
const response = await api.post(`${API_URL}/register`, user);
|
|
|
|
return true;
|
|
|
|
// else return false
|
|
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
export const login = async (user: LoginDto ) => {
|
|
|
|
try {
|
|
const response = await api.post(`${API_URL}/login`, user);
|
|
authStorage.setTokens(response.data);
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
export const logout = () => {
|
|
authStorage.clear();
|
|
}
|
|
|
|
export const getToken = () => {
|
|
authStorage.getAccessToken();
|
|
}
|