Integrating with Nuxt.js
This guide explains how to integrate OAuth2 authentication into a Nuxt.js 3 application using AuthAction with @sidebase/nuxt-auth.
Example Repository: For a complete working example, check out our example repository.
Overview
Section titled “Overview”This application showcases how to configure and handle authentication using AuthAction’s OAuth2 service. The setup includes:
- OAuth2 login flow using
@sidebase/nuxt-authwith AuthAction as the OIDC provider - Secure JWT-based session management
- Protected pages using Nuxt route middleware
- Logout with AuthAction’s OIDC logout flow
Prerequisites
Section titled “Prerequisites”Before using this application, ensure you have:
- Node.js 18+: Download from nodejs.org
- AuthAction OAuth2 credentials: You will need the
tenantDomain,clientId,clientSecret, and configured redirect URIs from your AuthAction setup.
Installation
Section titled “Installation”-
Clone the repository:
Terminal window git clone git@github.com:authaction/authaction-nuxtjs-example.gitcd authaction-nuxtjs-example -
Install dependencies:
Terminal window npm install -
Configure your AuthAction credentials:
Create a
.envfile in the root of your project:Terminal window AUTHACTION_TENANT_DOMAIN=your-authaction-tenant-domainAUTHACTION_CLIENT_ID=your-authaction-client-idAUTHACTION_CLIENT_SECRET=your-authaction-client-secretNUXT_SECRET=your-secure-random-stringNUXT_AUTH_ORIGIN=http://localhost:3000 -
Configure redirect URIs in the AuthAction dashboard:
- Login redirect URI:
http://localhost:3000/api/auth/callback/authaction - Logout redirect URI:
http://localhost:3000
- Login redirect URI:
-
Start the development server:
Terminal window npm run devThis will start the application on
http://localhost:3000. -
Testing Authentication:
- Navigate to
http://localhost:3000and click Login with AuthAction. - After login you are redirected back with your name and email shown.
- Navigate to
/dashboard— unauthenticated users are redirected to login. - Click Logout to end the session.
- Navigate to
Code Explanation
Section titled “Code Explanation”Auth Handler
Section titled “Auth Handler”server/api/auth/[...].ts — Configures @sidebase/nuxt-auth with AuthAction as an OIDC provider using the wellKnown discovery endpoint. The jwt callback stores the access token and the session callback exposes it to the client.
Route Middleware
Section titled “Route Middleware”middleware/auth.ts — Redirects unauthenticated users to /login. Applied per-page with definePageMeta({ middleware: 'auth' }).
Home Page
Section titled “Home Page”pages/index.vue — Uses the useAuth() composable to access session state and call signIn('authaction') / signOut().
Protected Page
Section titled “Protected Page”pages/dashboard.vue — Declares middleware: 'auth' so only authenticated users can access it.
Common Issues
Section titled “Common Issues”- Redirects not working: Verify the callback URL in AuthAction matches exactly:
http://localhost:3000/api/auth/callback/authaction - Session issues: Ensure
NUXT_SECRETis set to a long, random string - Network errors: Verify your app can reach
https://{AUTHACTION_TENANT_DOMAIN}/.well-known/openid-configuration