Skip to content

Commit

Permalink
fix(user): improve handling of authentication errors when fetching cu…
Browse files Browse the repository at this point in the history
…rrent user

- Update fetchCurrentUser API method to use new api.call with error handling
- Modify user store action to handle unauthenticated errors
- Reset store if user is not authenticated
  • Loading branch information
neSpecc committed Feb 4, 2025
1 parent 39e088b commit c1106ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/api/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function refreshTokens(refreshToken) {
* @returns {Promise<User>}
*/
export async function fetchCurrentUser() {
return (await api.callOld(QUERY_CURRENT_USER)).me;
return await api.call(QUERY_CURRENT_USER, {}, undefined, { allowErrors: true });
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/store/modules/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,22 @@ const actions = {
* @param {Function} commit - standard Vuex commit function
*/
async [FETCH_CURRENT_USER]({ commit }) {
const me = await userApi.fetchCurrentUser();
const response = await userApi.fetchCurrentUser();

commit(mutationTypes.SET_CURRENT_USER, me);
if (Array.isArray(response.errors) && response.errors.length) {
const code = response.errors[0].extensions.code;

/**
* If user is not authenticated, log out
*/
if (code === 'UNAUTHENTICATED') {
commit(RESET_STORE);

return;
}
}

commit(mutationTypes.SET_CURRENT_USER, response.data.me);
},

/**
Expand Down

0 comments on commit c1106ad

Please sign in to comment.