Skip to content

Commit

Permalink
refactor(login-vue): refactor useUserStore to handle whoami response …
Browse files Browse the repository at this point in the history
…more efficiently and improve error handling
  • Loading branch information
Ro0t-set committed May 24, 2024
1 parent e2e015e commit 47c7d6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions frontend-service/src/main/vue/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pinia.use(piniaPluginPersistedState);
router.beforeEach((to, from, next) => {
const userStore = useUserStore();
if (to.meta.requiresAuth && !userStore.isLoggedIn) {
console.log("Requires auth");
next("/login");
} else {
next();
Expand Down
16 changes: 10 additions & 6 deletions frontend-service/src/main/vue/stores/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const useUserStore = defineStore(
async function refresh() {
if (refreshing) return;
refreshing = true;
console.log("Refreshing user");
await whoami();
console.log("Refreshing user");
refreshing = false;
}

Expand All @@ -40,7 +40,6 @@ export const useUserStore = defineStore(
if (response.statusCode === 200) {
isLoggedIn.value = true;
jwt.value = (response as LoginApi.Responses.Success).access_token;
await whoami();
} else {
const typed = response as LoginApi.Errors.Type;
throw new Error(typed.error);
Expand Down Expand Up @@ -71,10 +70,15 @@ export const useUserStore = defineStore(
// ==================== USER ==================== //
async function whoami() {
try {
const response =
(await userController.whoami()) as WhoamiApi.Responses.Success;
username.value = response.username;
email.value = response.email;
const response: WhoamiApi.Response = await userController.whoami();
if (response.statusCode === 200) {
const typed = response as WhoamiApi.Responses.Success;
username.value = typed.username;
email.value = typed.email;
} else {
await logout();
console.log("automatic logout");
}
} catch (e) {
await logout();
console.log("automatic logout", e);
Expand Down

0 comments on commit 47c7d6e

Please sign in to comment.