literally every time
All checks were successful
Build and Deploy Frontend / build-and-deploy (push) Successful in 6s
All checks were successful
Build and Deploy Frontend / build-and-deploy (push) Successful in 6s
This commit is contained in:
49
client/src/api/AuthApi.ts
Normal file
49
client/src/api/AuthApi.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
// service to interact with the api/auth endpoints
|
||||
// handles user registration, user logins, tokens, password reset, etc.
|
||||
|
||||
import api from "./axios.ts"
|
||||
import type { User } from "../models/User.ts";
|
||||
|
||||
const API_URL: string = "/auth";
|
||||
|
||||
export const register = async (user: { username: string; email: string; password: string }) => {
|
||||
|
||||
try {
|
||||
const response = await api.post(`${API_URL}/register`, user);
|
||||
|
||||
// TODO: if valid
|
||||
|
||||
return true;
|
||||
|
||||
// else return false
|
||||
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const login = async (user: { username: string; password: string }) => {
|
||||
|
||||
try {
|
||||
const response = await api.post(`${API_URL}/login`, user);
|
||||
const token = response.data.token;
|
||||
|
||||
localStorage.setItem("token", token);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const logout = () => {
|
||||
localStorage.removeItem("token");
|
||||
}
|
||||
|
||||
export const getToken = () => {
|
||||
return localStorage.getItem("token");
|
||||
}
|
||||
Reference in New Issue
Block a user