Ember Auth Custom Authenticator

337 views Asked by At

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');
    }
});
1

There are 1 answers

2
Alex On BEST ANSWER

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);
        });
    });
}