-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathsessionTasks.ts
More file actions
95 lines (83 loc) · 2.9 KB
/
Copy pathsessionTasks.ts
File metadata and controls
95 lines (83 loc) · 2.9 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
import { logger } from '../../logger';
import type { ClerkOptions, SessionResource, SessionTask, SetActiveParams } from '../../types';
import { forwardClerkQueryParams } from './queryParams';
import { buildURL } from './url';
/**
* @internal
*/
export const INTERNAL_SESSION_TASK_ROUTE_BY_KEY: Record<SessionTask['key'], string> = {
'choose-organization': 'choose-organization',
'reset-password': 'reset-password',
'setup-mfa': 'setup-mfa',
} as const;
/**
* @internal
*/
export const getTaskEndpoint = (task: SessionTask) => `/tasks/${INTERNAL_SESSION_TASK_ROUTE_BY_KEY[task.key]}`;
/**
* @internal
*/
export function buildTaskUrl(task: SessionTask, opts: Pick<Parameters<typeof buildURL>[0], 'base'>) {
const params = forwardClerkQueryParams();
return buildURL(
{
base: opts.base,
hashPath: getTaskEndpoint(task),
searchParams: params,
},
{ stringify: true },
);
}
/**
* @internal
*/
export function navigateIfTaskExists(
session: SessionResource,
{
navigate,
baseUrl,
}: {
navigate: (to: string) => Promise<unknown>;
baseUrl: string;
},
) {
const currentTask = session.currentTask;
if (!currentTask) {
return;
}
return navigate(buildTaskUrl(currentTask, { base: baseUrl }));
}
export function warnMissingPendingTaskHandlers(options: Record<string, unknown>) {
const taskOptions = ['taskUrls', 'navigate'] as Array<
keyof (Pick<SetActiveParams, 'navigate'> & Pick<ClerkOptions, 'taskUrls'>)
>;
const hasAtLeastOneOption = Object.keys(options).some(option => taskOptions.includes(option as any));
if (hasAtLeastOneOption) {
return;
}
// TODO - Link to after-auth docs once it gets released
logger.warnOnce(
`Clerk: Session has pending tasks but no handling is configured. To handle pending tasks, provide either "taskUrls" for navigation to custom URLs or "navigate" for programmatic navigation. Without these options, users may get stuck on incomplete flows.`,
);
}
/**
* Warns when a session is in the `pending` state. Mirrors the behavior of
* `SessionStatusLogger` on iOS / Android so developers across platforms get the
* same heads-up that the SDK is treating their signed-in user as signed out
* until the remaining session task is resolved.
*
* Dedupe is per-session/per-task: a new pending session or a task transition
* within the same session both surface a fresh log line.
*/
export function warnPendingSessionStatus(session: Pick<SessionResource, 'id' | 'status' | 'currentTask'>) {
if (session.status !== 'pending') {
return;
}
const taskKey = session.currentTask?.key;
const tasksDescription = taskKey ? ` Remaining session tasks: [${taskKey}].` : '';
logger.warnOnce(
`Clerk: Session ${session.id} is currently pending. ` +
`\`isSignedIn\` will return false and \`useAuth\` will report the user as signed out ` +
`until the remaining session tasks are completed.${tasksDescription}`,
);
}