SDK

Vue SDK

@authon/vueVue 3 plugin, composables, and components for Authon authentication. Supports the Composition API and <script setup>.

npm: @authon/vueVue 3+TypeScript

Installation

bash
npm install @authon/vue

Plugin 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.

src/main.ts
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.

components/Header.vue
<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.

ts
<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.

App.vue
<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

ts
<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.

ts
<template>
  <header class="app-header">
    <AppLogo />
    <AuthonUserButton />
  </header>
</template>

Router Guards

UseauthonGuard to protect routes that require authentication. Mark protected routes with meta.requiresAuth:

src/router/index.ts
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:

ts
<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>
Authon — Universal Authentication Platform