fix session clearing on error

This commit is contained in:
Hayden 2023-03-22 21:12:55 -08:00
parent ed1230e17d
commit 29f2f7bcfe
No known key found for this signature in database
GPG key ID: 17CF79474E257545
2 changed files with 6 additions and 1 deletions

View file

@ -34,6 +34,7 @@ export function useUserApi(): UserClient {
requests.addResponseInterceptor(logger);
requests.addResponseInterceptor(r => {
if (r.status === 401) {
console.error("unauthorized request, invalidating session");
authCtx.invalidateSession();
}
});

View file

@ -2,10 +2,14 @@ export default defineNuxtRouteMiddleware(async () => {
const ctx = useAuthContext();
const api = useUserApi();
if (!ctx.isAuthorized()) {
return navigateTo("/");
}
if (!ctx.user) {
const { data, error } = await api.user.self();
if (error) {
navigateTo("/");
return navigateTo("/");
}
ctx.user = data.item;