Skip to content

Quickstart: Vue single-page application

This page gets a Vue 3 application built with Vite signing people in through your issuer with oidc-client-ts: a public client with PKCE and no secret, the person’s ID on the page, automatic renewal with the refresh token, and sign-out. It applies Sign users in with OpenID Connect and takes about ten minutes.

  • Node.js 20 or later.
  • A public client on your issuer with none as the token endpoint authentication, the redirect URIs http://localhost:5173/callback and http://localhost:5173/silent-renew.html, and the post-logout redirect URI http://localhost:5173/. See Register a client.
  • The application’s origin allowed for CORS: http://localhost:5173 on the issuer or on the client, and the production origin when you deploy. See Call the issuer from a browser application.
Terminal window
npm create vite@latest authwise-vue -- --template vue-ts
cd authwise-vue
npm install && npm install oidc-client-ts

This page was verified with Vite 8.3, Vue 3.5, and oidc-client-ts 3.5. Delete the scaffold’s components, assets, and style.css, and the style import in src/main.ts, so the files below stand alone.

Create .env with the issuer and the client:

VITE_AUTHWISE_ISSUER=https://login.example.com
VITE_AUTHWISE_CLIENT_ID=CLIENT_ID

Replace https://login.example.com with your issuer’s URL and CLIENT_ID with the client’s ID. Vite inlines these values at build time.

Pin the port in vite.config.ts, because the redirect URIs are registered for port 5173 exactly:

import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [vue()],
// The redirect URIs are registered for http://localhost:5173 exactly;
// fail instead of falling back to another port.
server: { port: 5173, strictPort: true },
})

Create src/auth.ts:

import { UserManager } from 'oidc-client-ts'
const origin = window.location.origin
export const userManager = new UserManager({
authority: import.meta.env.VITE_AUTHWISE_ISSUER,
client_id: import.meta.env.VITE_AUTHWISE_CLIENT_ID,
redirect_uri: `${origin}/callback`,
post_logout_redirect_uri: `${origin}/`,
silent_redirect_uri: `${origin}/silent-renew.html`,
response_type: 'code',
scope: 'openid profile email offline_access',
automaticSilentRenew: true,
})

offline_access asks for a refresh token, and automaticSilentRenew uses it to renew the access token a minute before it expires, without any navigation. The silent-redirect page is the library’s fallback when no refresh token is present; see Good to know for why it stays a fallback. Keep the UserManager in its own module so a hot update of a component does not create a second one with its own timer.

Replace src/App.vue:

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import type { User } from 'oidc-client-ts'
import { userManager } from './auth'
// Finish the redirect exactly once per page load (a module-level promise
// survives HMR re-running this setup), then put the URL back to "/".
const initialUser: Promise<User | null> =
window.location.pathname === '/callback'
? userManager.signinCallback().then((user) => {
window.history.replaceState(null, '', '/')
return user ?? null
})
: userManager.getUser()
const user = ref<User | null>(null)
const loading = ref(true)
const error = ref<string>()
const setUser = (u: User | null) => { user.value = u }
const setError = (e: unknown) => { error.value = String(e) }
const onUnloaded = () => setUser(null)
onMounted(() => {
initialUser.then(setUser, setError).finally(() => { loading.value = false })
// Fired after every sign-in and every renewal, automatic or manual.
userManager.events.addUserLoaded(setUser)
userManager.events.addUserUnloaded(onUnloaded)
})
onUnmounted(() => {
userManager.events.removeUserLoaded(setUser)
userManager.events.removeUserUnloaded(onUnloaded)
})
const signIn = () => userManager.signinRedirect()
const renew = () => userManager.signinSilent().then(setUser, setError)
const signOut = () => userManager.signoutRedirect()
const expiresAt = (u: User) => new Date(u.expires_at! * 1000).toISOString()
</script>
<template>
<p v-if="loading">Loading…</p>
<main v-else-if="!user">
<button @click="signIn">Sign in</button>
<p v-if="error" role="alert">{{ error }}</p>
</main>
<main v-else>
<p>Signed in as <code id="sub">{{ user.profile.sub }}</code></p>
<p>Access token expires at <time id="expires">{{ expiresAt(user) }}</time></p>
<button @click="renew">Renew now</button>
<button @click="signOut">Sign out</button>
<p v-if="error" role="alert">{{ error }}</p>
</main>
</template>

Add public/silent-renew.html, the static page the fallback loads in a hidden iframe:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Silent renew</title>
</head>
<body>
<!--
Loaded in a hidden iframe by oidc-client-ts when it renews without a
refresh token (prompt=none). It must be a plain page, not a route of the
Vue app: all it does is hand the callback URL back to the opener.
Pin the same version as package.json.
-->
<script src="https://cdn.jsdelivr.net/npm/oidc-client-ts@3.5.0/dist/browser/oidc-client-ts.min.js"></script>
<script>
new oidc.UserManager({}).signinSilentCallback()
</script>
</body>
</html>
Terminal window
npm run dev

Open http://localhost:5173/, click Sign in, and sign in on the hosted page. The app shows Signed in as u-… and the access token’s expiry. Renew now moves the expiry forward with one request to the token endpoint and no page change. Sign out ends the issuer session and returns to http://localhost:5173/.

  • Renewal is the refresh token. The issuer’s session cookie is SameSite=Lax, so a hidden iframe on your application’s origin cannot carry it, and a prompt=none request from that iframe answers login_required. With offline_access the library renews with the refresh token and never opens the iframe. Keep the scope.
  • Tokens live in sessionStorage, per tab, under oidc.user:AUTHORITY:CLIENT_ID. To share a sign-in across tabs, pass userStore: new WebStorageStateStore({ store: localStorage }).
  • Refresh tokens rotate on every renewal. The library stores the newest one for you; a reused one is refused.
  • signinCallback() consumes the state once. The sample keeps its promise at module level in <script setup>, so a hot update that re-runs the component does not call it twice.
  • The silent-renew page must be a plain static file at a registered redirect URI, not a route of the Vue app. In Vite, files under public/ cannot import from src/, which is why it loads the pinned browser bundle.
  • To type-check, run npm run build or npx vue-tsc -b. The scaffold’s root tsconfig.json only references the app and node configs, so vue-tsc --noEmit at the root checks nothing.
  • Listen to events.addSilentRenewError in a real application and start an interactive sign-in when it fires.
  • Send user.access_token as a bearer token to the API the client’s audience names, and validate it there against the issuer’s JWKS.
  • Read Keep the session alive for the refresh token lifetimes and how to tune them.