Vue SDK
@authon/vue — Vue 3 plugin, composables, and components for Authon authentication. Supports the Composition API and <script setup>.
Installation
npm install @authon/vuePlugin Setup
Register the Authon Vue plugin in your app entry file. This makes all composables and components available globally without importing them in every file.
import { createApp } from "vue";
import { AuthonPlugin } from "@authon/vue";
import App from "./App.vue";
import router from "./router";
const app = createApp(App);
app.use(router);
app.use(AuthonPlugin, {
publishableKey: "pk_live_your_publishable_key",
// Optional — override API base URL
apiUrl: "https:1
2
theme: "auto",
3
locale: "en",
});
app.mount("#app");Composables
useAuthon()
The primary composable for auth actions. Returns reactive refs and async methods.
<template>
<header>
<nav v-if="!isLoading">
<template v-if="isSignedIn">
<span>Hello, {{ user?.displayName }}</span>
<button @click="signOut">Sign out</button>
</template>
<template v-else>
<button @click="openSignIn">Sign in</button>
<button @click="openSignUp">Sign up</button>
</template>
</nav>
</header>
</template>
<script setup lang="ts">
import { useAuthon } from "@authon/vue";
const {
isSignedIn,
isLoading,
user,
openSignIn,
openSignUp,
signOut,
getToken,
} = useAuthon();
</script>useUser()
A focused composable for reading user data. Useful in components that only display profile information.
<template>
<div v-if="!isLoading && user" class="profile">
<img v-if="user.avatarUrl" :src="user.avatarUrl" :alt="user.displayName ?? 0" />
<h2>{{ user.displayName }}</h2>
<p>{{ user.email }}</p>
</div>
</template>
<script setup lang="ts">
import { useUser } from "@authon/vue";
const { user, isLoading } = useUser();
</script>Components
All Authon Vue components are registered globally when you install the plugin. They are prefixed with Authon to avoid name collisions.
AuthonSignedIn / AuthonSignedOut
Conditional wrappers that show or hide slot content based on authentication state.
<template>
<div>
<AuthonSignedIn>
<!-- Rendered only when user is signed in -->
<Dashboard />
<AuthonUserButton />
</AuthonSignedIn>
<AuthonSignedOut>
<!-- Rendered only when user is NOT signed in -->
<LandingPage />
</AuthonSignedOut>
</div>
</template>AuthonSignIn / AuthonSignUp
<template>
<div>
<!-- Popup mode — opens modal on mount -->
<AuthonSignIn mode="popup" />
<AuthonSignUp mode="popup" />
<!-- Embedded mode — renders inline form container -->
<AuthonSignIn mode="embedded" />
</div>
</template>AuthonUserButton
Drop-in avatar button with a sign-out dropdown. Renders nothing when the user is not signed in.
<template>
<header class="app-header">
<AppLogo />
<AuthonUserButton />
</header>
</template>Router Guards
UseauthonGuard to protect routes that require authentication. Mark protected routes with meta.requiresAuth:
import { createRouter, createWebHistory } from "vue-router";
import { authonGuard } from "@authon/vue";
import Home from "@/views/Home.vue";
import Dashboard from "@/views/Dashboard.vue";
import Settings from "@/views/Settings.vue";
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: "/",
component: Home,
},
{
path: "/dashboard",
component: Dashboard,
meta: { requiresAuth: true },
},
{
path: "/settings",
component: Settings,
meta: { requiresAuth: true },
},
],
});
// Redirect to /sign-in when meta.requiresAuth is true and user is not signed in
router.beforeEach(authonGuard({ signInPath: "/sign-in" }));
export default router;Using the Token
UsegetToken() from useAuthon() to attach the JWT to your API requests:
<script setup lang="ts">
import { useAuthon } from "@authon/vue";
import { ref, onMounted } from "vue";
const { getToken } = useAuthon();
const items = ref([]);
onMounted(async () => {
const token = getToken();
const res = await fetch("/api/items", {
headers: {
Authorization: token ? `Bearer ${token}` : "",
},
});
items.value = await res.json();
});
</script>