2022-10-15 12:15:55 -08:00
|
|
|
export default defineNuxtRouteMiddleware(async () => {
|
2023-02-17 21:57:21 -09:00
|
|
|
const ctx = useAuthContext();
|
2022-10-15 12:15:55 -08:00
|
|
|
const api = useUserApi();
|
|
|
|
|
2023-03-22 21:52:25 -08:00
|
|
|
if (!ctx.isAuthorized()) {
|
|
|
|
return navigateTo("/");
|
|
|
|
}
|
|
|
|
|
2023-02-17 21:57:21 -09:00
|
|
|
if (!ctx.user) {
|
2023-10-06 22:44:43 -05:00
|
|
|
console.log("Fetching user data");
|
2022-10-15 12:15:55 -08:00
|
|
|
const { data, error } = await api.user.self();
|
|
|
|
if (error) {
|
2023-03-22 21:52:25 -08:00
|
|
|
return navigateTo("/");
|
2022-10-15 12:15:55 -08:00
|
|
|
}
|
|
|
|
|
2023-02-17 21:57:21 -09:00
|
|
|
ctx.user = data.item;
|
2022-10-15 12:15:55 -08:00
|
|
|
}
|
|
|
|
});
|