Auth-module: Auth multiple logins

Created on 23 Mar 2020  路  8Comments  路  Source: nuxt-community/auth-module

Hi I want multiAuth like simple login and loginAsContributor so how will I suppose to do that?
Please someone tell me will be really thankful.

question

Most helpful comment

Indeed it is supported by scheme/strategy.

In nuxt.config.js you have to define them like this:

  auth: {
    strategies: {
      user: {
        _scheme: 'local',
        endpoints: {
          login: { url: '/api/auth/user/login', method: 'post', propertyName: 'token' },
          logout: { url: '/api/auth/user/logout', method: 'post' },
          user: { url: '/api/auth/user/user', method: 'get', propertyName: 'user' }
        },
      },
      admin: {
        _scheme: 'local',
        login: { url: '/api/auth/admin/login', method: 'post', propertyName: 'token' },
        logout: { url: '/api/auth/admin/logout', method: 'post' },
        user: { url: '/api/auth/admin/user', method: 'get', propertyName: 'user' }
      },

    }
  }

And then you can use this.$auth.loginWith('user', ...) or this.$auth.loginWith('admin', ...)

All 8 comments

HI, @imranr970 :)

I think this is a general nuxt question that you can ask in nuxt or auth module which supports multi-strategy.

Can't you please answer me or suggest me any article or any helping guide?

For sure would be happy to help @imranr970 :) Initially just moved the issue to a more proper place so others can see and help too. nuxt discord is also a friendly place for asking for help.

Would you please explain more about your question? Like what is your current stack for backend? And what authentication provider/flow you prefer to use?

I am using Laravel for backend and using jwt api I want I have multiple login options
login for user and login for admin. separate @nuxtjs/auth endpoints for each user.

Indeed it is supported by scheme/strategy.

In nuxt.config.js you have to define them like this:

  auth: {
    strategies: {
      user: {
        _scheme: 'local',
        endpoints: {
          login: { url: '/api/auth/user/login', method: 'post', propertyName: 'token' },
          logout: { url: '/api/auth/user/logout', method: 'post' },
          user: { url: '/api/auth/user/user', method: 'get', propertyName: 'user' }
        },
      },
      admin: {
        _scheme: 'local',
        login: { url: '/api/auth/admin/login', method: 'post', propertyName: 'token' },
        logout: { url: '/api/auth/admin/logout', method: 'post' },
        user: { url: '/api/auth/admin/user', method: 'get', propertyName: 'user' }
      },

    }
  }

And then you can use this.$auth.loginWith('user', ...) or this.$auth.loginWith('admin', ...)

I am really thankful to you one last question kindly reply this one too please.
How do I redirect each scheme to its specific route on login.
I mean admin login on adminDashboard.vue
userlogin on userLogin.vue

Hi, @imranr970 This is how I do it.
image
I call the redirect method after a successful login

@pi0 Could you please update this answer to work with Nuxt Auth v5.0?

Was this page helpful?
0 / 5 - 0 ratings