Skip to content

Commit

Permalink
fix(vue): Fix getToken() hanging when Clerk is already loaded (#4837)
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano authored Jan 6, 2025
1 parent 3e5250e commit 632bde1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-wolves-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/vue": patch
---

Fix an issue where `getToken()` from `useAuth()` composable returns an empty value
21 changes: 12 additions & 9 deletions packages/vue/src/composables/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import { useClerkContext } from './useClerkContext';
*/
function clerkLoaded(clerk: ShallowRef<Clerk | null>) {
return new Promise<Clerk>(resolve => {
const unwatch = watch(clerk, value => {
if (value?.loaded) {
unwatch();
resolve(clerk.value!);
}
});
watch(
clerk,
value => {
if (value?.loaded) {
resolve(value);
}
},
{ immediate: true },
);
});
}

Expand Down Expand Up @@ -73,12 +76,12 @@ type UseAuth = () => ToComputedRefs<UseAuthReturn>;
export const useAuth: UseAuth = () => {
const { clerk, authCtx } = useClerkContext();

const getToken: GetToken = createGetToken(clerk);
const signOut: SignOut = createSignOut(clerk);

const result = computed<UseAuthReturn>(() => {
const { sessionId, userId, actor, orgId, orgRole, orgSlug, orgPermissions } = authCtx.value;

const getToken: GetToken = createGetToken(clerk);
const signOut: SignOut = createSignOut(clerk);

const has = (params: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => {
if (!params?.permission && !params?.role) {
return errorThrower.throw(useAuthHasRequiresRoleOrPermission);
Expand Down

0 comments on commit 632bde1

Please sign in to comment.