You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

40 lines
1.1 KiB

import {
DEFAULT_AUTHENTICATED_LANDING_PATH,
isGuestOnlyRoute,
isPublicRoute,
normalizeSafeRedirect,
} from "../utils/auth-routes";
import { useAuthSession } from "../composables/useAuthSession";
export default defineNuxtRouteMiddleware(async (to) => {
const { initialized, loggedIn, refresh } = useAuthSession();
if (!initialized.value) {
await refresh();
}
const currentPath = to.path;
const currentFullPath = to.fullPath;
const isLoggedIn = loggedIn.value;
if (!isLoggedIn && !isPublicRoute(currentPath)) {
return navigateTo({
path: "/login",
query: { redirect: currentFullPath },
});
}
if (isLoggedIn && isGuestOnlyRoute(currentPath)) {
const redirectCandidate = Array.isArray(to.query.redirect)
? to.query.redirect[0]
: to.query.redirect;
const redirectTarget = normalizeSafeRedirect(
redirectCandidate,
DEFAULT_AUTHENTICATED_LANDING_PATH,
);
if (redirectTarget !== currentFullPath && redirectTarget !== currentPath) {
return navigateTo(redirectTarget);
}
return navigateTo(DEFAULT_AUTHENTICATED_LANDING_PATH);
}
});