Skip to content

Add a login flow

Turn the quickstart's already-configured OAuth setup into a real, working login.

If you completed Getting started, you already have a working authentication setup: you just haven't used it yet. This tutorial logs a real user in through it, so you can see the whole flow work before building on it.

Prerequisites

  • The Getting started tutorial, completed and still running (npm run dev in your project root).

Step 1: See what's already there

Open nuxt/nuxt.config.js in your project and look at modules:

modules: [
  ['druxt-auth', { clientId: process.env.OAUTH_CLIENT_ID }],
  'druxt-site',
],

The druxt-auth module is already installed and configured: npm run setup (back in Getting started) provisioned a Drupal OAuth Consumer (Drupal's entity for one registered OAuth client) for you and wrote its ID to OAUTH_CLIENT_ID in .env. There's also already a page at nuxt/pages/user/login.vue that starts the login flow:

export default {
  created() {
    this.$auth.loginWith('drupal-authorization_code');
  },
};

Outcome: you've confirmed there's nothing left to install: the frontend is already wired for login, it just doesn't have a visible way to trigger it yet. This tutorial adds that visible trigger.

Step 2: Make sure you have an active Drupal session

The OAuth flow needs you to already be logged into Drupal, so it has someone to ask "grant access to this app?". Run:

npm run login

Outcome: your browser opens a Drupal admin session, same as in Getting started. Leave that tab open (or just remember you're logged in) and go back to the frontend.

Step 3: Trigger the login

With the frontend running, open http://localhost:3000/user/login.

Outcome: the page briefly shows "Redirecting..." then sends you to Drupal's OAuth authorization screen, asking you to approve access for the "Druxt" consumer. Approve it. You're redirected back to http://localhost:3000/callback, then on to the frontend homepage, now authenticated.

Step 4: Confirm you're actually logged in

Nothing on the page visibly changes yet. There's no login state shown in the UI. Open your browser's DevTools, go to Application → Local Storage (Chrome) or Storage → Local Storage (Firefox) for localhost:3000, and look for keys starting with auth..

Outcome: you'll see auth.strategy set to drupal-authorization_code and an auth._token.drupal-authorization_code entry holding your access token. That's Nuxt's auth module confirming you're logged in.

Step 5: Log in as an editor

A login that works for the administrator can still fail for everyone else, because the administrator bypasses every permission check. Create an ordinary account named editor, with no extra role. Then give the two permissions a login needs to Drupal's authenticated role, which every signed-in user has:

npm run drush -- user:create editor --password=editor
npm run drush -- role:perm:add authenticated 'grant simple_oauth codes,access druxt resources'

grant simple_oauth codes lets the editor approve the consent screen. access druxt resources lets them read what anonymous visitors can, since the authenticated role doesn't inherit anonymous permissions. Running role:perm:add on a site that already grants both is harmless.

Now log in as the editor. Your browser is still signed in to Drupal as the administrator, so use a private window, which starts signed out:

  1. In a private window, open http://localhost:3000/user/login.
  2. Drupal asks you to log in first. Use editor, password editor.

Outcome: Drupal shows the editor the same authorization (consent) screen as in Step 3. After approving it, you're back on the frontend, logged in as the editor. If approving shows "The 'grant simple_oauth codes' permission is required.", the role is missing that permission.

The frontend's account menu comes from Drupal, so once you're logged in it shows "My account" and "Log out". They link to /user and /user/logout. The quickstart has no pages at those paths yet, so both show a 404. To add a logout page, see Logging out in the authentication how-to.

What you've got

A real, working OAuth 2.0 Authorization Code (with PKCE) login flow, backed by Drupal's Simple OAuth module. No passwords touch the frontend at any point.

Piece What it is
Drupal Consumer (provisioned by npm run setup) A public OAuth client with PKCE enabled, redirecting to /callback
druxt-auth module Wires @nuxtjs/auth-next up with Drupal-specific OAuth strategies
pages/user/login.vue Starts the flow
/callback route Added automatically by druxt-auth; completes the token exchange

From any component, this.$auth.loggedIn and this.$auth.user are now usable: user is populated from Drupal's /oauth/userinfo endpoint. A natural next step is adding a login/logout link to your layout using those, rather than requiring people to know the /user/login URL by heart. druxt-auth 0.5 also adds a /user/login page that renders its <DruxtAuthLogin /> sign-in form, and you can place that form in any page or layout. The quickstart's own pages/user/login.vue replaces the module's page.

Known limitations

Current as of druxt-auth 0.5.1:

  • Sessions renew silently, with three caveats. The scheme refreshes an expired access token automatically on any request, and server rendering restores cold loads from the cookie-held tokens. The real limits: an expired access token alone never logs anyone out (autoLogout defaults off), a custom druxt.axios instance bypasses the automatic refresh (the how-to covers that case), and auth-next assumes a 30-day refresh token while the consumer default is 14, so a long-idle session can still end mid-request.
  • Logging out ends only the frontend session. this.$auth.logout() leaves the tokens valid until they expire, because Simple OAuth has no revocation endpoint without a patch. The how-to covers a logout page and the revocation patch.