Repository navigation
Expand file tree
/
Copy pathfactorSorting.ts
More file actions
56 lines (49 loc) · 1.59 KB
/
Copy pathfactorSorting.ts
File metadata and controls
56 lines (49 loc) · 1.59 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
import type { SignInFactor, SignInStrategy } from '@clerk/shared/types';
const makeSortingOrderMap = <T extends string>(arr: T[]): Record<T, number> =>
arr.reduce(
(acc, k, i) => {
acc[k] = i;
return acc;
},
{} as Record<T, number>,
);
const STRATEGY_SORT_ORDER_PASSWORD_PREF = makeSortingOrderMap([
'passkey',
'password',
'email_link',
'email_code',
'phone_code',
] as SignInStrategy[]);
const STRATEGY_SORT_ORDER_OTP_PREF = makeSortingOrderMap([
'email_link',
'email_code',
'phone_code',
'passkey',
'password',
] as SignInStrategy[]);
const STRATEGY_SORT_ORDER_ALL_STRATEGIES_BUTTONS = makeSortingOrderMap([
'email_link',
'email_code',
'phone_code',
'passkey',
'password',
] as SignInStrategy[]);
const STRATEGY_SORT_ORDER_BACKUP_CODE_PREF = makeSortingOrderMap([
'totp',
'phone_code',
'backup_code',
] as SignInStrategy[]);
const makeSortingFunction =
(sortingMap: Record<SignInStrategy, number>) =>
(a: SignInFactor, b: SignInFactor): number => {
const orderA = sortingMap[a.strategy];
const orderB = sortingMap[b.strategy];
if (orderA === undefined || orderB === undefined) {
return 0;
}
return orderA - orderB;
};
export const passwordPrefFactorComparator = makeSortingFunction(STRATEGY_SORT_ORDER_PASSWORD_PREF);
export const otpPrefFactorComparator = makeSortingFunction(STRATEGY_SORT_ORDER_OTP_PREF);
export const backupCodePrefFactorComparator = makeSortingFunction(STRATEGY_SORT_ORDER_BACKUP_CODE_PREF);
export const allStrategiesButtonsComparator = makeSortingFunction(STRATEGY_SORT_ORDER_ALL_STRATEGIES_BUTTONS);