Search code examples
vue.jsoauth-2.0nuxt.jsnuxt-auth

How to implement Nuxt Server Middleware Google authorization?


I've got the following auth configuration in my nuxt.config.js

auth: {
  strategies: {
    google: {
      client_id: process.env.GOOGLE_KEY,
      codeChallengeMethod: '',
      scope: ['profile', 'email'],
      responseType: 'token id_token'
    }
  },
  redirect: {
    login: '/login',
    logout: '/logout',
    home: '/',
    callback: '/welcome'
  },
  rewriteRedirects: false
},
router: {
  middleware: ['auth']
},
serverMiddleware: [
  { path: '/db', handler: '~/api/db.js' },
],

This setups frontend authentication, so all my .vue pages are protected. Besides that, I've got some serverMiddleware, like in my api/db.js

const app = require('express')()

app.get('/fields/:schema', async (req, res) => {
  var result = []
  // some logics here
  return result;
})

Request to this resource is not protected by any auth, but I've noticed in Network tab in browser, that all request made by $axios.$get('db/fields/some_schema') from my .vue pages set some Google cookie, like

auth.strategy=google; auth._token.google=Bearer...

which is not used in my serverMiddleware api/db.js

Does Nuxt.js has some out of box way to setup Google authentication for server middleware? What is the right way to setup it?


Solution

  • I had to use this nuxt config

      auth: {
     strategies: {
       google: {
        clientId: 'to be added',
        clientId: '<your cliendID here>',
        codeChallengeMethod: '',
        responseType: 'code',
        endpoints: {
          token: 'http://localhost:8000/social-login/google/',
          userInfo: 'http://localhost:8000/auth/user/'
        },
       },
     }
    },
    

    and implement backend token and userInfo endpoints