Skip to content

Commit

Permalink
refactor: move initial whoami request to a middleware global
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenyWas committed Aug 11, 2024
1 parent 72acf7d commit 91f4fe5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
25 changes: 25 additions & 0 deletions middleware/00.whoami.global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default defineNuxtRouteMiddleware(async () => {
const user = useUser();
const auth = useAuth();
const { fetchWhoami } = useUserAPI();
const headers = useRequestHeaders();

if (headers['x-nuxt-error']) {
return;
}

await callOnce(async () => {
try {
const data = await fetchWhoami();

if (!data) {
throw new Error('Profile data is missed in whoami request');
}

user.value = data;
} catch (error) {
auth.value = { token: '', authorized: false };
console.log('Initial whoami request failed', error);
}
});
});
21 changes: 0 additions & 21 deletions plugins/00.whoami.ts

This file was deleted.

0 comments on commit 91f4fe5

Please sign in to comment.