Start a new topic

Login flow for logged in users

When we login a user (using login functionality) we have to ensure that there is no EXISTING user logged in (otherwise Kinvey throws an error)



This is fine if the user successfully logs in, because the new user is now the right user



However, if the user supplies incorrect login credentials, (or there is no response from Kinvey, which can happen in a mobile environment) - then we have a problem, because now we have a null user (because we had had to, just prior to the login, do a log out). [The app I am building always has a user as one is created on initialisation and does not function with a null user state]



Ideally I would like to work out how to



* run a login request, even when a user is logged in (i.e without doing the logout just prior to login)

* if the request is successful, put them to the new user

* if the request fails / incorrect credentials, keep them on the user they currently are



But I not sure that the above is possible. However it seems to be a fairly conventional idea?



Thanks!



(My solution would be to remove the restriction on being logged in with an existing user, when logging in a new user.... but I am sure you have this restriction for a good reason)



Angular library
1 Comment

Switching user accounts typically, from a users perspective, involves logging out prior to login. I realize our service requires a user context even when you’re not logged in from the users perspective. I’d propose you try something like:



```

var originalUser = $kinvey.getActiveUser();

var promise = $kinvey.User.login('username', 'password');

promise.then(null, function(e) {

// Restore original user in case of specific error.

if(e.name === 'InvalidCredentials') {

$kinvey.setActiveUser(originalUser);// Restore.

}

});

```
Login or Signup to post a comment