I created token based authorization. After login, it stored token in localStorage but I realized that localStorage is synchronous and this is not what I want in my single page app. So I look into Angular Local Forage which as they say is async.
First I download localForage and angularLocalForage and add it in index
<script src="public/scripts/angular/localforage.js"></script>
<script src="public/scripts/angular/angular-localForage.js"></script>
next, I add the module in app.js
'LocalForageModule'
on success call in loginService i added following lines:
$localForage.set('authorization', {
token: response.data.data.attributes.token,
nick: 'other values..if neccessary'
});
and in my External Controller which wrap entire page I added $localForage
get method:
'use strict';
app.controller('ExternalCtrl', ['$scope', '$localForage',
function($scope, $localForage) {
$scope.authentication = {};
$scope.authentication = {
isAuth: false
};
var authData = $localForage.get('authorization');
if(authData) {
$scope.authentication.isAuth = true;
console.log($scope.authentication.isAuth);
}
}
]);
Unfortunately, that doesn't work and makes all page doesn't show up it causes this error:
TypeError: $localForage.get is not a function at new (ExternalCtrl.js:11) at Object.invoke (
Acording the doc
There are no get() and set() method with localForage, should use use getItem() and setItem() and they are async, promise.
$localForage.setItem('authorization', {
token: response.data.data.attributes.token,
nick: 'other values..if neccessary'
}).then(function() {
//logged and stored data
});
and
function loadData() {
//here load data
}
$localForage.getItem('authorization').then(function(authData) {
//we got a stored authData
loadData();
}, function() {
// not logged
});