feat: change requests to use apiBase localStorage value

This commit is contained in:
SauceyRed 2025-05-07 19:47:51 +02:00
parent 4364e9fa3b
commit fecf6fb6e0

View file

@ -1,11 +1,12 @@
export const useAuth = () => {
const apiVersion = useRuntimeConfig().public.apiVersion;
const config = useRuntimeConfig();
const accessToken = useCookie("access_token");
const user = useState("user", () => null);
async function register(username: string, email: string, password: string) {
const apiBase = localStorage.getItem("apiBase");
const hashedPass = await hashPassword(password);
const res = await $fetch(`/api/v${apiVersion}/auth/register`, {
const res = await $fetch(`${apiBase}/auth/register`, {
method: "POST", body:
{
email, identifier: username, password: hashedPass, device_name: "Linux Laptop"
@ -16,10 +17,11 @@ export const useAuth = () => {
}
async function login(username: string, password: string, device_name: string) {
const apiBase = localStorage.getItem("apiBase");
const hashedPass = await hashPassword(password);
console.log("hashedPass:", hashedPass);
//authStore.setAccessToken(accessToken);
const res = await $fetch(`/api/v${apiVersion}/auth/login`, {
const res = await fetchWithAuth(`${apiBase}/auth/login`, {
method: "POST", body:
{
username, password: hashedPass, device_name: "Linux Laptop"
@ -32,12 +34,13 @@ export const useAuth = () => {
}
async function logout(password: string) {
const apiBase = localStorage.getItem("apiBase");
console.log("password:", password);
console.log("access:", accessToken.value);
const hashedPass = await hashPassword(password);
console.log("hashed");
const res = await fetchWithAuth(`/api/v${apiVersion}/auth/revoke`, {
const res = await fetchWithAuth(`${apiBase}/auth/revoke`, {
method: "POST",
body:
{
@ -51,18 +54,22 @@ export const useAuth = () => {
async function revoke() {
accessToken.value = null;
localStorage.removeItem("instanceUrl");
localStorage.removeItem("apiBase");
}
async function refresh() {
const res = await $fetch(`/api/v${apiVersion}/auth/refresh`, {
const apiBase = localStorage.getItem("apiBase");
const res = await fetchWithAuth(`${apiBase}/auth/refresh`, {
method: "POST"
}) as { access_token: string };
accessToken.value = res.access_token;
}
async function fetchUser() {
const apiBase = localStorage.getItem("apiBase");
if (!accessToken.value) return;
const res = await fetchWithAuth(`/api/v${apiVersion}/users/me`) as any;
const res = await fetchWithAuth(`${apiBase}/users/me`) as any;
user.value = res;
}