feat(#5): use auth

This commit is contained in:
Sirttas
2026-06-28 14:18:09 +02:00
parent e7249df23f
commit 61fbc8e16b
11 changed files with 1263 additions and 26 deletions
+76 -3
View File
@@ -1,19 +1,22 @@
import {logResource} from "@/service";
import axios from "axios";
import {getAccessToken, setAccessToken} from "@/auth/token";
import axios, {InternalAxiosRequestConfig} from "axios";
import {
AcquisitionApi,
ActivityApi,
AuthApi,
CharacterApi,
CharacterRuleBookApi,
LedgerApi,
MarketApi,
MeResponse,
ReprocessingApi,
RuleBookApi,
TransactionApi
} from "@/generated/mammon";
export const mammonUrl = import.meta.env.VITE_MAMMON_URL;
export const mammonAddCharacterUrl = mammonUrl + "oauth2/authorization/esi"
export const mammonLoginUrl = mammonUrl + "oauth2/authorization/esi"
const mammonAxiosInstance = axios.create({
baseURL: mammonUrl,
@@ -24,6 +27,76 @@ const mammonAxiosInstance = axios.create({
})
logResource(mammonAxiosInstance)
const credentialedClient = axios.create({
baseURL: mammonUrl,
withCredentials: true,
})
logResource(credentialedClient)
mammonAxiosInstance.interceptors.request.use(config => {
const token = getAccessToken();
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
})
const authApi = new AuthApi(undefined, mammonUrl, mammonAxiosInstance);
const credentialedAuthApi = new AuthApi(undefined, mammonUrl, credentialedClient);
let onAuthExpired: () => void = () => {};
export const setOnAuthExpired = (callback: () => void): void => {
onAuthExpired = callback;
}
let refreshing: Promise<string | null> | null = null;
export const refreshAccessToken = (): Promise<string | null> => {
if (!refreshing) {
refreshing = credentialedAuthApi.refresh()
.then(response => {
setAccessToken(response.data.accessToken);
return response.data.accessToken;
})
.catch(() => {
setAccessToken(null);
return null;
})
.finally(() => {
refreshing = null;
});
}
return refreshing;
}
mammonAxiosInstance.interceptors.response.use(response => response, async error => {
const original = error.config as (InternalAxiosRequestConfig & { _retried?: boolean }) | undefined;
if (error.response?.status === 401 && original && !original._retried) {
original._retried = true;
const token = await refreshAccessToken();
if (token) {
original.headers.Authorization = `Bearer ${token}`;
return mammonAxiosInstance(original);
}
onAuthExpired();
}
return Promise.reject(error);
})
export const fetchMe = (): Promise<MeResponse> =>
authApi.me().then(response => response.data);
export const addCharacter = (): Promise<void> =>
authApi.addCharacter({withCredentials: true}).then(() => undefined);
export const postLogout = (): Promise<void> =>
credentialedAuthApi.logout().then(() => undefined);
export const ledgerApi = new LedgerApi(undefined, mammonUrl, mammonAxiosInstance);
export const transactionApi = new TransactionApi(undefined, mammonUrl, mammonAxiosInstance);
export const characterApi = new CharacterApi(undefined, mammonUrl, mammonAxiosInstance);
@@ -32,4 +105,4 @@ export const characterRuleBookApi = new CharacterRuleBookApi(undefined, mammonUr
export const activityApi = new ActivityApi(undefined, mammonUrl, mammonAxiosInstance);
export const acquisitionApi = new AcquisitionApi(undefined, mammonUrl, mammonAxiosInstance);
export const marketApi = new MarketApi(undefined, mammonUrl, mammonAxiosInstance);
export const reprocessingApi = new ReprocessingApi(undefined, mammonUrl, mammonAxiosInstance);
export const reprocessingApi = new ReprocessingApi(undefined, mammonUrl, mammonAxiosInstance);