Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase logout user all sessions

I am using Firebase authentication in my iOS app. Is there any way in Firebase when user login my app with Firebase then logout that user all other devices(sessions)? Can I do that with Firebase admin SDK?

like image 700
Karen Hovhannisyan Avatar asked Feb 15 '19 07:02

Karen Hovhannisyan


2 Answers

When i had this issue i resolved it with cloud functions Please visit this link for more details https://firebase.google.com/docs/auth/admin/manage-sessions#revoke_refresh_tokens

Do the following;

  1. Set up web server with firebase cloud functions (if none exists)
  2. use the admin sdk(thats the only way this method would work) - [Visit this link] ( (https://firebase.google.com/docs/admin/setup#initialize_the_sdk).
  3. Create an api that receives the uid and revokes current sessions as specified in the first link above
  admin.auth().revokeRefreshTokens(uid)
    .then(() => {
      return admin.auth().getUser(uid);
    })
    .then((userRecord) => {
      return new Date(userRecord.tokensValidAfterTime).getTime() / 1000;
    })
    .then((timestamp) => {
      //return valid response to ios app to continue the user's login process
  });

Voila users logged out. I hope this gives insight into resolving the issue

like image 105
hazelcodes Avatar answered Oct 23 '22 00:10

hazelcodes


Firebase doesn't provide such feature. You need to manage it yourself.

Here is the Firebase Doc and they haven't mentioned anything related to single user sign in.

Here is what you can do for this-

Take one token in User node (Where you save user's other data) in Firebase database and regenerate it every time you logged in into application, Match this token with already logged in user's token (Which is saved locally) in appDidBecomeActive and appDidFinishLaunching or possibly each time you perform any operation with Firebase or may be in some fixed time interval. If tokens are different logged out the user manually and take user to authenticate screen.

like image 3
TheTiger Avatar answered Oct 22 '22 23:10

TheTiger