Search code examples
firebasereact-nativegoogle-cloud-firestorefirebase-authenticationfirebase-security

Creating Admin Login System using Firebase Security Rules and Authentication


This is a continuation question of this one. First Check this.

As per the answers, I have updated my security rules in my Firestore database something like this:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null && request.auth.token.email == "...";
    }
  }
}

What code now I have to write in my app?

NOTE I have checked all the previous questions related to this problem but that answers are not giving any solution to me

Remember The code of my app is written in react native language


Solution

  • In your app, you need to just take the value from an email and password input, check if it is for example "admin@gmail.com" and "admin1234" if true "signInWithEmailAndPassword(auth, emailValue, pswdValue)" which would be a bit like that (i don't code in react-native so it might not be correct but this is what you need to implement):

    import { signInWithEmailAndPassword } from '@firebase/auth';
    
    if (emailValue === 'admin@gmail.com' && pswdValue === 'admin1234') {
          signInWithEmailAndPassword(auth, emailValue, pswdValue)
            .then(() => console.log('Sign-in successful'))
            .catch((error) => console.log('Sign-in error:', error));
    }