-
Notifications
You must be signed in to change notification settings - Fork 28
/
login-hooks.js
176 lines (161 loc) · 4.84 KB
/
login-hooks.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// @flow
import * as React from 'react';
import { setDataLoadedActionType } from '../actions/client-db-store-actions.js';
import {
identityLogInActionTypes,
useIdentityPasswordLogIn,
useIdentityWalletLogIn,
} from '../actions/user-actions.js';
import { useKeyserverAuth } from '../keyserver-conn/keyserver-auth.js';
import { logInActionSources } from '../types/account-types.js';
import { authoritativeKeyserverID } from '../utils/authoritative-keyserver.js';
import { useDispatchActionPromise } from '../utils/redux-promise-utils.js';
import { useSelector, useDispatch } from '../utils/redux-utils.js';
// We can't just do everything in one async callback, since the server calls
// would get bound to Redux state from before the login. In order to pick up the
// updated CSAT and currentUserInfo from Redux, we break the login into two
// steps.
type CurrentStep =
| { +step: 'inactive' }
| {
+step: 'identity_login_dispatched',
+resolve: () => void,
+reject: Error => void,
};
const inactiveStep = { step: 'inactive' };
type LogInInputs =
| {
+accountType: 'username',
+username: string,
+password: string,
}
| {
+accountType: 'ethereum',
+walletAddress: string,
+siweMessage: string,
+siweSignature: string,
};
function useLogIn(): LogInInputs => Promise<void> {
const [currentStep, setCurrentStep] =
React.useState<CurrentStep>(inactiveStep);
const identityPasswordLogIn = useIdentityPasswordLogIn();
const identityWalletLogIn = useIdentityWalletLogIn();
const dispatchActionPromise = useDispatchActionPromise();
const returnedFunc = React.useCallback(
(logInInputs: LogInInputs) =>
new Promise<void>(
// eslint-disable-next-line no-async-promise-executor
async (resolve, reject) => {
if (currentStep.step !== 'inactive') {
return;
}
const action =
logInInputs.accountType === 'username'
? identityPasswordLogIn(
logInInputs.username,
logInInputs.password,
)
: identityWalletLogIn(
logInInputs.walletAddress,
logInInputs.siweMessage,
logInInputs.siweSignature,
);
void dispatchActionPromise(identityLogInActionTypes, action);
try {
await action;
setCurrentStep({
step: 'identity_login_dispatched',
resolve,
reject,
});
} catch (e) {
reject(e);
}
},
),
[
currentStep,
dispatchActionPromise,
identityPasswordLogIn,
identityWalletLogIn,
],
);
const keyserverAuth = useKeyserverAuth(authoritativeKeyserverID());
const isRegisteredOnIdentity = useSelector(
state =>
!!state.commServicesAccessToken &&
!!state.currentUserInfo &&
!state.currentUserInfo.anonymous,
);
const registeringOnAuthoritativeKeyserverRef = React.useRef(false);
const dispatch = useDispatch();
React.useEffect(() => {
if (
!isRegisteredOnIdentity ||
currentStep.step !== 'identity_login_dispatched' ||
registeringOnAuthoritativeKeyserverRef.current
) {
return;
}
registeringOnAuthoritativeKeyserverRef.current = true;
const { resolve, reject } = currentStep;
void (async () => {
try {
await keyserverAuth({
authActionSource: process.env.BROWSER
? logInActionSources.keyserverAuthFromWeb
: logInActionSources.keyserverAuthFromNative,
setInProgress: () => {},
hasBeenCancelled: () => false,
doNotRegister: false,
});
dispatch({
type: setDataLoadedActionType,
payload: {
dataLoaded: true,
},
});
resolve();
} catch (e) {
reject(e);
} finally {
setCurrentStep(inactiveStep);
registeringOnAuthoritativeKeyserverRef.current = false;
}
})();
}, [currentStep, isRegisteredOnIdentity, keyserverAuth, dispatch]);
return returnedFunc;
}
function usePasswordLogIn(): (
username: string,
password: string,
) => Promise<void> {
const logIn = useLogIn();
return React.useCallback(
(username: string, password: string) =>
logIn({
accountType: 'username',
username,
password,
}),
[logIn],
);
}
function useWalletLogIn(): (
walletAddress: string,
siweMessage: string,
siweSignature: string,
) => Promise<void> {
const logIn = useLogIn();
return React.useCallback(
(walletAddress: string, siweMessage: string, siweSignature: string) =>
logIn({
accountType: 'ethereum',
walletAddress,
siweMessage,
siweSignature,
}),
[logIn],
);
}
export { usePasswordLogIn, useWalletLogIn };