1
1
import { resolveAuthState } from '@clerk/shared/authorization' ;
2
+ import { deriveState } from '@clerk/shared/deriveState' ;
2
3
import type {
3
4
CheckAuthorizationWithCustomPermissions ,
4
5
Clerk ,
@@ -83,7 +84,7 @@ type UseAuth = (options?: PendingSessionOptions) => UseAuthReturn;
83
84
* }
84
85
*/
85
86
export const useAuth : UseAuth = ( { treatPendingAsSignedOut } = { } ) => {
86
- const authContext = useStore ( $authStore ) ;
87
+ const authContext = useAuthStore ( ) ;
87
88
const clerkContext = useStore ( $clerkStore ) ;
88
89
89
90
const getToken : GetToken = useCallback ( createGetToken ( ) , [ ] ) ;
@@ -139,14 +140,18 @@ export const useAuth: UseAuth = ({ treatPendingAsSignedOut } = {}) => {
139
140
return payload ;
140
141
} ;
141
142
143
+ function useStore < T extends Store , SV extends StoreValue < T > > ( store : T , getServerSnapshot ?: ( ) => SV ) : SV {
144
+ const get = store . get . bind ( store ) ;
145
+ return useSyncExternalStore < SV > ( store . listen , get , getServerSnapshot || get ) ;
146
+ }
147
+
142
148
/**
143
149
* This implementation of `useStore` is an alternative solution to the hook exported by nanostores
144
150
* Reference: https://github.com/nanostores/react/blob/main/index.js
145
151
*/
146
- function useStore < T extends Store , SV extends StoreValue < T > > ( store : T ) : SV {
147
- const get = store . get . bind ( store ) ;
148
-
149
- return useSyncExternalStore < SV > ( store . listen , get , ( ) => {
152
+ function useAuthStore ( ) {
153
+ const get = $authStore . get . bind ( $authStore ) ;
154
+ return useStore ( $authStore , ( ) => {
150
155
// Per react docs
151
156
/**
152
157
* optional getServerSnapshot:
@@ -160,7 +165,17 @@ function useStore<T extends Store, SV extends StoreValue<T>>(store: T): SV {
160
165
* When this runs on the server we want to grab the content from the async-local-storage.
161
166
*/
162
167
if ( typeof window === 'undefined' ) {
163
- return authAsyncStorage . getStore ( ) ;
168
+ return deriveState (
169
+ false ,
170
+ {
171
+ user : null ,
172
+ session : null ,
173
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
174
+ client : null ! ,
175
+ organization : null ,
176
+ } ,
177
+ authAsyncStorage . getStore ( ) as any ,
178
+ ) ;
164
179
}
165
180
166
181
/**
0 commit comments