Spaces:
Running
Running
| import axios from "axios"; | |
| import type { AuthUser } from "../types"; | |
| export interface AuthCredentials { | |
| username: string; | |
| password: string; | |
| } | |
| const authStorageKey = "resource-portal.auth"; | |
| export const getStoredCredentials = (): AuthCredentials | null => { | |
| try { | |
| const raw = window.sessionStorage.getItem(authStorageKey); | |
| if (!raw) return null; | |
| const parsed = JSON.parse(raw) as AuthCredentials; | |
| return parsed.username && parsed.password ? parsed : null; | |
| } catch { | |
| return null; | |
| } | |
| }; | |
| export const setStoredCredentials = (credentials: AuthCredentials) => { | |
| window.sessionStorage.setItem(authStorageKey, JSON.stringify(credentials)); | |
| }; | |
| export const clearStoredCredentials = () => { | |
| window.sessionStorage.removeItem(authStorageKey); | |
| }; | |
| export const api = axios.create({ | |
| baseURL: import.meta.env.VITE_API_BASE_URL ?? "/api/v1", | |
| }); | |
| api.interceptors.request.use((config) => { | |
| const credentials = getStoredCredentials(); | |
| if (credentials && !config.auth) { | |
| config.auth = credentials; | |
| } | |
| return config; | |
| }); | |
| export const getCurrentUser = async (credentials?: AuthCredentials) => | |
| (await api.get<AuthUser>("/me", credentials ? { auth: credentials } : undefined)).data; | |