vendredi 24 février 2017

How to auto redirect after authenticate using ember-simple-auth

I am using JWT (JSON Web Token) to authenticate user. I am using ApplicationRouteMixin in application.js route which supports to handle two events (success login and success logout) by default.

Now when I login with a user credential using code below,

  login(credential) {
    const authenticator = 'authenticator:jwt';
    const session = this.get('session');

    session.authenticate(authenticator, credential)
      .catch(() => {
        const errorMessage = "Wrong email or password. Please try again!";
        this.set('errorMessage', errorMessage);
      });
  },

The URL stay the same even the response is success which includes the valid JWT.

{"jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODg1NDIyMTcsInN1YiI6Mn0.Ma0ZtLWhoO1mb5cluGJZcKuHb-J2ZJdZOd3AFhVI258"}

I have to refresh the page manually to get the page redirect. However, when I am in the protected route. The auto redirect is working for this.get('session').invalidate().

I know I can just add a then after authenticate method (see code below) to redirect the URL to the right one, but after browse so many examples I see no one do this.

    session.authenticate(authenticator, credential)
      .then(() => {
        this.transitionTo('browse');
      })
      .catch(() => {
        const errorMessage = "Wrong email or password. Please try again!";
        this.set('errorMessage', errorMessage);
      });

I am missing anything here?




Aucun commentaire:

Enregistrer un commentaire