How to prevent multiple login with same credentials from different device in angular Firebase application ?
2 Answers
Make an UUID and register it in the db on every login. Then check if it match, if it not match, then logout. Can do something like:
import { v4 as uuid } from 'uuid';
// on login
this.uuidValue = uuid();
//Add to db I would say realtime not firestore
this.doCheck();
//Check like
private doCheck() {
this.checkSub = this.db.object(`uuid/$ID`).valueChanges().subscribe((_uuid: any) => {
if(this.uuidValue !== _uuid) {
//logout
this.checkSub.unsubscribe();
Comments
I can think of two options. A simple not so secure one, and a more complicated more secure one:
You could create a folder in your database that keeps track of logged in users. Then you can query it before or after someone tries to login and handle the event accordingly. Of course this way you can't prevent users from tampering with the javascript.
If you want to make sure that people cannot circumvent your blocker you could implement custom authentication during which you check the database on the server and only return a token if nobody else is logged in. https://firebase.google.com/docs/auth/web/custom-auth