-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathSessionUtils.ts
More file actions
125 lines (103 loc) · 4.66 KB
/
Copy pathSessionUtils.ts
File metadata and controls
125 lines (103 loc) · 4.66 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import Onyx from 'react-native-onyx';
import useOnyx from '@hooks/useOnyx';
import CONFIG from '@src/CONFIG';
import ONYXKEYS from '@src/ONYXKEYS';
const NEW_PARTNER_USER_ID_PREFIX = 'expensify.cash-';
/**
* Determine if the transitioning user is logging in as a new user.
*/
function isLoggingInAsNewUser(transitionURL?: string, sessionEmail?: string): boolean {
// The OldDot mobile app does not URL encode the parameters, but OldDot web
// does. We don't want to deploy OldDot mobile again, so as a work around we
// compare the session email to both the decoded and raw email from the transition link.
const params = new URLSearchParams(transitionURL);
const paramsEmail = params.get('email');
const delegatorEmail = params.get('delegatorEmail');
// If the email param matches what is stored in the session then we are
// definitely not logging in as a new user. If delegator email matches session email
// it means that we are login in to another account as supportal user.
if (paramsEmail === sessionEmail || delegatorEmail === sessionEmail) {
return false;
}
// If they do not match it might be due to encoding, so check the raw value
// Capture the un-encoded text in the email param
const emailParamRegex = /[?&]email=([^&]*)/g;
const matches = emailParamRegex.exec(transitionURL ?? '');
const linkedEmail = matches?.[1] ?? null;
if (linkedEmail === sessionEmail) {
return false;
}
// If URLSearchParams didn't find it (e.g. transitionURL is a full URL which
// mangles the first query-param key), fall back to regex
const linkedDelegatorEmail = getDelegatorEmailFromURL(transitionURL) ?? null;
return linkedEmail !== sessionEmail && linkedDelegatorEmail !== sessionEmail;
}
/**
* Determine if the transitioning user is logging in as a delegate
*/
function isLoggingInAsDelegate(transitionURL?: string): boolean {
const params = new URLSearchParams(transitionURL);
const delegatorEmail = params.get('delegatorEmail');
if (delegatorEmail) {
return true;
}
// If URLSearchParams didn't find it (e.g. transitionURL is a full URL which
// mangles the first query-param key), fall back to regex
const linkedDelegatorEmail = getDelegatorEmailFromURL(transitionURL);
return !!linkedDelegatorEmail;
}
/**
* Looks for *delegatorEmail* param in given URL using regex
*/
function getDelegatorEmailFromURL(url?: string): string | undefined {
const delegatorEmailParamRegex = /[?&]delegatorEmail=([^&]*)/g;
const delegatorMatches = delegatorEmailParamRegex.exec(url ?? '');
return delegatorMatches?.[1];
}
let loggedInDuringSession: boolean | undefined;
// To tell if the user logged in during this session we will check the value of session.authToken once when the app's JS inits. When the user logs out
// we can reset this flag so that it can be updated again.
Onyx.connectWithoutView({
key: ONYXKEYS.SESSION,
callback: (session) => {
if (loggedInDuringSession) {
return;
}
// We are incorporating a check for 'signedInWithShortLivedAuthToken' to handle cases where login is performed using a ShortLivedAuthToken
// This check is necessary because, with ShortLivedAuthToken, 'authToken' gets populated, leading to 'loggedInDuringSession' being assigned a false value
if (session?.authToken && !session?.signedInWithShortLivedAuthToken) {
loggedInDuringSession = false;
} else {
loggedInDuringSession = true;
}
},
});
function resetDidUserLogInDuringSession() {
loggedInDuringSession = true;
}
function didUserLogInDuringSession() {
return !!loggedInDuringSession;
}
function checkIfShouldUseNewPartnerName(partnerUserID?: string): boolean {
if (!CONFIG.IS_HYBRID_APP) {
return true;
}
// On HybridApp, users who logged in on the old SignInPage must use legacy partner name.
// Users who logged in on NewDot SignInPage have partnerUserID with "expensify.cash-" prefix and use new partner name.
if (partnerUserID?.startsWith(NEW_PARTNER_USER_ID_PREFIX)) {
return true;
}
return false;
}
const AGENT_EMAIL_REGEX = /^agent_\d+@expensify\.ai$/i;
function isAgentEmail(email?: string): boolean {
if (!email) {
return false;
}
return AGENT_EMAIL_REGEX.test(email);
}
function useIsAgentAccount(): boolean {
const [sessionEmail] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.email});
return isAgentEmail(sessionEmail);
}
export {isLoggingInAsNewUser, didUserLogInDuringSession, resetDidUserLogInDuringSession, checkIfShouldUseNewPartnerName, isLoggingInAsDelegate, isAgentEmail, useIsAgentAccount};