nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 924 forks source link

use token to login instead of username and password #1784

Closed duckimann closed 1 year ago

duckimann commented 2 years ago

I currently have this setup

export default {
  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // 'bootstrap-vue/nuxt',
    "@nuxtjs/i18n",
    "@nuxtjs/axios",
    "@nuxtjs/proxy",
    "@nuxtjs/auth-next",
  ],

  router: {
    middleware: ['auth']
  },

  auth: {
    strategies: {
      facebook: {
        clientId: '...',
        endpoints: {
          token: "/api/facebook/"
        }
      },
      google: {
        clientId: process.env.GOOGLE_AUTH_CLIENT_ID,
        codeChallengeMethod: "",
        responseType: 'code',
        endpoints: {
          token: '/api/google/',
          userInfo: '/api/user/'
        }
      },
    }
  },

  axios: {
    proxy: true,
  },
  proxy: {
    "/api/": {
      target: process.env.BASE_API_URL || 'http://localhost/',
      pathRewrite: { '^/api/': '/' },
    },
  },
}

and now i want to send the token from facebook/google to my backend, validate the token then returns my backend's token, and set it as primary. Basically like local strategy but use token from those 2 services instead of email/password combination.

How should i do it?

Note: I've done with my backend, it does accept token from those 2 services.

trandaison commented 1 year ago

Setup a callback URL for your facebook app. Facebook will redirect to your callback URL with a token, then make an api call with that token to your server with axios, after that use setTokens, setUser, setStrategy methods to login.

The library already supported this!

duckimann commented 1 year ago

I know that already. The thing i wanted is i just need to put a url in then the lib handle the rest (fetch, set token, etc). But thanks.