I am making a MEAN stack application with authentication by JWT. This is the script which handles token authentication:
authenticationService.js
examApp.factory('authenticationService', ['$window', function ($window) {
var authToken = {};
var store = $window.localStorage;
var key = 'auth-token';
authToken.getToken = function () {
return store.getItem(key);
}
authToken.setToken = function (token) {
if (token) {
store.setItem = (key, token);
} else {
store.removeItem(key);
}
}
return authToken;}]);
Now, whenever i am logging into my application, the user/admin can log in successfully but in console, i am not receiving any tokens with token=null being displayed.
When i log in the application through POSTMAN tool and manually copy-pasting token after token=
part in routes, i can perform any task, but logging in through angularjs ui, i get
Token authentication failed
Any help will be appreciated.
There is a problem in your code at
authToken.setToken = function (token) {
if (token) {
store.setItem = (key, token); // Here is the problem
} else {
store.removeItem(key);
}
}
You are assigning the store.setItem
, instead you need to execute store.setItem
like below
authToken.setToken = function (token) {
if (token) {
store.setItem(key, token);
} else {
store.removeItem(key);
}
}