How to force logout firebase auth user from app remotely
up vote
7
down vote
favorite
I have a project which uses firebase auth with firebaseUI to authenticate users. I have enabled Google, Facebook and email providers. What I need is to remotely logout or disable some of the users.
I want the users to logout from the app on doing so. I tried disabling the user in the firebase console and also used the firebase admin SDK (https://firebase.google.com/docs/auth/admin/manage-sessions) to revoke the refresh tokens.
I waited for more than 2 days and still noticed that the user was logged in and could access the firestore data.
I have also gone through and tried
Firebase still retrieving authData after deletion
Can anyone point to what I am doing wrong ?
firebase firebase-authentication firebase-admin
add a comment |
up vote
7
down vote
favorite
I have a project which uses firebase auth with firebaseUI to authenticate users. I have enabled Google, Facebook and email providers. What I need is to remotely logout or disable some of the users.
I want the users to logout from the app on doing so. I tried disabling the user in the firebase console and also used the firebase admin SDK (https://firebase.google.com/docs/auth/admin/manage-sessions) to revoke the refresh tokens.
I waited for more than 2 days and still noticed that the user was logged in and could access the firestore data.
I have also gone through and tried
Firebase still retrieving authData after deletion
Can anyone point to what I am doing wrong ?
firebase firebase-authentication firebase-admin
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I have a project which uses firebase auth with firebaseUI to authenticate users. I have enabled Google, Facebook and email providers. What I need is to remotely logout or disable some of the users.
I want the users to logout from the app on doing so. I tried disabling the user in the firebase console and also used the firebase admin SDK (https://firebase.google.com/docs/auth/admin/manage-sessions) to revoke the refresh tokens.
I waited for more than 2 days and still noticed that the user was logged in and could access the firestore data.
I have also gone through and tried
Firebase still retrieving authData after deletion
Can anyone point to what I am doing wrong ?
firebase firebase-authentication firebase-admin
I have a project which uses firebase auth with firebaseUI to authenticate users. I have enabled Google, Facebook and email providers. What I need is to remotely logout or disable some of the users.
I want the users to logout from the app on doing so. I tried disabling the user in the firebase console and also used the firebase admin SDK (https://firebase.google.com/docs/auth/admin/manage-sessions) to revoke the refresh tokens.
I waited for more than 2 days and still noticed that the user was logged in and could access the firestore data.
I have also gone through and tried
Firebase still retrieving authData after deletion
Can anyone point to what I am doing wrong ?
firebase firebase-authentication firebase-admin
firebase firebase-authentication firebase-admin
edited Nov 16 at 2:40
esarkis
838
838
asked Oct 31 at 16:19
Ashwin Valento
1066
1066
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
3
down vote
You can send a message data with FCM to force to log out.
For example, if the users use android application.
- Save the FCM token in a collection in firebase Realtime.
- configure the Android client app, in the service. LINK You have to make when receive a message with especial string, force to log out.
- make the trigger you need in cloud functions, to send the data LINK when you need the user log out.
SUCCESS!
add a comment |
up vote
3
down vote
You also cannot remotely force a user to be signed out. Any sign out will have to happen from the device that the user is signed in on.
There is no way to revoke an access token once that is minted. This means that even if you disable the user's account, they may continue to have access for up to an hour.
If that is too long, the trick (as also mentioned in my answer to the question you linked) is to maintain a list of blocked users in your database (or elsewhere) and then check against that in your security rules (or other authorization layer).
For example in the realtime database, you could create a list of blocked user's UIDs:
banned_uids
uid1: true
uid2: true
And then check against that in your security rules with:
".read": "auth.uid !== null && !root.child('banned_uids').child(auth.uid).exists()"
Thanks, @frank-van-puffelen. In our case, we tried disabling the user and tried revoking his/her refresh token, but the user is still able to access data even after waiting for 1 hour (we waited for 2 days also :) ). We also have rule added in Firestore to allow only authenticated user. If my understanding is wrong, please help me understand when to revoke user refresh token?
– Vishal Vijay
Nov 10 at 10:38
I noticed I said ID token, where I mean access token, so I fixed that. Revoking access tokens isn't enough, so that's where security rules come in. If the steps don't work for you, show exactly what you've done. Without code or exact steps-to-reproduce, it is hard for anyone to say where you made a mistake.
– Frank van Puffelen
Nov 10 at 15:08
add a comment |
up vote
2
down vote
As per your scenarios, i assume that you need to make user logout when user is disabled.
Use One global variable to store TokenNo (might be in shared preference or sqlite):
Add following code to your manifest:
<service android:name=".YourFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Add following code in your
public class LogoutOntokenchange extends FirebaseMessagingService
@Override
public void onNewToken (String token)
if(TokenNo=>1) //if tokenNo >=1 means he already logged in
TokenNo=0;
FirebaseAuth.getInstance().signOut(); //Then call signout method
else
TokenNo=1; //store token no in db
What Happens here:
When user logged in first time onNewToken is called then It goes into else then TokenNo is updated to 1 from 0.
When You disable any user then automatically token is refreshed.Then OnNewToken is called then TokenNo>=1 so user will be logged out.
NOTE: When user log in for first time i.e if TokenNo variable is not stored then store it as 0.
For reference: https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService
As far as I know,onNewToken
above belongs to FCM. Can you please explain why are we Logging the user out on refresh of FCM token ?
– Ashwin Valento
Nov 15 at 15:31
add a comment |
up vote
0
down vote
Not tested yet, as our backend programmer, who is in charge of setting up Firestore rules was gone for the day, but in theory this should work: (and it's something I'll test tomorrow)
Having a FirebaseAuth.AuthStateListener in charge of serving UI based on the status of the user
This combined with rules in firestore
match /collection
allow read: if isAuth();
Where isAuth is:
function isAuth()
return request.auth.uid != null;
If the user is then disabled, while being logged in, whenever the user tries to read data from the collection, he should be denied, and a signOut() call should be made.
The AuthStateListener will then detect it, and sign the user out.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You can send a message data with FCM to force to log out.
For example, if the users use android application.
- Save the FCM token in a collection in firebase Realtime.
- configure the Android client app, in the service. LINK You have to make when receive a message with especial string, force to log out.
- make the trigger you need in cloud functions, to send the data LINK when you need the user log out.
SUCCESS!
add a comment |
up vote
3
down vote
You can send a message data with FCM to force to log out.
For example, if the users use android application.
- Save the FCM token in a collection in firebase Realtime.
- configure the Android client app, in the service. LINK You have to make when receive a message with especial string, force to log out.
- make the trigger you need in cloud functions, to send the data LINK when you need the user log out.
SUCCESS!
add a comment |
up vote
3
down vote
up vote
3
down vote
You can send a message data with FCM to force to log out.
For example, if the users use android application.
- Save the FCM token in a collection in firebase Realtime.
- configure the Android client app, in the service. LINK You have to make when receive a message with especial string, force to log out.
- make the trigger you need in cloud functions, to send the data LINK when you need the user log out.
SUCCESS!
You can send a message data with FCM to force to log out.
For example, if the users use android application.
- Save the FCM token in a collection in firebase Realtime.
- configure the Android client app, in the service. LINK You have to make when receive a message with especial string, force to log out.
- make the trigger you need in cloud functions, to send the data LINK when you need the user log out.
SUCCESS!
answered Nov 10 at 6:27
Mike Brian Olivera
3651513
3651513
add a comment |
add a comment |
up vote
3
down vote
You also cannot remotely force a user to be signed out. Any sign out will have to happen from the device that the user is signed in on.
There is no way to revoke an access token once that is minted. This means that even if you disable the user's account, they may continue to have access for up to an hour.
If that is too long, the trick (as also mentioned in my answer to the question you linked) is to maintain a list of blocked users in your database (or elsewhere) and then check against that in your security rules (or other authorization layer).
For example in the realtime database, you could create a list of blocked user's UIDs:
banned_uids
uid1: true
uid2: true
And then check against that in your security rules with:
".read": "auth.uid !== null && !root.child('banned_uids').child(auth.uid).exists()"
Thanks, @frank-van-puffelen. In our case, we tried disabling the user and tried revoking his/her refresh token, but the user is still able to access data even after waiting for 1 hour (we waited for 2 days also :) ). We also have rule added in Firestore to allow only authenticated user. If my understanding is wrong, please help me understand when to revoke user refresh token?
– Vishal Vijay
Nov 10 at 10:38
I noticed I said ID token, where I mean access token, so I fixed that. Revoking access tokens isn't enough, so that's where security rules come in. If the steps don't work for you, show exactly what you've done. Without code or exact steps-to-reproduce, it is hard for anyone to say where you made a mistake.
– Frank van Puffelen
Nov 10 at 15:08
add a comment |
up vote
3
down vote
You also cannot remotely force a user to be signed out. Any sign out will have to happen from the device that the user is signed in on.
There is no way to revoke an access token once that is minted. This means that even if you disable the user's account, they may continue to have access for up to an hour.
If that is too long, the trick (as also mentioned in my answer to the question you linked) is to maintain a list of blocked users in your database (or elsewhere) and then check against that in your security rules (or other authorization layer).
For example in the realtime database, you could create a list of blocked user's UIDs:
banned_uids
uid1: true
uid2: true
And then check against that in your security rules with:
".read": "auth.uid !== null && !root.child('banned_uids').child(auth.uid).exists()"
Thanks, @frank-van-puffelen. In our case, we tried disabling the user and tried revoking his/her refresh token, but the user is still able to access data even after waiting for 1 hour (we waited for 2 days also :) ). We also have rule added in Firestore to allow only authenticated user. If my understanding is wrong, please help me understand when to revoke user refresh token?
– Vishal Vijay
Nov 10 at 10:38
I noticed I said ID token, where I mean access token, so I fixed that. Revoking access tokens isn't enough, so that's where security rules come in. If the steps don't work for you, show exactly what you've done. Without code or exact steps-to-reproduce, it is hard for anyone to say where you made a mistake.
– Frank van Puffelen
Nov 10 at 15:08
add a comment |
up vote
3
down vote
up vote
3
down vote
You also cannot remotely force a user to be signed out. Any sign out will have to happen from the device that the user is signed in on.
There is no way to revoke an access token once that is minted. This means that even if you disable the user's account, they may continue to have access for up to an hour.
If that is too long, the trick (as also mentioned in my answer to the question you linked) is to maintain a list of blocked users in your database (or elsewhere) and then check against that in your security rules (or other authorization layer).
For example in the realtime database, you could create a list of blocked user's UIDs:
banned_uids
uid1: true
uid2: true
And then check against that in your security rules with:
".read": "auth.uid !== null && !root.child('banned_uids').child(auth.uid).exists()"
You also cannot remotely force a user to be signed out. Any sign out will have to happen from the device that the user is signed in on.
There is no way to revoke an access token once that is minted. This means that even if you disable the user's account, they may continue to have access for up to an hour.
If that is too long, the trick (as also mentioned in my answer to the question you linked) is to maintain a list of blocked users in your database (or elsewhere) and then check against that in your security rules (or other authorization layer).
For example in the realtime database, you could create a list of blocked user's UIDs:
banned_uids
uid1: true
uid2: true
And then check against that in your security rules with:
".read": "auth.uid !== null && !root.child('banned_uids').child(auth.uid).exists()"
edited Nov 10 at 15:08
answered Nov 9 at 22:22
Frank van Puffelen
220k25361387
220k25361387
Thanks, @frank-van-puffelen. In our case, we tried disabling the user and tried revoking his/her refresh token, but the user is still able to access data even after waiting for 1 hour (we waited for 2 days also :) ). We also have rule added in Firestore to allow only authenticated user. If my understanding is wrong, please help me understand when to revoke user refresh token?
– Vishal Vijay
Nov 10 at 10:38
I noticed I said ID token, where I mean access token, so I fixed that. Revoking access tokens isn't enough, so that's where security rules come in. If the steps don't work for you, show exactly what you've done. Without code or exact steps-to-reproduce, it is hard for anyone to say where you made a mistake.
– Frank van Puffelen
Nov 10 at 15:08
add a comment |
Thanks, @frank-van-puffelen. In our case, we tried disabling the user and tried revoking his/her refresh token, but the user is still able to access data even after waiting for 1 hour (we waited for 2 days also :) ). We also have rule added in Firestore to allow only authenticated user. If my understanding is wrong, please help me understand when to revoke user refresh token?
– Vishal Vijay
Nov 10 at 10:38
I noticed I said ID token, where I mean access token, so I fixed that. Revoking access tokens isn't enough, so that's where security rules come in. If the steps don't work for you, show exactly what you've done. Without code or exact steps-to-reproduce, it is hard for anyone to say where you made a mistake.
– Frank van Puffelen
Nov 10 at 15:08
Thanks, @frank-van-puffelen. In our case, we tried disabling the user and tried revoking his/her refresh token, but the user is still able to access data even after waiting for 1 hour (we waited for 2 days also :) ). We also have rule added in Firestore to allow only authenticated user. If my understanding is wrong, please help me understand when to revoke user refresh token?
– Vishal Vijay
Nov 10 at 10:38
Thanks, @frank-van-puffelen. In our case, we tried disabling the user and tried revoking his/her refresh token, but the user is still able to access data even after waiting for 1 hour (we waited for 2 days also :) ). We also have rule added in Firestore to allow only authenticated user. If my understanding is wrong, please help me understand when to revoke user refresh token?
– Vishal Vijay
Nov 10 at 10:38
I noticed I said ID token, where I mean access token, so I fixed that. Revoking access tokens isn't enough, so that's where security rules come in. If the steps don't work for you, show exactly what you've done. Without code or exact steps-to-reproduce, it is hard for anyone to say where you made a mistake.
– Frank van Puffelen
Nov 10 at 15:08
I noticed I said ID token, where I mean access token, so I fixed that. Revoking access tokens isn't enough, so that's where security rules come in. If the steps don't work for you, show exactly what you've done. Without code or exact steps-to-reproduce, it is hard for anyone to say where you made a mistake.
– Frank van Puffelen
Nov 10 at 15:08
add a comment |
up vote
2
down vote
As per your scenarios, i assume that you need to make user logout when user is disabled.
Use One global variable to store TokenNo (might be in shared preference or sqlite):
Add following code to your manifest:
<service android:name=".YourFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Add following code in your
public class LogoutOntokenchange extends FirebaseMessagingService
@Override
public void onNewToken (String token)
if(TokenNo=>1) //if tokenNo >=1 means he already logged in
TokenNo=0;
FirebaseAuth.getInstance().signOut(); //Then call signout method
else
TokenNo=1; //store token no in db
What Happens here:
When user logged in first time onNewToken is called then It goes into else then TokenNo is updated to 1 from 0.
When You disable any user then automatically token is refreshed.Then OnNewToken is called then TokenNo>=1 so user will be logged out.
NOTE: When user log in for first time i.e if TokenNo variable is not stored then store it as 0.
For reference: https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService
As far as I know,onNewToken
above belongs to FCM. Can you please explain why are we Logging the user out on refresh of FCM token ?
– Ashwin Valento
Nov 15 at 15:31
add a comment |
up vote
2
down vote
As per your scenarios, i assume that you need to make user logout when user is disabled.
Use One global variable to store TokenNo (might be in shared preference or sqlite):
Add following code to your manifest:
<service android:name=".YourFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Add following code in your
public class LogoutOntokenchange extends FirebaseMessagingService
@Override
public void onNewToken (String token)
if(TokenNo=>1) //if tokenNo >=1 means he already logged in
TokenNo=0;
FirebaseAuth.getInstance().signOut(); //Then call signout method
else
TokenNo=1; //store token no in db
What Happens here:
When user logged in first time onNewToken is called then It goes into else then TokenNo is updated to 1 from 0.
When You disable any user then automatically token is refreshed.Then OnNewToken is called then TokenNo>=1 so user will be logged out.
NOTE: When user log in for first time i.e if TokenNo variable is not stored then store it as 0.
For reference: https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService
As far as I know,onNewToken
above belongs to FCM. Can you please explain why are we Logging the user out on refresh of FCM token ?
– Ashwin Valento
Nov 15 at 15:31
add a comment |
up vote
2
down vote
up vote
2
down vote
As per your scenarios, i assume that you need to make user logout when user is disabled.
Use One global variable to store TokenNo (might be in shared preference or sqlite):
Add following code to your manifest:
<service android:name=".YourFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Add following code in your
public class LogoutOntokenchange extends FirebaseMessagingService
@Override
public void onNewToken (String token)
if(TokenNo=>1) //if tokenNo >=1 means he already logged in
TokenNo=0;
FirebaseAuth.getInstance().signOut(); //Then call signout method
else
TokenNo=1; //store token no in db
What Happens here:
When user logged in first time onNewToken is called then It goes into else then TokenNo is updated to 1 from 0.
When You disable any user then automatically token is refreshed.Then OnNewToken is called then TokenNo>=1 so user will be logged out.
NOTE: When user log in for first time i.e if TokenNo variable is not stored then store it as 0.
For reference: https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService
As per your scenarios, i assume that you need to make user logout when user is disabled.
Use One global variable to store TokenNo (might be in shared preference or sqlite):
Add following code to your manifest:
<service android:name=".YourFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Add following code in your
public class LogoutOntokenchange extends FirebaseMessagingService
@Override
public void onNewToken (String token)
if(TokenNo=>1) //if tokenNo >=1 means he already logged in
TokenNo=0;
FirebaseAuth.getInstance().signOut(); //Then call signout method
else
TokenNo=1; //store token no in db
What Happens here:
When user logged in first time onNewToken is called then It goes into else then TokenNo is updated to 1 from 0.
When You disable any user then automatically token is refreshed.Then OnNewToken is called then TokenNo>=1 so user will be logged out.
NOTE: When user log in for first time i.e if TokenNo variable is not stored then store it as 0.
For reference: https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService
edited Nov 10 at 15:33
answered Nov 10 at 15:28
maneesh
11312
11312
As far as I know,onNewToken
above belongs to FCM. Can you please explain why are we Logging the user out on refresh of FCM token ?
– Ashwin Valento
Nov 15 at 15:31
add a comment |
As far as I know,onNewToken
above belongs to FCM. Can you please explain why are we Logging the user out on refresh of FCM token ?
– Ashwin Valento
Nov 15 at 15:31
As far as I know,
onNewToken
above belongs to FCM. Can you please explain why are we Logging the user out on refresh of FCM token ?– Ashwin Valento
Nov 15 at 15:31
As far as I know,
onNewToken
above belongs to FCM. Can you please explain why are we Logging the user out on refresh of FCM token ?– Ashwin Valento
Nov 15 at 15:31
add a comment |
up vote
0
down vote
Not tested yet, as our backend programmer, who is in charge of setting up Firestore rules was gone for the day, but in theory this should work: (and it's something I'll test tomorrow)
Having a FirebaseAuth.AuthStateListener in charge of serving UI based on the status of the user
This combined with rules in firestore
match /collection
allow read: if isAuth();
Where isAuth is:
function isAuth()
return request.auth.uid != null;
If the user is then disabled, while being logged in, whenever the user tries to read data from the collection, he should be denied, and a signOut() call should be made.
The AuthStateListener will then detect it, and sign the user out.
add a comment |
up vote
0
down vote
Not tested yet, as our backend programmer, who is in charge of setting up Firestore rules was gone for the day, but in theory this should work: (and it's something I'll test tomorrow)
Having a FirebaseAuth.AuthStateListener in charge of serving UI based on the status of the user
This combined with rules in firestore
match /collection
allow read: if isAuth();
Where isAuth is:
function isAuth()
return request.auth.uid != null;
If the user is then disabled, while being logged in, whenever the user tries to read data from the collection, he should be denied, and a signOut() call should be made.
The AuthStateListener will then detect it, and sign the user out.
add a comment |
up vote
0
down vote
up vote
0
down vote
Not tested yet, as our backend programmer, who is in charge of setting up Firestore rules was gone for the day, but in theory this should work: (and it's something I'll test tomorrow)
Having a FirebaseAuth.AuthStateListener in charge of serving UI based on the status of the user
This combined with rules in firestore
match /collection
allow read: if isAuth();
Where isAuth is:
function isAuth()
return request.auth.uid != null;
If the user is then disabled, while being logged in, whenever the user tries to read data from the collection, he should be denied, and a signOut() call should be made.
The AuthStateListener will then detect it, and sign the user out.
Not tested yet, as our backend programmer, who is in charge of setting up Firestore rules was gone for the day, but in theory this should work: (and it's something I'll test tomorrow)
Having a FirebaseAuth.AuthStateListener in charge of serving UI based on the status of the user
This combined with rules in firestore
match /collection
allow read: if isAuth();
Where isAuth is:
function isAuth()
return request.auth.uid != null;
If the user is then disabled, while being logged in, whenever the user tries to read data from the collection, he should be denied, and a signOut() call should be made.
The AuthStateListener will then detect it, and sign the user out.
answered Nov 15 at 15:53
AverageJoeDK
1
1
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53087895%2fhow-to-force-logout-firebase-auth-user-from-app-remotely%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown