diff --git a/apps/web/src/core/lib/auth.helper.ts b/apps/web/src/core/lib/auth.helper.ts index 20c81a5..66d5d82 100644 --- a/apps/web/src/core/lib/auth.helper.ts +++ b/apps/web/src/core/lib/auth.helper.ts @@ -43,40 +43,44 @@ export async function terminateAuthSession(options: TerminateOptions = {}) { } export async function initiateAuthSession(respLogin: any) { - const data = respLogin?.data ?? {}; - const { token, id } = data; - const userProfile = lodash.omit(data, ['token']); + try { + const data = respLogin?.data ?? {}; + const { token, id } = data; + const userProfile = lodash.omit(data, ['token']); - if (!token) throw new Error('Login response missing token'); + if (!token) throw new Error('Login response missing token'); - // FIXME: Populate this with the mapped privilege data. - // NOTE: The assignment below needs to be updated. Replace the empty object `{}` - // with the actual mapped data (e.g., from mappingUserPrivilege(data)). - // Expected structure (Record): - // { - // "TRANSACTION_BOOKING": { - // ALLOW_CREATE: true, - // ALLOW_VIEW: true, - // // ... - // } - // } - const userPrivilege: Record = {}; + // FIXME: Populate this with the mapped privilege data. + // NOTE: The assignment below needs to be updated. Replace the empty object `{}` + // with the actual mapped data (e.g., from mappingUserPrivilege(data)). + // Expected structure (Record): + // { + // "TRANSACTION_BOOKING": { + // ALLOW_CREATE: true, + // ALLOW_VIEW: true, + // // ... + // } + // } + const userPrivilege: Record = {}; - /** - * Store credentials in local storage - */ - await appStorage.setItem(AppStorageKey.USER_ID, id); - await appDatabase.setItem(AppDatabaseKey.ACCESS_TOKEN, token); - await appDatabase.setItem(AppDatabaseKey.USER_PROFILE, { ...userProfile, label: userProfile.role }); - await appDatabase.setItem(AppDatabaseKey.USER_PRIVILEGE, userPrivilege); + /** + * Store credentials in local storage + */ + await appStorage.setItem(AppStorageKey.USER_ID, id); + await appDatabase.setItem(AppDatabaseKey.ACCESS_TOKEN, token); + await appDatabase.setItem(AppDatabaseKey.USER_PROFILE, { ...userProfile, label: userProfile.role }); + await appDatabase.setItem(AppDatabaseKey.USER_PRIVILEGE, userPrivilege); - /** - * Determine redirect destination - * If the user was redirected here from a protected page, honour that URL. - * Otherwise fall back to the default app entry point. - */ - const params = new URLSearchParams(window.location.search); - const redirectTo = params.get('redirect'); + /** + * Determine redirect destination + * If the user was redirected here from a protected page, honour that URL. + * Otherwise fall back to the default app entry point. + */ + const params = new URLSearchParams(window.location.search); + const redirectTo = params.get('redirect'); - window.location.replace(redirectTo ? decodeURIComponent(redirectTo) : '/app'); + window.location.replace(redirectTo ? decodeURIComponent(redirectTo) : '/app'); + } catch (error) { + throw error as any; + } }