samedi 14 avril 2018

Authentification with ember-simple-auth in before model hook

I create a app thet need to implement authentification with email/password on all pages except one page (mobile_messages), where need to authenticate with refresh token. I extend from JWT authenticator and override authenticate method. So it looks like:

authenticate (credentials, headers) {
return new Ember.RSVP.Promise((resolve, reject) => {
  this.makeRequest('/auth/mobile_token', credentials, headers)
    .then((response) => {
      Ember.run(() => {
        try {
          const sessionData = this.handleAuthResponse(response)
          resolve(sessionData)
        } catch (error) {
          reject(error)
        }
      })
    }, (xhr) => {
      Ember.run(() => { reject(xhr.responseJSON || xhr.responseText) })
    })
})

}

On mobile_messages route I try to authenticate in before model hook.

beforeModel (transition) {
const authenticator = 'authenticator:api-token'
return this.get('session').authenticate(authenticator, {api_token: transition.queryParams.api_token}).then(() => {
  }, (reason) => {
  transition.abort()
  this.get('notifications').error('Permission denied.', {
    autoClear: true,
    clearDuration: 6200
  })
})

},

I need to stay on mobile_messages route if authenticate rejected. But when I enter to route with wront token I got next backtrase:

Preparing to transition from '' to 'mobileMessages'
index.js:169 generated -> controller:mobileMessages {fullName: 
"controller:mobileMessages"}
index.js:169 generated -> controller:aut`enter code here`henticated 
{fullName: "controller:authenticated"}
index.js:169 generated -> controller:loading {fullName: 
"controller:loading"}
router.js:300 Intermediate-transitioned into 'authenticated.loading'
index.js:169 generated -> route:messages-faxes {fullName: 
"route:messages-faxes"}
router.js:190 Transitioned into 'login'
jquery.js:9600 POST http://localhost:3000/auth/mobile_token 500 
(Internal Server Error)

It looks like I was redirected before got response from server. An I can't find who is redirect me from route. I try to check ApplicationRouteMixin but i got that sessionInvalidated method calls only if you click logout button. And sessionAuthenticated after success authentification.

If I push to route correct token, then I first redirect to login page and then sessionAuthenticated fires. After that i redirect to baseURL.

Hot to solve issue with redirection to login page?




Aucun commentaire:

Enregistrer un commentaire