@fusionauth/vue-sdk

中文文档
1.4.0发布于 2026/08/06ApacheTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2231

FusionAuth solves the problem of building essential security without adding risk or distracting from your primary application

原始使用文档
文档目录11 节
  1. Getting Started
  2. Quickstart
  3. Installation
  4. Usage
  5. Configuring the SDK
  6. useFusionAuth composable
  7. UI Components
  8. Documentation
  9. Known issues
  10. Releases
  11. Upgrade Policy

An SDK for using FusionAuth in Vue applications.

Table of Contents

Overview

This SDK provides helpful methods and reactive values that integrate with FusionAuth to help automatically manage authentication state in your Vue app.

Your users will be sent to FusionAuth’s themeable hosted login pages and then log in. After that, they are sent back to your Vue application.

Once authentication succeeds, the following secure, HTTP-only cookies will be set:

  • app.at - an OAuth Access Token

  • app.rt - a Refresh Token used to obtain a new app.at. This cookie will only be set if refresh tokens are enabled on your FusionAuth instance.

The access token can be presented to APIs to authorize the request and the refresh token can be used to get a new access token.

There are 2 ways to interact with this SDK:

  1. By hosting your own server that performs the OAuth token exchange and meets the server code requirements for FusionAuth Web SDKs.
  2. By using the server hosted on your FusionAuth instance, i.e., not writing your own server code.

If you are hosting your own server, see server code requirements.

You can use this library against any version of FusionAuth or any OIDC compliant identity server.

Getting Started

If you are new to Vue development, you may want to start with the Quickstart guide. If you are already familiar with Vue development, skip to the Installation section.

Quickstart

See the FusionAuth Vue Quickstart for a full tutorial on using FusionAuth and Vue.

Installation

NPM:

npm install @fusionauth/vue-sdk

Yarn:

yarn add @fusionauth/vue-sdk

Usage

Configuring the SDK

Configure and initialize the FusionAuthVuePlugin when you create your Vue app:

import { createApp } from 'vue';
import FusionAuthVuePlugin, { type FusionAuthConfig } from '@fusionauth/vue-sdk';

const config: FusionAuthConfig = {
  clientId: "", // Your app's FusionAuth client id
  serverUrl: "", // The url of the server that performs the token exchange
  redirectUri: "", // The URI that the user is directed to after the login/register/logout action
  shouldAutoFetchUserInfo: true, // Automatically fetch userInfo when logged in. Defaults to false.
  shouldAutoRefresh: true, // Enables automatic token refresh. Defaults to false.
  onRedirect: (state?: string) => { }, // Optional callback invoked upon redirect back from login or register.
  // useDpop: true, // Opt-in to DPoP mode. See "DPoP Mode" below. Defaults to false.
}

const app = createApp(App);

app.use(FusionAuthVuePlugin, config);
app.mount('#app')

If you want to use the pre-styled buttons, don't forget to import the css file:

import '@fusionauth/vue-sdk/dist/style.css';

Configuring with Nuxt

If you're using the SDK in a nuxt app, pass the useCookie composable into the config object in your plugin definition.

import { useCookie } from "#app";

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(FusionAuthVuePlugin, {
    ...config
    nuxtUseCookie: useCookie,
  });
});
Alternate ways to define your nuxt plugin

Using createFusionAuth, the SDK can be configured more flexibly.

export default defineNuxtPlugin({
  setup(nuxtApp) {
    const fusionauth = createFusionAuth(config);   
    nuxtApp.vueApp.use(FusionAuthVuePlugin, { instance: fusionauth })
    return {
      provide: { fusionauth }
    };
  },
})

useFusionAuth composable

You can interact with the SDK by using the useFusionAuth, which leverages Vue's Composition API. View the full API documentation

<script setup lang="ts">
import { computed } from 'vue';
import { useFusionAuth } from "@fusionauth/vue-sdk";

const {
  isLoggedIn,
  userInfo,
  isFetchingUserInfo,
  login,
  register,
  logout
} = useFusionAuth();

const welcomeMessage = computed(() => {
  const name = userInfo.value?.given_name
  return name 
    ? 'Welcome!'
    : `Welcome ${userInfo.value.given_name}!`;
});
</script>

<template>
  <p>{{ welcomeMessage }}</p>

  <div v-if="isLoggedIn">
    <p v-if="isFetchingUserInfo">
      Loading...
    </p>
    <button @click="logout()">Logout</button>
  </div>

  <div v-if="!isLoggedIn">
    <button @click="login()">Login</button>
    <p>or</p>
    <button @click="register()">Register</button>
  </div>
</template>

State parameter

The login and register functions accept an optional string parameter: state, which will be passed back to the optional onRedirect callback specified on your FusionAuthConfig. Though you may pass any value you would like for the state parameter, it is often used to indicate which page the user was on before redirecting to login or registration, so that the user can be returned to that location after a successful authentication.

DPoP Mode

By default, the SDK calls a Hosted Backend that stores tokens in HttpOnly cookies (useDpop: false, the default). In DPoP mode, the SDK instead calls FusionAuth endpoints directly and binds tokens to a private key generated in the browser. Enable it by setting useDpop: true on FusionAuthConfig:

const config: FusionAuthConfig = {
  clientId: "",
  redirectUri: "",
  serverUrl: "",
  useDpop: true, // Opt-in to DPoP mode.
  dpopTokenStorage: 'localStorage', // 'localStorage' (default, persists across reloads) or 'memory'.
}

When useDpop: true, useFusionAuth() additionally returns dpopFetch, generateProof, and getAccessToken. These are undefined when useDpop is false or not set.

<script setup lang="ts">
import { useFusionAuth } from "@fusionauth/vue-sdk";

const { dpopFetch, generateProof, getAccessToken } = useFusionAuth();

const response = await dpopFetch('https://api.example.com/data', { method: 'GET' });

const accessToken = getAccessToken();
const proof = await generateProof('https://api.example.com/data', 'GET', accessToken);
// axios.get('https://api.example.com/data', {
//   headers: { Authorization: `DPoP ${accessToken}`, DPoP: proof }
// });
</script>

In DPoP mode, the login/register redirect round trip finishes asynchronously (there's no Hosted Backend to set cookies before the app reloads). onRedirect fires only after isLoggedIn and tokens are fully updated, so it's a reliable place to hook in post-login navigation:

const config: FusionAuthConfig = {
  // ...
  useDpop: true,
  onRedirect: () => router.push('/account'),
}

UI Components

Protecting Content

The RequireAuth and RequireAnonymous can be used to restrict content based on authentication and authorization.

<template>
  <RequireAuth :with-role="['ADMIN', 'SUPER-ADMIN']">
    <!-- only displays for users with specifed role -->
    <button>Delete user</button>
  </RequireAuth>
</template>

<template>
  <RequireAnonymous>
    <!-- content for unauthenticated users -->
  </RequireAnonymous>
</template>

Pre-built buttons

There are three pre-styled buttons that are configured to perform login/logout/registration. They can be placed anywhere in your app as is.

<template>
  <FusionAuthLoginButton />
  <FusionAuthLogoutButton />
  <FusionAuthRegisterButton />
</template>

<style>
  :root {
    --fusionauth-button-background-color: #096324;
    --fusionauth-button-text-color: #fff;
  }
</style>

With the CSS variables, you can customize the buttons to match your app’s style.

Documentation

Full library documentation

These docs are generated with typedoc and configured with typedoc-plugin-markdown.

Use backticks for code in this readme. This readme is included on the FusionAuth website, and backticks show the code in the best light there.

Known issues

Using shouldAutoRefresh with Nuxt may cause the first refresh attempt to fail with 403, if your plugin instance is instantiated on the server. This is likely because the FusionAuth authorization token is httpOnly and not present in the request sent by the server. Subsequent refresh requests queued up by SDK auto-refreshing should succeed.

You may prefer to invoke initAutoRefresh from the app:beforeMount hook, which runs client-side only.

defineNuxtPlugin({
  setup: (nuxtApp) => {
    const fusionauth = createFusionAuth({
      ...config,
      shouldAutoRefresh: false, // is false by default
    });   
    nuxtApp.vueApp.use(FusionAuthVuePlugin, { instance: fusionauth })
    return {
      provide: { fusionauth }
    };
  },
  hooks: {
    "app:beforeMount"() {
      const { $fusionauth } = useNuxtApp();
      $fusionauth.initAutoRefresh();
    },
  }
})

Releases

This package is released via GitHub actions.

Upgrade Policy

This library may periodically receive updates with bug fixes, security patches, tests, code samples, or documentation changes.

These releases may also update dependencies, language engines, and operating systems, as we'll follow the deprecation and sunsetting policies of the underlying technologies that the libraries use.

This means that after a dependency (e.g. language, framework, or operating system) is deprecated by its maintainer, this library will also be deprecated by us, and may eventually be updated to use a newer version.

版本与发布记录

@fusionauth/vue-sdk 共发布 7 个版本,首次发布于 2023/08/31;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.4.0latest2026/08/063 个月前
1.3.02026/06/244 个月前
1.2.02024/06/272 年前
1.1.02024/05/172 年前
1.0.12024/05/092 年前
1.0.02024/04/262 年前

关于 @fusionauth/vue-sdk 的常见问题

@fusionauth/vue-sdk 是什么?

FusionAuth solves the problem of building essential security without adding risk or distracting from your primary application

如何安装 @fusionauth/vue-sdk?

在项目目录运行 npm install @fusionauth/vue-sdk,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(>=3.5.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fusionauth/vue-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fusionauth/vue-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fusionauth/vue-sdk 可以商用吗?

@fusionauth/vue-sdk 使用 Apache 许可。声明的许可为 Apache,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@fusionauth/vue-sdk 的下载量有多少?

根据 npm 官方下载统计,@fusionauth/vue-sdk 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,231 次,日均约 74 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fusionauth/vue-sdk 还在维护吗?

最新版本 1.4.0 发布于 2026/08/06(3 个月前);至今共发布 7 个版本,其中过去 12 个月发布了 2 个;首次发布于 2023/08/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。