ember-auth icon indicating copy to clipboard operation
ember-auth copied to clipboard

Problem with rememberable

Open martinthogersen opened this issue 11 years ago • 4 comments

Hello,

I'm having a problem with rememberable in my application, when I delete my session on the server, and the "ember-auth-rememberable" cookie still has the old auth_token in it's value.

Here's the error I'm getting on init:

POST http://localhost:3000/api/v1/sessions => 401 (Unauthorized)
Error while loading route: Object {}
Uncaught #<Object> 

How do I fix that? I would like to redirect the user to index.

This is the configuration I'm using:

App.Auth = Ember.Auth.extend
  request: 'jquery'
  response: 'json'
  strategy: 'token'
  tokenKey: 'auth_token'
  tokenIdKey: 'user_id'
  tokenLocation: 'authHeader' 
  tokenHeaderKey: 'Token'
  session: 'cookie'
  signInEndPoint: '/api/v1/sessions'
  signOutEndPoint: '/api/v1/sessions'
  modules: ['authRedirectable', 'actionRedirectable', 'emberData']
  authRedirectable:
    route: 'index'
  rememberable: 
    tokenKey: 'remember_token' 
    period: 14 
    autoRecall: true 
  actionRedirectable:
    signOutRoute: 'index'
  emberData: 
    userModel: 'user'

My SessionsController on the server sends this result back, when the session token is invalid:

render json: { }, status: :unauthorized

martinthogersen avatar Nov 06 '13 13:11 martinthogersen

+1 When remember token is invalid and autoRecall is on it prevents the Ember app from loading.

BFalkner avatar Nov 27 '13 17:11 BFalkner

Did you all ever figure out a way around this? It seems like for me the recall() promise never even gets rejected, as my route error handlers don't pick it up even.

conrad-vanl avatar Dec 13 '13 21:12 conrad-vanl

Not yet. I'm still hoping for a solution for this. Until then you can check if the authtoken is valid by making a call to your server, before initializing ember auth.

martinthogersen avatar Dec 13 '13 21:12 martinthogersen

This is my solution:

Em.Auth.RememberableAuthModule.reopen({
    recall: function (opts) {
        var token,
            self = this;
        if (null == opts)
            opts = {};
        if (!get$(get$(this, 'auth'), 'signedIn') && (token = this.retrieveToken())) {
            set$(this, 'fromRecall', true);
            opts.data || (opts.data = {});
            get$(opts, 'data')[get$(get$(this, 'config'), 'tokenKey')] = token;
            if (null != get$(get$(this, 'config'), 'endPoint')) {
                return get$(this, 'auth').signIn(get$(get$(this, 'config'), 'endPoint'), opts);
            } else {
                return get$(this, 'auth').signIn(opts).catch(function(error){
                    self.recall();
                    // some actions to present error
                });
            }
        } else {
            return new (get$(get$(Em, 'RSVP'), 'resolve'));
        }
    }
});

KirillSuhodolov avatar Jan 13 '14 21:01 KirillSuhodolov