service: $token

$token

Methods

getCachedToken() → {string}

Get the token saved in localStorage or sessionStorage

Source:
Returns:

Stored token

Type
string
Example
angular.module('someModule', ['ngToken'])
       .controller(function($token){
             var token = $token.getCachedToken();
             console.log(token);
        });

keepAlive() → {*}

Calls the keepAlive endpoint to keep the session alive. This keeps tokens from expiring when the user is still browsing. If successful, sets a new user token and calls a success event. Otherwise, calls a failure event. This is automatically called by $tokenTimeout when the keepalive event is broadcast.

Source:
Returns:

Data from http request

Type
*
Example
angular.module('someModule', ['ngToken'])
       .controller(function($scope, $token){

             $scope.$on('$tokenKeepalive', function(event, data){
                 // do post keepalive stuff
                 // data will be the response from the http request
             });

             $scope.$on('$tokenKeepAliveFail', function(event, data){
                 // handle keepalive failure
                 // probably means that the token has expired
                 // data will be the response from the http request
             });

              var user = {"username":"user", "password":"password"}
             $token.keepAlive(user);
        });

login(credentials) → {*}

Takes login credentials and sends them to the login endpoint. If successful, saves the token to storage and broadcasts a succss event. Otherwise it broadcasts a failure event.

Parameters:
Name Type Description
credentials *

Credentials to pass to the server using the login endpoint

Source:
Returns:

Data from http request

Type
*
Example
angular.module('someModule', ['ngToken'])
       .controller(function($scope, $token){

             $scope.$on('$tokenAuthSuccess', function(event, data){
                 // do post login stuff
                 // data will be the response from the http request
             });

             $scope.$on('$tokenAuthFail', function(event, data){
                 // handle login failure
                 // data will be the response from the http request
             });

              var user = {"username":"user", "password":"password"}
             $token.login(user); // log the user in
        });

logout()

Makes a request to the logout endpoint, calls $token#sessionExpired, and broadcasts an event

Source:
Example
angular.module('someModule', ['ngToken'])
       .controller(function($scope, $token){

             $scope.$on('$tokenLogoutSuccess', function(event, data){
                 // do post logout stuff
                 // data will be the response from the http request
             });

             $scope.$on('$tokenLogoutFail', function(event, data){
                 // handle logout failure
                 // data will be the response from the http request
             });

             $token.logout(); // log the user out
        });

sessionExpired()

Called when user session expires to clear the stored token out of storage. Called automatically by $token#logout

Source:

setToken(token)

Sets a new token in storage. ng-token will automatically set tokens, so this method is mostly for convenience or if you need to reset a token manually.

Parameters:
Name Type Description
token string

Token to store

Source:
Example
angular.module('someModule', ['ngToken'])
       .controller(function($token){
             $token.setToken('someToken');
             var token = $token.getCachedToken();
             console.log(token); // 'someToken'
        });