-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathuser.ts
47 lines (44 loc) · 1.86 KB
/
user.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { SamUserAttributes, User } from 'src/libs/ajax/User';
import { clearNotification, sessionExpirationProps } from 'src/libs/notifications';
import { AuthState, authStore, TerraUserProfile, TerraUserState, userStore } from 'src/libs/state';
export const refreshTerraProfile = async () => {
const profile: TerraUserProfile = await User().profile.get();
userStore.update((state: TerraUserState) => ({ ...state, profile }));
};
export const refreshSamUserAttributes = async (): Promise<void> => {
const terraUserAttributes: SamUserAttributes = await User().getUserAttributes();
userStore.update((state: TerraUserState) => ({ ...state, terraUserAttributes }));
};
export const loadTerraUser = async (): Promise<void> => {
try {
const signInStatus = 'userLoaded';
const getProfile = User().profile.get();
const getCombinedState = User().getSamUserCombinedState();
const [profile, terraUserCombinedState] = await Promise.all([getProfile, getCombinedState]);
const { terraUserAttributes, enterpriseFeatures, samUser, terraUserAllowances, termsOfService, favoriteResources } =
terraUserCombinedState;
clearNotification(sessionExpirationProps.id);
userStore.update((state: TerraUserState) => ({
...state,
profile,
terraUserAttributes,
enterpriseFeatures,
samUser,
favoriteResources,
}));
authStore.update((state: AuthState) => ({
...state,
signInStatus,
terraUserAllowances,
termsOfService,
}));
} catch (error: unknown) {
if ((error as Response).status !== 403) {
throw error;
}
// If the call to User endpoints fail with a 403, it means the user is not registered.
// Update the AuthStore state to forward them to the registration page.
const signInStatus = 'unregistered';
authStore.update((state: AuthState) => ({ ...state, signInStatus }));
}
};