简体   繁体   中英

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?

Ember Simple Auth uses Mixins to determine the route transition behavior that should happen if a user is authenticated/unauthenticated.

For example, this mixin will not allow the user to stay on the route if they are unauthenticated:

import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
  // route code
});

What you probably want to use is the UnauthenticatedRouteMixin

This mixin is used to make routes accessible only if the session is not authenticated (eg, login and registration routes). It defines a beforeModel method that aborts the current transition and instead transitions to the routeIfAlreadyAuthenticated if the session is authenticated.

Include UnauthenticatedRouteMixin in your routes, which needs to accessed if the session is not validated. For example:

// app/routes/login.js
import UnauthenticatedRouteMixin from 'ember-simple- 
auth/mixins/unauthenticated-route-mixin';

export default Ember.Route.extend(UnauthenticatedRouteMixin);

It was an error with loading hook. I make an error with naming routes. I created route with name loading to redirect to messages-faxes route. In this case when before model hook return promise ember generate route:application_loading. In application_loading route I run transition to messages-faxes route which has UnauthenticatedRouteMixin. This mixin see that user is not Authenticated and redirect to loading page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM