Skip to main content

Vue 3 Authentication & Route Guards System

Build a complete authentication system for Vue 3 — JWT management, route guards, role-based access, and session handling.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Build a complete authentication system for a Vue 3 + Vue Router 4 application. Include: 1) Auth store (Pinia) with login, logout, register, refresh token actions, 2) JWT management — access + refresh token with auto-refresh before expiry, 3) Axios/fetch interceptor for automatic Authorization header injection, 4) Route guards with beforeEach — redirect unauthenticated users to login, 5) Role-based route access: admin, manager, editor, viewer with v-permission directive for UI elements, 6) Persistent auth state across page refresh using httpOnly cookies (preferred) or secure localStorage, 7) OAuth social login integration with Google and GitHub, 8) Session timeout detection with warning modal at 5 minutes before expiry, 9) Auth composable: useAuth() with isAuthenticated, user, hasRole(), login(), logout(), 10) Protected API call wrapper that handles 401 → refresh → retry automatically. Include TypeScript types, route meta typing, and tests.

What this prompt does

This prompt asks the AI to build a complete authentication system for a Vue 3 app using [router_version]. It centers on a Pinia auth store with login, logout, register, and refresh actions; JWT handling with access plus refresh tokens that auto-refresh before expiry; and an Axios or fetch interceptor that injects the Authorization header automatically. Route guards via beforeEach redirect unauthenticated users to login, and role-based access for [roles] is enforced with a v-permission directive on UI elements.

The structure works because authentication breaks in the gaps — the silent token refresh, the 401 retry, the persisted state across reload — and this prompt addresses each one explicitly. Persistent auth via [storage_strategy], OAuth login with [oauth_providers], a session-timeout warning at [timeout_warning], a useAuth() composable, and a protected API wrapper that does 401-then-refresh-then-retry automatically turn a login screen into a real session system, with TypeScript types and tests. Centralizing everything in a Pinia store keeps auth state in one place rather than scattered across components, so the route guards, the interceptor, and the v-permission directive all read from a single source of truth and stay consistent as the app grows.

When to use it

  • You are standing up auth for a Vue 3 app that needs to be genuinely secure, not just a login form.
  • You need access plus refresh tokens with automatic refresh before expiry.
  • You want route guards redirecting unauthenticated users and role gating for [roles].
  • You need auth state to survive page reload via [storage_strategy].
  • You are adding OAuth social login with [oauth_providers].
  • You want a useAuth() composable and a 401-refresh-retry API wrapper so sessions feel seamless.

Example output

Expect a Pinia auth store, JWT access/refresh handling with auto-refresh, a request interceptor injecting Authorization, beforeEach route guards, a v-permission directive for [roles], persistent auth via [storage_strategy], OAuth integration for [oauth_providers], a session-timeout warning at [timeout_warning], a useAuth() composable (isAuthenticated, hasRole, login, logout), and a protected API wrapper with automatic 401 refresh-and-retry, plus types and tests.

Pro tips

  • Prefer httpOnly cookies for [storage_strategy] where your backend supports them; localStorage is simpler but more exposed to XSS.
  • Make sure the auto-refresh fires before expiry, not on the 401 — proactive refresh avoids a failed request flicker.
  • Define [roles] to match your backend's actual role names so v-permission checks line up with server authorization.
  • Test the 401-refresh-retry path under concurrent requests; multiple simultaneous 401s can trigger duplicate refresh calls without a lock.
  • Wire [oauth_providers] callbacks carefully — redirect URIs and state handling are where social login most often breaks.
  • Treat the [timeout_warning] modal as a real UX moment; give users a clear way to extend the session before it expires.

Frequently Asked Questions

Where should tokens be stored?
The `[storage_strategy]` variable lets you choose, and httpOnly cookies are preferred where your backend supports them because they are not readable by JavaScript. Secure localStorage is simpler to implement but more exposed to XSS, so weigh that tradeoff for your threat model.
How does the silent token refresh work?
The system uses access and refresh tokens, refreshing the access token before it expires rather than waiting for a 401. The protected API wrapper also catches 401s, refreshes, and retries automatically, so users rarely see a failed request from an expired session.
Does it handle role-based access in the UI?
Yes. Role-based route access covers your `[roles]`, and a v-permission directive hides or shows UI elements based on the user's role. Match `[roles]` to your backend's actual role names so client-side checks line up with server-side authorization.
Can multiple simultaneous requests cause duplicate refresh calls?
They can, if several requests hit a 401 at once without coordination. Test the refresh-retry path under concurrency and ensure a single refresh is shared via a lock or in-flight promise, otherwise you may fire redundant refresh calls and race conditions.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Vue.js & Angular Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support