I have a simple Backand and Ionic app where I want users to log in once and no more from that point on, just like the Facebook app for example. So once the user is logged in, I receive a token from Backand. From what I know, I assume I have to save that token in localStorage (which I'm doing, and works). But from that point on, I don't understand what I need to do to log the user back in when he revisits.
I have tried in my angular "run" method to look for an existing token in the localstorage, and if one exists, I paste it in my http headers. (the following function exists in the authentication service and is being called in the "run" method).
self.checkExistingUser = function() {
if ($localStorage.user_token) {
$http.defaults.headers.common.Authorization = $localStorage.user_token;
Backand.user.getUserDetails()
.then(function (response) {
// Do stuff
}
console.log('Token found, logging in user');
}
};
I assumed that the "getUserDetails()" call would interpret the Authorization header I had just added. But that's what I misunderstood; that's not how it works.
So my question is: how do I automatically log in the returning (existing) user with that token? I can't seem to find any function for that purpose in the Backand docs.
Thanks in advance!
Your comment made me find the answer to my question, Kornatzky.
The problem was that I had included my appName
, anonymousToken
and signUpToken
into the initialization (BackandProvider.init). Removing the anonymousToken
and adding useAnonymousTokenByDefault:false
solved the problem.
getUserDetails
now returns my currently logged-in user instead of a Guest object. Thanks for your help!