-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathutils.tsx
More file actions
74 lines (65 loc) · 2.39 KB
/
utils.tsx
File metadata and controls
74 lines (65 loc) · 2.39 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
import { OAuthError } from './errors';
const CODE_RE = /[?&]code=[^&]+/;
const STATE_RE = /[?&]state=[^&]+/;
const ERROR_RE = /[?&]error=[^&]+/;
interface WithError {
error: string;
}
interface WithErrorAndDescription {
error: string;
error_description: string;
}
export const hasAuthParams = (searchParams = window.location.search): boolean =>
(CODE_RE.test(searchParams) || ERROR_RE.test(searchParams)) &&
STATE_RE.test(searchParams);
const normalizeErrorFn =
(fallbackMessage: string) =>
(error: unknown): Error => {
if (error instanceof Error) {
return error;
}
// try to check errors of the following form: {error: string; error_description?: string}
if (
error !== null &&
typeof error === 'object' &&
'error' in error &&
typeof (error as WithError).error === 'string'
) {
if (
'error_description' in error &&
typeof (error as WithErrorAndDescription).error_description === 'string'
) {
const e = error as WithErrorAndDescription;
return new OAuthError(e.error, e.error_description);
}
const e = error as WithError;
return new OAuthError(e.error);
}
return new Error(fallbackMessage);
};
export const loginError = normalizeErrorFn('Login failed');
export const tokenError = normalizeErrorFn('Get access token failed');
/**
* @ignore
* Helper function to map the v1 `redirectUri` option to the v2 `authorizationParams.redirect_uri`
* and log a warning.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const deprecateRedirectUri = (options?: any) => {
if (options?.redirectUri) {
console.warn(
'Using `redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `redirectUri` will be no longer supported in a future version'
);
options.authorizationParams = options.authorizationParams ?? {};
options.authorizationParams.redirect_uri = options.redirectUri;
delete options.redirectUri;
}
if (options?.authorizationParams?.redirectUri) {
console.warn(
'Using `authorizationParams.redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `authorizationParams.redirectUri` will be removed in a future version'
);
options.authorizationParams.redirect_uri =
options.authorizationParams.redirectUri;
delete options.authorizationParams.redirectUri;
}
};