From dd123c6ea0a490aef4c0b0acd11332955bba3399 Mon Sep 17 00:00:00 2001 From: shaysw Date: Tue, 30 Aug 2022 21:33:56 +0300 Subject: [PATCH] added GET call to BE to fetch user logged in status instead of relying on try/catch --- src/store/user.store.ts | 19 ++++++++++++++++--- src/utils/utils.ts | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/store/user.store.ts b/src/store/user.store.ts index 16b4002a..348066a0 100644 --- a/src/store/user.store.ts +++ b/src/store/user.store.ts @@ -11,8 +11,9 @@ import { updateUserOrganization, } from 'services/user.service'; import { IUserInfo } from 'models/user/IUserInfo'; -import { ROLE_ADMIN_NAME } from 'utils/utils'; +import { ROLE_ADMIN_NAME, GET_IS_USER_LOGGED_IN_URL } from 'utils/utils'; import { IFormInput } from 'components/molecules/UserUpdateForm'; +import axios from 'axios'; export default class UserStore { isUserAuthenticated: boolean = false; @@ -70,8 +71,19 @@ export default class UserStore { }); } - getUserLoginDetails() { - fetchUserInfo() + async getUserLoginDetails() { + let isUserLoggedIn = false; + + await axios.get(GET_IS_USER_LOGGED_IN_URL, { withCredentials: true }) + .then((response) => { + isUserLoggedIn = response.data['is_user_logged_in'] + }).catch((err) => { + console.log('Unable to determine whether user is logged in'); + console.error(err); + }); + + if (isUserLoggedIn){ + fetchUserInfo() .then((userData) => { runInAction(() => { this.userInfo = userData; @@ -85,6 +97,7 @@ export default class UserStore { }); console.error(err); }); + } } async updateUserInfo(formInput: IFormInput) { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index b3539fb0..a441168b 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -39,6 +39,7 @@ export const GET_USERS_INFO_LIST_URL = `${authServerUrl}user/get_all_users_info` export const ADD_ROLE_TO_USER_URL = `${authServerUrl}user/add_to_role`; export const UPDATE_USER_ORG_URL = `${authServerUrl}/user/update_user_org`; export const REMOVE_USER_FROM_ORG_URL = `${authServerUrl}/user/remove_user_from_org`; +export const GET_IS_USER_LOGGED_IN_URL = `${authServerUrl}/user/is_user_logged_in`; AUTH_LOGIN_GOOGLE_URL.searchParams.append('redirect_url', `${window.location.origin}${REDIRECT_ROUTE!}`);