1

I want to perform some code when ember-simple-auth session get authenticated.

routes/application.js

import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin, {
    currentUser: Ember.inject.service('current-user'),
    ajax: Ember.inject.service('ajax'),

    actions: {
        authSuccess: function() {
            var self = this;
            self.get('ajax').request({
                url: "http://prod-drunkedguru.rhcloud.com/rest/users/me",
                method: "GET"
            }, function(response) {
                Ember.$('#loginWindow').modal('hide');
                self.get('currentUser').setUser(response);
            }, function(xhr, status, error) {
                if (status === "Incorrect credentials") {
                    self.set('hasError', true);
                }
            });

        }
    },

    sessionAuthenticated: function() {
        console.log("auth success!");
    },

    sessionInvalidated: function() {
        console.log("401");
    }
});

autheticators/digest.js

import Base from 'ember-simple-auth/authenticators/base';
import Ember from 'ember';
import CryptoJS from 'npm:crypto-js';

export default Base.extend({

    restore: function(data) {
        if (data.email && data.password) {
            return new Ember.RSVP.Promise(function(resolve, reject) {
                resolve({email: data.email, password: data.password, digests: {}});
            });
        }
    },

    authenticate: function(email, password) {
        var self = this;
        return new Ember.RSVP.Promise(function(resolve, reject) {
            Ember.run(function() {
                resolve({email: email, password: password, digests: {}});
            });
        });
    },

    invalidate: function(data) {
        //
    }
});

restore() function successfully called on application start and resolve() in it called too. I expect "auth success!" string in console, but get nothing. Is this a bug or it's some mistake in my code? Thanks.

UPDATE:

After some investigation I realized that sessionAuthenticated has been called but only once. And it doesn't called when session restored from localStorage with method restore. Is this expected behavior? For me it looks like a bug...

Crabar
  • 1,829
  • 1
  • 14
  • 26

1 Answers1

0

sessionAuthenticated and sessionInvalidated are actions. Just move them in your actions hash and you should get your logging statements.

justinaray
  • 118
  • 10
  • Thx. I will try. But I thought these are methods (as I learn from http://ember-simple-auth.com/api/classes/ApplicationRouteMixin.html). – Crabar Nov 09 '15 at 08:06
  • Ah, this is a change from 0.8.x to 1.x. Sorry to muddy the waters. – justinaray Nov 09 '15 at 15:20