Auth-module: Merging 'endpoints'

Created on 7 Feb 2018  路  4Comments  路  Source: nuxt-community/auth-module

Currently, _request function in auth.class.js uses Object.assign to merge this.options.endpoints[name] and endpoint.

const opts = Object.assign({}, this.options.endpoints[name], endpoint)

In my use case, I need to add some headers and a propertyName to my request.
Adding these values to nuxt.config.js doesn't work.
Adding them in this.$auth.login({...}) works.

I think that it might be because Object.assign uses shallow merge. I suggest swapping it for deep merge.

This bug report is available on Nuxt.js community (#c45)
wontfix

Most helpful comment

@JesusCrow You set headers in the endpoints, we don't support assigning a headers globally, you can only set headers in a specific endpoint.

All 4 comments

@JesusCrow

endpoints: {
  login: { url: '/api/auth/login', method: 'post', propertyName: 'token', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } },
  logout: { url: '/api/auth/logout', method: 'post' },
  user: { url: '/api/auth/user', method: 'get', propertyName: 'user' }
},

I just tried this on the demo example and it worked.
I think there is no need for shallow merging, but if you feel limited by this, feel free to-reopen.

Maybe I'm doing something wrong than.

  auth: {
    fetchUserOnLogin: false,
    endpoints: {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Basic XXXXXXXXXXXX'
      },
      login: 'api/oauth2/token',
      method: 'post',
      propertyName: 'access_token'
    }
  },

with

    this.$auth.login({
      data: {
        username: 'username',
        password: 'password',
        grant_type: 'password'
      }
    })

fails, while this

    this.$auth.login({
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Basic XXXXXXXXXXXXX'
      },
      data: {
        username: 'username',
        password: 'password',
        grant_type: 'password'
      },
      propertyName: 'access_token'
    })

works

@JesusCrow You set headers in the endpoints, we don't support assigning a headers globally, you can only set headers in a specific endpoint.

Oh......
Sorry, I thought I was writing inside login, and not endpoints! Such a foolish mistake, I'm going to do a seppuku with a :dango:

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AhmedAtef07 picture AhmedAtef07  路  3Comments

manniL picture manniL  路  4Comments

aaronhuisinga picture aaronhuisinga  路  3Comments

dasisyouyu picture dasisyouyu  路  3Comments

essamamdani picture essamamdani  路  3Comments