infrabel / themes-gnap

Standardized build to produce web themes for use with GNaP.
http://gnap.io/
BSD 3-Clause "New" or "Revised" License
3 stars 4 forks source link

Add a authentication service to obtain a JWT token #102

Open sroosen opened 9 years ago

sroosen commented 9 years ago

Currently you need to manually create a token using the Token resource service and handle the HTTP response yourself. It would be better if we added a authentication service that would handle all of this.

The current flow looks like this:

var credentials = {
    username: vm.credentials.username,
    password: vm.credentials.password
};

var newToken = new Token(credentials);

newToken.$save().then(
    function (token) { // success
        // store token
        sessionService.beginSession(token.Token);

        // redirect back to the referrer or to the app root
        var redirectState = $location.search().redirect_state; /* jshint ignore:line*/
        if (redirectState) {
            $state.go(redirectState);
        } else {
            $location.path('/');
        }
    },
    function (error) { // error
        if (error.status === 400) {
            vm.invalidCredentials = true;
        }
    });

It should have a simple function signature like this:

authenticationService.authenticate(username, password)
    .then(function(token) {
        // redirect here
    });