1
votes

I have followed Authentication Tutorial, but running into some issues.

I have a php backend api which resides in another domain, http://rest.api {local development} The ember js application uses ember-app-kit and connects to the rest api. When the user submits the login form it sends the username/email with password to one of the route defined in the rest api Session Controller

import AuthManager from 'lms/config/auth_manager';

var SessionNewController = Ember.ObjectController.extend({

attemptedTransition : null,
loginText : 'Log In',

actions: {
    loginUser : function() {
        var self = this;
        var router = this.get('target');
        var data = this.getProperties('identity', 'password');
        var attemptedTrans = this.get('attemptedTransition');

        $.post('http://rest.api/login',
            data,
            function(results) {
                console.log(results.session);
                console.log(results.user_id);
                AuthManager.authenticate(results.session, results.user_id);
                if(attemptedTrans) {
                    attemptedTrans.retry();
                    self.set('attemptedTransition', null);
                } else {
                    router.transitionTo('index');
                }
            }
        )
    }
}


});

export default SessionNewController;

After receiving the api result in the results variable which looks like this :

Object {success: "user login success", session: "2OmwKLPclC.YhYAT3745467my7t0m2uo", user_id: "1"}

But as soon as I capture the data and send it to the AuthManager which resides in Auth Manager Code

import User from 'lms/models/user';
import Application from 'lms/adapters/application';
var AuthManager = Ember.Object.extend({

    init: function() {
        this._super();
        var accessToken = $.cookie('access_token');
        var authUserId = $.cookie('auth_user');
        if(!Ember.isEmpty(accessToken) || !Ember.isEmpty(authUserId)) {
            this.authenticate(accessToken, authUserId);
        }
    },

    isAuthenticated: function() {
        return !Ember.isEmpty(this.get('ApiKey.accessToken')) && !Ember.isEmpty(this.get('ApiKey.user'));
    },

    authenticate: function(accessToken, userId) {
        $.ajaxSetup({
            headers: { 'Authorization': 'Bearer ' + accessToken }
        });
        var user = User.store.find(userId);
        console.log(user);
        this.set('ApiKey', ApiKey.create({
            accessToken: accessToken,
            user: user
        }));
    },

    reset: function() {
        this.set('ApiKey', null);
        $.ajaxSetup({
            headers: { 'Authorization': 'Bearer None' }
        });
    },

    apiKeyObserver: function() {
        Application.accessToken = this.get('apikey.accessToken');
        if (Ember.isEmpty(this.get('ApiKey'))) {
            $.removeCookie('access_token');
            $.removeCookie('auth_user');
        } else {
            $.cookie('access_token', this.get('ApiKey.accessToken'));
            $.cookie('auth_user', this.get('ApiKey.user.id'));
        }
    }.observes('ApiKey')
});

export default AuthManager;

I got an error in the console saying

Uncaught TypeError: Object function () {
    if (!wasApplied) {
      Class.proto(); // prepare prototype...
    }
    o_defineProperty(this, GUID_KEY, undefinedDescriptor);
    o_defineProperty(this, '_super', undefinedDescriptor);
    var m = met...<omitted>...e' new.js:23
(anonymous function) new.js:23
jQuery.Callbacks.fire jquery.js:1037
jQuery.Callbacks.self.fireWith jquery.js:1148
done jquery.js:8074
jQuery.ajaxTransport.send.callback jquery.js:8598

It is not able to pass the variables to the imported function.

1

1 Answers

0
votes

Finally got this working. The error that was I doing is after extending the Ember.Object.extend() on auth_manager.js, I didn't create the object anywhere. Thats why it couldnt set create a cookie and throwing that error message.

All I had to do was, .create() after extending the object.

Don't know whether it is the right method or not. But it certainly works.