FireBase Auth

FireBase Auth

Step 1

  • First Implement FireBase
  • After Implementing Firebase

Step 2

  • Open Project And Go to Build > Authentication
  • Go to Sign-In methods
  • Click Add new provider
  • From Select Pop-up select Email/Password from Native providers

Step 3

  • go to documentation page
  • in there go to build / authentication > get Started
  • click on get Started ( Link Below )

Step 4

  • Follow methods according to documents
  • and implement logics in your activity
  • Code Implementation Given Below

Note:

  • As Email And Password you need to take from user in your app
  • FireBase will check email and password in it database and gives response accordingly.
 

Code Implementation

Check current auth state

  • Declare an instance of FirebaseAuth.
private lateinit var auth: FirebaseAuth
  • In the onCreate() method, initialize the FirebaseAuth instance.
// Initialize Firebase Auth auth = Firebase.auth
  • When initializing your Activity, check to see if the user is currently signed in or Not.
val currentUser = auth.currentUser   if (currentUser != null) { startActivity(Intent(this, MainActivity::class.java)) finish() } else { startActivity(Intent(this@GetStartedActivity, SignInActivity::class.java)) finish() } }

Sign up new users

Create a new createAccount method that takes in an email address and password, validates them, and then creates a new user with the createUserWithEmailAndPassword method.
auth.createUserWithEmailAndPassword(email, password)     .addOnCompleteListener(this) { task ->         if (task.isSuccessful) {             // Sign in success, update UI with the signed-in user's information             Log.d(TAG, "createUserWithEmail:success")             val user = auth.currentUser             updateUI(user)         } else {             // If sign in fails, display a message to the user.             Log.w(TAG, "createUserWithEmail:failure", task.exception)             Toast.makeText(baseContext,"Authentication failed.",Toast.LENGTH_SHORT,) .show()             updateUI(null)         }     }
Add a form to register new users with their email and password and call this new method when it is submitted. You can see an example in our quickstart sample.

Sign in existing users

Create a new signIn method which takes in an email address and password, validates them, and then signs a user in with the signInWithEmailAndPassword method.
auth.signInWithEmailAndPassword(email, password)     .addOnCompleteListener(this) { task ->         if (task.isSuccessful) {             // Sign in success, update UI with the signed-in user's information             Log.d(TAG, "signInWithEmail:success")             val user = auth.currentUser             updateUI(user)         } else {             // If sign in fails, display a message to the user.             Log.w(TAG, "signInWithEmail:failure", task.exception)             Toast.makeText(                 baseContext,                 "Authentication failed.",                 Toast.LENGTH_SHORT,             ).show()             updateUI(null)         }     }
Add a form to sign in users with their email and password and call this new method when it is submitted. You can see an example in our quickstart sample.
 

Sign Out user

logout current user
val currentUser = auth.currentUser if (currentUser != null) { auth.signOut() startActivity(Intent(this@MainActivity, GetStartedActivity::class.java)) finish()̥ }

Access user information

If a user has signed in successfully you can get their account data at any point with the getCurrentUser method.
val user = Firebase.auth.currentUser user?.let {     // Name, email address, and profile photo Url     val name = it.displayName     val email = it.email     val photoUrl = it.photoUrl     // Check if user's email is verified     val emailVerified = it.isEmailVerified     // The user's ID, unique to the Firebase project. Do NOT use this value to     // authenticate with your backend server, if you have one. Use     // FirebaseUser.getIdToken() instead.     val uid = it.uid }