I'm really trying to understand how all of this operates. It seems like it should be working as intended. I have an Auth factory that, when the jwt token expires, calls its 'delegate' method which obtains a new token using the refresh token. However, I keep encountering the error '_this.delegate is not a function'. (I am calling delegate in both cases for testing purposes)
webapp.factory('Auth', function($http, API_URL, $window, $location, jwtHelper ) {
var _this = this;
var delegate = function(){
$http.post(API_URL+'/admin/delegate', {refresh_token: $window.sessionStorage.refreshToken } ).success(function(result) {
$window.sessionStorage.authToken = result.token;
$window.sessionStorage.refreshToken = result.refresh_token;
console.log('delegate-result: '+JSON.stringify(result));
$location.path('/about');
//LocalService.set('authToken', JSON.stringify(result));
});
};
return {
//returns true if there is an auth token
isAuthenticated: function() {
var storedJwt = $window.sessionStorage.authToken;
console.log('stored JWT: '+storedJwt);
var storedPayload = jwtHelper.decodeToken(storedJwt);
console.log('payload: '+JSON.stringify(storedPayload));
if(jwtHelper.isTokenExpired(storedJwt)){
console.log('is expired expired: '+jwtHelper.getTokenExpirationDate(storedJwt));
_this.delegate();
} else {
console.log('is not expired expires: '+jwtHelper.getTokenExpirationDate(storedJwt));
//For testing
_this.delegate();
}
return $window.sessionStorage.authToken;
//LocalService.get('authToken');
},
delegate: delegate,
//login function, should be moved to login controller
login: function(email, password) {
var login = $http.post(API_URL+'/authenticate', {email: email, password: password } );
login.success(function(result) {
console.log('login-result: '+JSON.stringify(result));
$window.sessionStorage.authToken = result.token;
$window.sessionStorage.refreshToken = result.refresh_token;
$location.path('/about');
//LocalService.set('authToken', JSON.stringify(result));
});
return login;
},