-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathsignUpHelpers.ts
More file actions
143 lines (131 loc) · 4.31 KB
/
Copy pathsignUpHelpers.ts
File metadata and controls
143 lines (131 loc) · 4.31 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { HubInternal } from '@aws-amplify/core/internals/utils';
import { signIn } from '../apis/signIn';
import { SignInInput, SignInOutput } from '../types';
import { AutoSignInEventData } from '../types/models';
import { AutoSignInCallback } from '../../../types/models';
import { AuthError } from '../../../errors/AuthError';
import { resetAutoSignIn, setAutoSignIn } from '../apis/autoSignIn';
import { AUTO_SIGN_IN_EXCEPTION } from '../../../errors/constants';
import { signInWithUserAuth } from '../apis/signInWithUserAuth';
import { getAuthSessionValidity } from './getAuthSessionValidity';
export function handleCodeAutoSignIn(signInInput: SignInInput) {
const stopHubListener = HubInternal.listen<AutoSignInEventData>(
'auth-internal',
async ({ payload }) => {
switch (payload.event) {
case 'confirmSignUp': {
const response = payload.data;
if (response?.isSignUpComplete) {
HubInternal.dispatch('auth-internal', {
event: 'autoSignIn',
});
setAutoSignIn(autoSignInWithCode(signInInput));
stopHubListener();
}
}
}
},
);
// This will stop the listener if confirmSignUp is not resolved.
const timeOutId = setTimeout(() => {
stopHubListener();
clearTimeout(timeOutId);
resetAutoSignIn();
}, getAuthSessionValidity());
}
// Debounces the auto sign-in flow with link
// This approach avoids running the useInterval and signIn API twice in a row.
// This issue would be common as React.18 introduced double rendering of the
// useEffect hook on every mount.
// https://github.com/facebook/react/issues/24502
// https://legacy.reactjs.org/docs/strict-mode.html#ensuring-reusable-state
type TimeOutOutput = ReturnType<typeof setTimeout>;
function debounce<F extends (...args: any[]) => any>(fun: F, delay: number) {
let timer: TimeOutOutput | undefined;
return (args: F extends (...args: infer A) => any ? A : never): void => {
if (!timer) {
fun(...args);
}
clearTimeout(timer as TimeOutOutput);
timer = setTimeout(() => {
timer = undefined;
}, delay);
};
}
function handleAutoSignInWithLink(
signInInput: SignInInput,
resolve: (value: SignInOutput) => void,
reject: (reason?: any) => void,
) {
const start = Date.now();
const autoSignInPollingIntervalId = setInterval(async () => {
const elapsedTime = Date.now() - start;
const maxTime = getAuthSessionValidity();
if (elapsedTime > maxTime) {
clearInterval(autoSignInPollingIntervalId);
reject(
new AuthError({
name: AUTO_SIGN_IN_EXCEPTION,
message: 'The account was not confirmed on time.',
recoverySuggestion:
'Try to verify your account by clicking the link sent your email or phone and then login manually.',
}),
);
resetAutoSignIn();
} else {
try {
const signInOutput = await signIn(signInInput);
if (signInOutput.nextStep.signInStep !== 'CONFIRM_SIGN_UP') {
resolve(signInOutput);
clearInterval(autoSignInPollingIntervalId);
resetAutoSignIn();
}
} catch (error) {
clearInterval(autoSignInPollingIntervalId);
reject(error);
resetAutoSignIn();
}
}
}, 5000);
}
const debouncedAutoSignInWithLink = debounce(handleAutoSignInWithLink, 300);
const debouncedAutoSignWithCodeOrUserConfirmed = debounce(
handleAutoSignInWithCodeOrUserConfirmed,
300,
);
export function autoSignInWhenUserIsConfirmedWithLink(
signInInput: SignInInput,
): AutoSignInCallback {
return async () => {
return new Promise<SignInOutput>((resolve, reject) => {
debouncedAutoSignInWithLink([signInInput, resolve, reject]);
});
};
}
async function handleAutoSignInWithCodeOrUserConfirmed(
signInInput: SignInInput,
resolve: (value: SignInOutput) => void,
reject: (reason?: any) => void,
) {
try {
const output =
signInInput?.options?.authFlowType === 'USER_AUTH'
? await signInWithUserAuth(signInInput)
: await signIn(signInInput);
resolve(output);
resetAutoSignIn();
} catch (error) {
reject(error);
resetAutoSignIn();
}
}
function autoSignInWithCode(signInInput: SignInInput): AutoSignInCallback {
return async () => {
return new Promise<SignInOutput>((resolve, reject) => {
debouncedAutoSignWithCodeOrUserConfirmed([signInInput, resolve, reject]);
});
};
}
export const autoSignInUserConfirmed = autoSignInWithCode;