Druxt Authentication Module
DruxtAuth is a Nuxt.js module designed to streamline authentication for Druxt sites by integrating with Drupal's Simple OAuth2 module and the popular `@nuxtjs/auth` module. It abstracts away much of the boilerplate required for connecting a Nuxt frontend to a Drupal backend using OAuth2. The current stable version is 0.4.0, indicating a pre-1.0 release, with updates and patches released as needed. Its key differentiator is its specific focus on the Druxt ecosystem, providing pre-configured strategies for both Drupal Authorization Code and Password grants, significantly simplifying the setup process for developers building headless Drupal applications with Nuxt. It extends the core functionalities of `@nuxtjs/auth` by providing Drupal-specific endpoints and configurations, making it a go-to solution for authentication within the Druxt project framework.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'loginWith')
cause The `$auth` plugin is not properly initialized or accessed outside a Nuxt/Vue component context, or `druxt-auth` module is not correctly registered in `nuxt.config.js`.fixEnsure `druxt-auth` is listed in `buildModules` in `nuxt.config.js` and `$auth` is accessed within a Vue component method or Nuxt context (e.g., `this.$auth` or `context.$auth`). -
OAuth Error: invalid_client / Invalid client credentials.
cause The `clientId` or `clientSecret` provided in `nuxt.config.js` does not match the configured Drupal OAuth consumer, or the consumer itself is misconfigured.fixVerify that `clientId` and `clientSecret` (if used) are exact matches to the Drupal OAuth consumer settings. Check the Drupal consumer's UUID and secret, and ensure it's enabled. -
OAuth Error: unauthorized_client / The client is not authorized to request an authorization code using this method.
cause The Drupal OAuth consumer is not configured for the requested grant type (e.g., trying to use password grant on a consumer configured only for authorization code, or missing PKCE for authorization code).fixReview your Drupal OAuth consumer settings. For 'Authorization Code' grant, ensure 'Use PKCE?' is checked and 'Is Confidential' is unchecked. For 'Password' grant, ensure 'Is Confidential' is checked and a 'New Secret' is provided.
Warnings
- breaking The `clientId` option is mandatory for `druxt-auth` to function correctly. Without it, the module cannot identify the Drupal OAuth consumer.
- gotcha When using the `drupal-password` grant, the `clientSecret` option is required and your Nuxt application must be running in SSR (Server-Side Rendering) mode. Client-side-only applications cannot securely use the password grant type with a confidential client.
- gotcha Incorrect configuration of the Drupal Simple OAuth module (e.g., wrong grant types, redirect URIs, or PKCE settings) will lead to authentication failures.
- gotcha The `druxt-auth` module depends on the `@nuxtjs/auth` module. While `druxt-auth` configures it, understanding the underlying `@nuxtjs/auth` documentation can be crucial for advanced use cases or troubleshooting.
Install
-
npm install druxt-auth -
yarn add druxt-auth -
pnpm add druxt-auth
Imports
- module registration
import DruxtAuth from 'druxt-auth'
export default { buildModules: [ ['druxt-auth', { clientId: '...' }] ] } - $auth plugin
import { $auth } from 'druxt-auth'this.$auth.loginWith('drupal-authorization_code') - Drupal Strategies
this.$auth.loginWith('oauth2')this.$auth.loginWith('drupal-password', { data: { username: 'user', password: 'pwd' } })
Quickstart
module.exports = {
// Required for Druxt
modules: [
'druxt'
],
buildModules: [
['druxt-auth', {
clientId: process.env.DRUPAL_CONSUMER_UUID ?? 'YOUR_DRUPAL_CONSUMER_UUID',
clientSecret: process.env.DRUPAL_CONSUMER_SECRET ?? '', // Required for password grant, keep empty for authorization code
scope: ['default'],
}]
],
druxt: {
baseUrl: process.env.DRUPAL_BASE_URL ?? 'https://demo-api.druxtjs.org'
},
// For password grant, Nuxt must be in SSR mode.
// This setting is typically in a server-side context or specific environment configuration.
// Example of usage in a Vue component (e.g., pages/login.vue)
// <template>
// <button @click="login">Login with Authorization Code</button>
// </template>
// <script>
// export default {
// methods: {
// async login() {
// try {
// await this.$auth.loginWith('drupal-authorization_code');
// console.log('Initiated Authorization Code Flow');
// } catch (error) {
// console.error('Login failed:', error);
// }
// }
// }
// }
// </script>
}