diff --git a/.changeset/shy-onions-happen.md b/.changeset/shy-onions-happen.md new file mode 100644 index 00000000000..6d97f0b781f --- /dev/null +++ b/.changeset/shy-onions-happen.md @@ -0,0 +1,9 @@ +--- +'@clerk/astro': minor +'@clerk/clerk-react': minor +'@clerk/types': minor +'@clerk/vue': minor +--- + +Update `has` from `useAuth()` to always be a function. +Previously `has` would be `undefined` when `isLoaded:false`, now it will be a function that always returns false. diff --git a/packages/astro/src/react/hooks.ts b/packages/astro/src/react/hooks.ts index d931c197378..46dfcfb63aa 100644 --- a/packages/astro/src/react/hooks.ts +++ b/packages/astro/src/react/hooks.ts @@ -1,11 +1,4 @@ -import type { - ActJWTClaim, - CheckAuthorizationWithCustomPermissions, - Clerk, - GetToken, - OrganizationCustomRoleKey, - SignOut, -} from '@clerk/types'; +import type { CheckAuthorizationWithCustomPermissions, Clerk, GetToken, SignOut, UseAuthReturn } from '@clerk/types'; import type { Store, StoreValue } from 'nanostores'; import { useCallback, useSyncExternalStore } from 'react'; @@ -14,9 +7,6 @@ import { authAsyncStorage } from '#async-local-storage'; import { $authStore } from '../stores/external'; import { $clerk, $csrState } from '../stores/internal'; -type CheckAuthorizationSignedOut = undefined; -type CheckAuthorizationWithoutOrgOrUser = (params?: Parameters[0]) => false; - /** * @internal */ @@ -53,60 +43,6 @@ const createSignOut = () => { }; }; -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; /** @@ -178,7 +114,7 @@ export const useAuth: UseAuth = () => { orgId: undefined, orgRole: undefined, orgSlug: undefined, - has: undefined, + has: () => false, signOut, getToken, }; diff --git a/packages/react/src/hooks/useAuth.ts b/packages/react/src/hooks/useAuth.ts index 3f1d00ffd93..bdd4b29eb1c 100644 --- a/packages/react/src/hooks/useAuth.ts +++ b/packages/react/src/hooks/useAuth.ts @@ -151,7 +151,7 @@ export function useDerivedAuth(authObject: any): UseAuthReturn { orgId: undefined, orgRole: undefined, orgSlug: undefined, - has: undefined, + has: () => false, signOut, getToken, }; diff --git a/packages/types/src/hooks.ts b/packages/types/src/hooks.ts index 3d1a3d9f04f..de6297a51c2 100644 --- a/packages/types/src/hooks.ts +++ b/packages/types/src/hooks.ts @@ -12,8 +12,7 @@ import type { import type { SignUpResource } from './signUp'; import type { UserResource } from './user'; -type CheckAuthorizationSignedOut = undefined; -type CheckAuthorizationWithoutOrgOrUser = (params: Parameters[0]) => false; +type CheckAuthorizationWithoutUser = (params: Parameters[0]) => false; /** * Return values of the `useAuth()` hook @@ -53,7 +52,7 @@ export type UseAuthReturn = /** * A function that checks if the user has specific permissions or roles. See the [reference doc](https://clerk.com/docs/references/backend/types/auth-object#has). */ - has: CheckAuthorizationSignedOut; + has: CheckAuthorizationWithoutUser; /** * A function that signs out the current user. Returns a promise that resolves when complete. See the [reference doc](https://clerk.com/docs/references/javascript/clerk/clerk#sign-out). */ @@ -72,7 +71,7 @@ export type UseAuthReturn = orgId: null; orgRole: null; orgSlug: null; - has: CheckAuthorizationWithoutOrgOrUser; + has: CheckAuthorizationWithoutUser; signOut: SignOut; getToken: GetToken; } diff --git a/packages/vue/src/composables/useAuth.ts b/packages/vue/src/composables/useAuth.ts index 1fe578e0c22..b402035fdc3 100644 --- a/packages/vue/src/composables/useAuth.ts +++ b/packages/vue/src/composables/useAuth.ts @@ -110,7 +110,7 @@ export const useAuth: UseAuth = () => { orgId: undefined, orgRole: undefined, orgSlug: undefined, - has: undefined, + has: () => false, signOut, getToken, }; @@ -158,7 +158,7 @@ export const useAuth: UseAuth = () => { orgId: null, orgRole: null, orgSlug: null, - has: () => false, + has, signOut, getToken, };