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 的更多技能

mosaic
clerk
Work on Mosaic UI: styling a component with slot recipes (`defineSlotRecipe` / `useRecipe` / slots / variants), or building a flow — authoring a state machine…
clerk-billing
clerk
用于订阅管理的 Clerk Billing - 渲染 Clerk 的 PricingTable
clerk-nextjs-patterns
clerk
用于身份验证、中间件、服务器操作以及使用Clerk进行用户范围缓存的高级Next.js模式。区分服务器端await auth()与客户端useAuth()钩子;混合使用是常见的破坏性错误。涵盖中间件策略(公共优先与受保护优先)、API路由保护以及正确的HTTP状态码(401与403)。包括使用unstable_cache进行用户范围缓存模式以及保护服务器操作免受未授权修改。提供Core 2兼容性...
clerk-expo-patterns
clerk
使用 Clerk 的 Expo / React Native 模式 — SecureStore 令牌缓存、OAuth
changesets
clerk
Create or refresh a `.changeset/<slug>.md` for the current branch, or report that none is required. Triggers on "/changesets create", "add a changeset",…
clerk
clerk
clerk 二进制文件是通往 Clerk 后端 API 和平台 API 的预认证网关,同时提供项目级工具(认证、链接、环境变量拉取、实例配置)。当用户询问任何涉及 Clerk 资源的问题时,请优先使用 clerk,而不是手动编写 curl 命令。
clerk-cli
clerk
clerk 二进制文件是通往 Clerk 后端 API 和平台 API 的预认证网关,同时提供项目级工具(身份验证、链接、环境拉取、实例配置)。当用户询问任何涉及 Clerk 资源的问题时,请优先使用 clerk,而不是手动编写 curl 命令。
clerk-astro-patterns
clerk
使用 Clerk 的 Astro 模式——中间件、SSR 页面、岛屿组件