Auth-module: yarn build && yarn start results in "Unknown middleware auth"

Created on 7 Sep 2019  路  2Comments  路  Source: nuxt-community/auth-module

Version

v4.8.2

Reproduction link

https://codesandbox.io/s/codesandbox-nuxt-vb0ki

Steps to reproduce

With a pretty intro-level configuration of the auth module like so (excerpt of nuxt.config.js):

    router: {
        middleware: ['auth'],
    },
    modules: [
        '@nuxtjs/auth',
        // Doc: https://axios.nuxtjs.org/usage
        '@nuxtjs/axios',
    ],
    /*
     ** Auth module configuration
     ** See https://auth.nuxtjs.org/guide/scheme.html
     */
    auth: {
        strategies: {
            auth0: {
                client_id: process.env.AUTH0_CLIENT_ID,
                audience: process.env.AUTH0_AUDIENCE,
                // For some reason, even though this `domain` property should be all we
                // need, it isn't; so I have to add the following manually below:
                domain: process.env.AUTH0_DOMAIN,
                authorization_endpoint: `https://${process.env.AUTH0_DOMAIN}/authorize`,
                userinfo_endpoint: `https://${process.env.AUTH0_DOMAIN}/userinfo`,
            },
        },
        redirect: {
            login: '/',
            logout: '/',
            home: '/',
            callback: '/logged-in',
        },
    },
    /*
     ** Axios module configuration
     ** See https://axios.nuxtjs.org/options
     */
    axios: {},

(Where all my process.env.AUTH0_* env vars are correctly defined.)

What is expected ?

What happens when I run the app via yarn dev--everything works perfectly, namely I shouldn't get that unknown middleware SSR error.

What is actually happening?

Basically when running with yarn dev everything works perfectly, but when running with yarn build and then yarn start (and I mean my app does practically _nothing_ but login/logout, have a homepage, a profile page (for authenticated users only) and a logged-in route that redirects to the homepage), I get the following error from the Nuxt server's SSR:

Screen Shot 2019-09-06 at 4.36.27 PM.png

Again, this error only happens when running from the built universal app server (i.e. yarn build && yarn start) and there are no errors at all and the whole workflow works perfectly with yarn dev.

Additional comments?

Additionally, notice my comment in that excerpt of my nuxt.config.js about the domain not working: For some reason, even though this domain property should be all I need for the login/fetchUser functionality to work with the auth0 strategy, it isn't; so I still had to add the authorization_endpoint and userinfo_endpoint options manually.

This bug report is available on Nuxt community (#c411)
bug

Most helpful comment

Actually, I just double-checked. This isn't a bug. It was happening because I'm using webpack's tree-shaking (i.e. I'm adding sideEffects: true to the webpack rule that processes my JS/TS/etc), and while I had added node_modules to the exclude array, I needed to add .nuxt to it too so the server code in particular won't have its unused imports pruned.

I just double-checked about the domain thing too, and that too was due to this over-zealous tree-shaking.

Goes to show sometimes all you have to do is form the question to get the answer 馃し鈥嶁檪. All the same, could be worth thinking about to make this repo tree-shaking friendly. 鉂わ笍 this module; thanks!

All 2 comments

Actually, I just double-checked. This isn't a bug. It was happening because I'm using webpack's tree-shaking (i.e. I'm adding sideEffects: true to the webpack rule that processes my JS/TS/etc), and while I had added node_modules to the exclude array, I needed to add .nuxt to it too so the server code in particular won't have its unused imports pruned.

I just double-checked about the domain thing too, and that too was due to this over-zealous tree-shaking.

Goes to show sometimes all you have to do is form the question to get the answer 馃し鈥嶁檪. All the same, could be worth thinking about to make this repo tree-shaking friendly. 鉂わ笍 this module; thanks!

i got the same problem, can u teach me the solve, thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DougHayward picture DougHayward  路  4Comments

weijinnx picture weijinnx  路  3Comments

pi0 picture pi0  路  3Comments

dasisyouyu picture dasisyouyu  路  3Comments

ishitatsuyuki picture ishitatsuyuki  路  4Comments