clerk-vue-patterns

作者: clerk

Vue 3 模式搭配 Clerk — 組合式函式(useAuth、useUser、

npx skills add https://github.com/clerk/skills --skill clerk-vue-patterns

Vue Patterns

SDK: @clerk/vue v2+ (Vue 3). For Nuxt, use clerk-nuxt-patterns.

What Do You Need?

TaskReference
Composables: useAuth, useUser, useOrganizationreferences/composables.md
Vue Router navigation guardsreferences/vue-router-guards.md
Pinia store with auth statereferences/pinia-integration.md

Mental Model

Vue uses composables from @clerk/vue:

  • useAuth() — reactive isSignedIn, userId, signOut
  • useUser() — reactive user object
  • useClerk() — full Clerk instance for advanced operations
  • useOrganization() — reactive organization, membership

Setup

Vue (Plain)

// main.ts
import { clerkPlugin } from '@clerk/vue'
import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)
app.use(clerkPlugin, {
  publishableKey: import.meta.env.VITE_CLERK_PUBLISHABLE_KEY,
})
app.mount('#app')

Composables Usage

<script setup lang="ts">
import { useAuth, useUser } from '@clerk/vue'

const { isSignedIn, userId, signOut } = useAuth()
const { user } = useUser()
</script>

<template>
  <div v-if="isSignedIn">
    <p>Hello {{ user?.firstName }}</p>
    <button @click="signOut()">Sign Out</button>
  </div>
  <SignInButton v-else />
</template>

Org Switching

<script setup lang="ts">
import { useOrganizationList } from '@clerk/vue'

const { userMemberships, setActive } = useOrganizationList()
</script>

<template>
  <button
    v-for="mem in userMemberships.data ?? []"
    :key="mem.organization.id"
    @click="setActive({ organization: mem.organization.id })"
  >
    {{ mem.organization.name }}
  </button>
</template>

Common Pitfalls

SymptomCauseFix
Composables return undefinedNot inside ClerkProvider treeEnsure app.use(clerkPlugin, { publishableKey }) is called
userId reactive but not updatingDestructuring loses reactivityUse const { userId } = useAuth() (toRefs-style composable, reactive)

Import Map

WhatImport
Composables@clerk/vue
Plugin setup@clerk/vue
Components@clerk/vue

See Also

  • clerk-setup - Initial Clerk install
  • clerk-custom-ui - Custom flows & appearance
  • clerk-orgs - B2B organizations

Docs

來自 clerk 的更多技能

clerk-monorepo
clerk
在clerk/javascript SDK monorepo中高效工作。適用於設定儲存庫、建置/測試/執行套件、決定使用哪個@clerk/*…
official
mosaic
clerk
Work on Mosaic UI: styling a component with slot recipes (`defineSlotRecipe` / `useRecipe` / slots / variants), or building a flow — authoring a state machine…
official
mosaic-machine
clerk
編寫與使用 Mosaic 狀態機。當使用者正在使用 createMachine 撰寫狀態機、建模多步驟流程、將機器連接至 React 等情境時使用。
official
clerk-android
clerk
Implement Clerk authentication for native Android apps using Kotlin and Jetpack Compose with clerk-android source-guided patterns. Use for prebuilt…
official
clerk-astro-patterns
clerk
使用 Clerk 的 Astro 模式 — 中介軟體、SSR 頁面、島嶼元件、API 路由、靜態 vs SSR 渲染。觸發條件:astro clerk、clerk astro middleware、…
official
clerk-backend-api
clerk
Clerk 後端 REST API 探索工具與執行工具。瀏覽標籤、檢查端點配置結構,並執行已驗證的要求。適用於列出使用者、管理…
official
clerk-chrome-extension-patterns
clerk
使用 @clerk/chrome-extension 進行 Chrome 擴充功能驗證 -- popup/sidepanel 設定、透過 Web 應用程式使用 syncHost 進行 OAuth/SAML、為 service workers 使用 createClerkClient 以及…
official
clerk-cli
clerk
Operate the Clerk CLI (`clerk` binary) for authentication, user/org/session management, impersonation, local webhook testing, deploy verification, instance…
official