dev #1
2 changed files with 79 additions and 70 deletions
|
@ -30,11 +30,11 @@ export const useAuth = () => {
|
||||||
{
|
{
|
||||||
username, password: hashedPass, device_name: "Linux Laptop"
|
username, password: hashedPass, device_name: "Linux Laptop"
|
||||||
}
|
}
|
||||||
}) as { access_token: string, refresh_token: string }; fetch
|
}) as { access_token: string, refresh_token: string };
|
||||||
console.log("hi");
|
console.log("hi");
|
||||||
accessToken.value = res.access_token;
|
accessToken.value = res.access_token;
|
||||||
console.log("access token:", accessToken.value);
|
console.log("access token:", accessToken.value);
|
||||||
await fetchUser();
|
//await fetchUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function logout(password: string) {
|
async function logout(password: string) {
|
||||||
|
@ -60,19 +60,17 @@ export const useAuth = () => {
|
||||||
|
|
||||||
async function refresh() {
|
async function refresh() {
|
||||||
console.log("refreshing");
|
console.log("refreshing");
|
||||||
try {
|
|
||||||
const res = await fetchWithApi("/auth/refresh", {
|
const res = await fetchWithApi("/auth/refresh", {
|
||||||
method: "POST"
|
method: "POST"
|
||||||
}) as { access_token: string };
|
}) as any;
|
||||||
accessToken.value = res.access_token;
|
console.log("finished refreshing:", res);
|
||||||
|
accessToken.value = res?.access_token;
|
||||||
console.log("set new access token");
|
console.log("set new access token");
|
||||||
} catch (error) {
|
|
||||||
console.error("refresh error:", error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchUser() {
|
async function fetchUser() {
|
||||||
if (!accessToken.value) return;
|
if (!accessToken.value) return;
|
||||||
|
console.log("fetchuser access token:", accessToken.value);
|
||||||
const res = await fetchWithApi("/users/me") as UserResponse;
|
const res = await fetchWithApi("/users/me") as UserResponse;
|
||||||
user.value = res;
|
user.value = res;
|
||||||
return user.value;
|
return user.value;
|
||||||
|
|
|
@ -9,7 +9,6 @@ export default async <T>(path: string, options: NitroFetchOptions<string> = {})
|
||||||
path = path.slice(0, path.lastIndexOf("/"));
|
path = path.slice(0, path.lastIndexOf("/"));
|
||||||
}
|
}
|
||||||
console.log("formatted path:", path);
|
console.log("formatted path:", path);
|
||||||
try {
|
|
||||||
const accessToken = useCookie("access_token");
|
const accessToken = useCookie("access_token");
|
||||||
console.log("access token:", accessToken.value);
|
console.log("access token:", accessToken.value);
|
||||||
const apiBase = useCookie("api_base").value;
|
const apiBase = useCookie("api_base").value;
|
||||||
|
@ -49,23 +48,35 @@ export default async <T>(path: string, options: NitroFetchOptions<string> = {})
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
console.error("Error fetching resource");
|
||||||
if (error?.response?.status === 401) {
|
if (error?.response?.status === 401) {
|
||||||
|
console.log("Error status is 401");
|
||||||
if (!path.startsWith("/auth/refresh")) {
|
if (!path.startsWith("/auth/refresh")) {
|
||||||
|
console.log("Path is not refresh endpoint");
|
||||||
try {
|
try {
|
||||||
|
console.log("Trying to refresh");
|
||||||
await refresh();
|
await refresh();
|
||||||
|
console.log("Successfully refreshed token");
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
console.log("Failed to refresh token");
|
||||||
if (error?.response?.status === 401) {
|
if (error?.response?.status === 401) {
|
||||||
|
console.log("Refresh returned 401");
|
||||||
reauthFailed = true;
|
reauthFailed = true;
|
||||||
|
console.log("Revoking");
|
||||||
await revoke();
|
await revoke();
|
||||||
|
console.log("Redirecting to login");
|
||||||
|
await navigateTo("/login");
|
||||||
|
console.log("redirected");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
}
|
console.log("Path is refresh endpoint, throwing error");
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
console.log("throwing error");
|
||||||
console.error("error:", error);
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue