After successfully following the Auth0 Vuejs tutorial with Gridsome during development, I encountered a problem when trying to build using gridsome build
. The build failed because window
was undefined in a server context.
I discovered some issues in the Auth0-js library stating that Auth0 should only be used on the client side. However, due to Gridsome's structure, I struggled to find a way to exclusively load Auth0-js on the client side.
In Gridsome, the main.js file is where plugins are added and authentication routing is defined.
Main.js
import AuthServicePlugin from '~/plugins/auth0.plugin'
import auth from '~/auth/auth.service'
export default function (Vue, { router, head, isClient }) {
...
Vue.use(AuthServicePlugin)
// Handle Authentication
router.beforeEach((to, from, next) => {
if (to.path === "/auth/logout" || to.path === "/auth/callback" || auth.isAuthenticated()) {
return next();
}
// Specify the current path as the customState parameter, meaning it
// will be returned to the application after auth
auth.login({ target: to.path });
})
Attempting to follow a Gatsby.js Auth0 implementation tutorial, I tried excluding auth0-js from webpack loading with null-loader
.
gridsome.config.js
configureWebpack: {
/*
* During the build step, `auth0-js` will break because it relies on
* browser-specific APIs. Fortunately, we don’t need it during the build.
* Using Webpack’s null loader, we’re able to effectively ignore `auth0-js`
* during the build. (See `src/utils/auth.js` to see how we prevent this
* from breaking the app.)
*/
module: {
rules: [
{
test: /auth0-js/,
use: 'null-loader',
},
],
},
I am seeking suggestions on how to properly include and load Auth0 exclusively on the client side within the Gridsome framework.