Repository navigation
Expand file tree
/
Copy pathhooks.ts
More file actions
271 lines (247 loc) · 6.68 KB
/
Copy pathhooks.ts
File metadata and controls
271 lines (247 loc) · 6.68 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import type {
ActJWTClaim,
CheckAuthorizationWithCustomPermissions,
Clerk,
GetToken,
OrganizationCustomRoleKey,
SignOut,
} from '@clerk/types';
import type { Store, StoreValue } from 'nanostores';
import { useCallback, useSyncExternalStore } from 'react';
// @ts-ignore
import { authAsyncStorage } from '#async-local-storage';
import { $authStore } from '../stores/external';
import { $clerk, $csrState } from '../stores/internal';
type CheckAuthorizationSignedOut = undefined;
type CheckAuthorizationWithoutOrgOrUser = (params?: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;
/**
* @internal
*/
const clerkLoaded = () => {
return new Promise<Clerk>(resolve => {
$csrState.subscribe(({ isLoaded }) => {
if (isLoaded) {
resolve($clerk.get()!);
}
});
});
};
/**
* @internal
*/
const createGetToken = () => {
return async (options: any) => {
const clerk = await clerkLoaded();
if (!clerk.session) {
return null;
}
return clerk.session.getToken(options);
};
};
/**
* @internal
*/
const createSignOut = () => {
return async (...args: any) => {
const clerk = await clerkLoaded();
return clerk.signOut(...args);
};
};
type UseAuthReturn =
| {
isLoaded: false;
isSignedIn: undefined;
userId: undefined;
sessionId: undefined;
actor: undefined;
orgId: undefined;
orgRole: undefined;
orgSlug: undefined;
has: CheckAuthorizationSignedOut;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: false;
userId: null;
sessionId: null;
actor: null;
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationWithoutOrgOrUser;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: true;
userId: string;
sessionId: string;
actor: ActJWTClaim | null;
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationWithoutOrgOrUser;
signOut: SignOut;
getToken: GetToken;
}
| {
isLoaded: true;
isSignedIn: true;
userId: string;
sessionId: string;
actor: ActJWTClaim | null;
orgId: string;
orgRole: OrganizationCustomRoleKey;
orgSlug: string | null;
has: CheckAuthorizationWithCustomPermissions;
signOut: SignOut;
getToken: GetToken;
};
type UseAuth = () => UseAuthReturn;
/**
* Returns the current auth state, the user and session ids and the `getToken`
* that can be used to retrieve the given template or the default Clerk token.
*
* Until Clerk loads, `isLoaded` will be set to `false`.
* Once Clerk loads, `isLoaded` will be set to `true`, and you can
* safely access the `userId` and `sessionId` variables.
*
* For projects using a server, you can have immediate access to this data during SSR.
*
* @example
* A simple example:
*
* function Hello() {
* const { isSignedIn, sessionId, userId } = useAuth();
* if(isSignedIn) {
* return null;
* }
* console.log(sessionId, userId)
* return <div>...</div>
* }
*
* @example
* This page will be fully rendered during SSR:
*
* export HelloPage = () => {
* const { isSignedIn, sessionId, userId } = useAuth();
* console.log(isSignedIn, sessionId, userId)
* return <div>...</div>
* }
*/
export const useAuth: UseAuth = () => {
const { sessionId, userId, actor, orgId, orgRole, orgSlug, orgPermissions } = useStore($authStore);
const getToken: GetToken = useCallback(createGetToken(), []);
const signOut: SignOut = useCallback(createSignOut(), []);
const has = useCallback(
(params: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => {
if (!params?.permission && !params?.role) {
throw new Error(
'Missing parameters. `has` from `useAuth` requires a permission or role key to be passed. Example usage: `has({permission: "org:posts:edit"`',
);
}
if (!orgId || !userId || !orgRole || !orgPermissions) {
return false;
}
if (params.permission) {
return orgPermissions.includes(params.permission);
}
if (params.role) {
return orgRole === params.role;
}
return false;
},
[orgId, orgRole, userId, orgPermissions],
);
if (sessionId === undefined && userId === undefined) {
return {
isLoaded: false,
isSignedIn: undefined,
sessionId,
userId,
actor: undefined,
orgId: undefined,
orgRole: undefined,
orgSlug: undefined,
has: undefined,
signOut,
getToken,
};
}
if (sessionId === null && userId === null) {
return {
isLoaded: true,
isSignedIn: false,
sessionId,
userId,
actor: null,
orgId: null,
orgRole: null,
orgSlug: null,
has: () => false,
signOut,
getToken,
};
}
if (!!sessionId && !!userId && !!orgId && !!orgRole) {
return {
isLoaded: true,
isSignedIn: true,
sessionId,
userId,
actor: actor || null,
orgId,
orgRole,
orgSlug: orgSlug || null,
has,
signOut,
getToken,
};
}
if (!!sessionId && !!userId && !orgId) {
return {
isLoaded: true,
isSignedIn: true,
sessionId,
userId,
actor: actor || null,
orgId: null,
orgRole: null,
orgSlug: null,
has: () => false,
signOut,
getToken,
};
}
throw new Error('Invalid state. Feel free to submit a bug or reach out to support');
};
/**
* This implementation of `useStore` is an alternative solution to the hook exported by nanostores
* Reference: https://github.com/nanostores/react/blob/main/index.js
*/
function useStore<T extends Store, SV extends StoreValue<T>>(store: T): SV {
const get = store.get.bind(store);
return useSyncExternalStore<SV>(store.listen, get, () => {
// Per react docs
/**
* optional getServerSnapshot:
* A function that returns the initial snapshot of the data in the store.
* It will be used only during server rendering and during hydration of server-rendered content on the client.
* The server snapshot must be the same between the client and the server, and is usually serialized and passed from the server to the client.
* If you omit this argument, rendering the component on the server will throw an error.
*/
/**
* When this runs on the server we want to grab the content from the async-local-storage.
*/
if (typeof window === 'undefined') {
return authAsyncStorage.getStore();
}
/**
* When this runs on the client, during hydration, we want to grab the content the store.
*/
return get();
});
}