0

When writing a custom authenticator using Ember.SimpleAuth with Ember-CLI, what exactly does the authenticate method of the custom authenticator need to return in order to establish a user as logged in? Below is the authenticator as it currently exists. We are using a phalcon rest api for the back end, so ultimately it seems that this method will need to hit that URL and authenticate the user on the server side, but what should the server return in order for ember.simpleauth to do what it needs to do?

import Ember from "ember";
import App from '../app';
import Base from "simple-auth/authenticators/base";

export default Base.extend({
    tokenEndpoint: 'login',

    restore: function(data) {
        console.log('ran resotre');
    },
    authenticate: function(credentials) {
        alert(credentials.identification);
        alert(credentials.password);
    },
    invalidate: function() {
        console.log('ran invalidate');
    }
});
John
  • 171
  • 1
  • 2
  • 11

1 Answers1

0

I would read Ember Simple Auth - API

authenticate needs to return a promise. Within the method, that promise needs to be resolved or rejected. A resolved promise would signal a successful authentication, while a rejected promise a failure in authentication. Here is how I structured a quick authenticate function.

 authenticate: function (credentials, options) {
    return new Ember.RSVP.Promise(function (resolve, reject) {
        var loginPromise = Ember.$.post(<token url goes here>, {
            username: credentials.identification,
            password: credentials.password
        });
        loginPromise.then(function (data) {
            resolve({
                token: data.token,
                userData: data.user
            });
        }, function (error) {
            reject(error);
        });
    });
}
Alex
  • 463
  • 3
  • 12
  • Thanks for the example. That helped me understand things a lot better. There is one other thing that I am running into. When the authenticate method is run and the Ember.$.post call happens, it throws a CORS error. I am trying to send this request to a REST API that is on localhost:8080/projectname. cont'd – John Nov 13 '14 at 16:27
  • The ember app is on localhost:4200. I don't understand why I am able to use Ember Data to make calls to the API with not problem, but when I try to do the above ajax post i get this error: `Content Security Policy: The page's settings observed the loading of a resource at localhost:8080/phalconbase/v1/auth/login ("connect-src http://localhost:4200 http://api.local:8080 ws://localhost:35729 ws://0.0.0.0:35729"). A CSP report is being sent.` – John Nov 13 '14 at 16:29