feat: manage loading state in auth middleware
This commit is contained in:
parent
379b85db4e
commit
a439f9481a
1 changed files with 5 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
export default defineNuxtRouteMiddleware(async (to, from) => {
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||||
console.log("to.path:", to.path);
|
console.log("to.path:", to.path);
|
||||||
|
const loading = useState("loading");
|
||||||
const accessToken = useCookie("access_token").value;
|
const accessToken = useCookie("access_token").value;
|
||||||
if (["/login", "/register"].includes(to.path)) {
|
if (["/login", "/register"].includes(to.path)) {
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
|
@ -9,11 +10,15 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!accessToken) {
|
if (!accessToken) {
|
||||||
|
loading.value = true;
|
||||||
|
console.log("set loading to true");
|
||||||
const { refresh } = useAuth();
|
const { refresh } = useAuth();
|
||||||
console.log("hi");
|
console.log("hi");
|
||||||
await refresh();
|
await refresh();
|
||||||
const query = new URLSearchParams();
|
const query = new URLSearchParams();
|
||||||
query.set("redirect_to", to.path);
|
query.set("redirect_to", to.path);
|
||||||
|
loading.value = false;
|
||||||
|
console.log("set loading to false");
|
||||||
return await navigateTo("/login" + (query ?? ""));
|
return await navigateTo("/login" + (query ?? ""));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue