2022-10-15 20:15:55 +00:00
|
|
|
export default defineNuxtRouteMiddleware(async () => {
|
2023-02-18 06:57:21 +00:00
|
|
|
const ctx = useAuthContext();
|
2022-10-15 20:15:55 +00:00
|
|
|
const api = useUserApi();
|
|
|
|
|
2023-03-23 05:52:25 +00:00
|
|
|
if (!ctx.isAuthorized()) {
|
|
|
|
return navigateTo("/");
|
|
|
|
}
|
|
|
|
|
2023-02-18 06:57:21 +00:00
|
|
|
if (!ctx.user) {
|
2022-10-15 20:15:55 +00:00
|
|
|
const { data, error } = await api.user.self();
|
|
|
|
if (error) {
|
2023-03-23 05:52:25 +00:00
|
|
|
return navigateTo("/");
|
2022-10-15 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 06:57:21 +00:00
|
|
|
ctx.user = data.item;
|
2022-10-15 20:15:55 +00:00
|
|
|
}
|
|
|
|
});
|