How to Signup/ Login/ Logout User In Firebase Authentication ?



First enable "Email/Password" provider :

  1. Open your project console and navigate to Build > Authentication dashboard in that open Sign-in method tab.
  2. Click on Email/Password that is in Native Provider column.
  3. Now enable Email/Password provider and hit Save button. 
    Email/Password tab in Firebase Auth
  4. Email Link : If you want passwordless login system then enable second option. In that link will be sent every time to user's email address for Login. [Hear we go with password base system. You can also change this in future if you want] 

    After Clicking Save :
    Email/Password tab in Firebase Auth

4. If you haven't setup firebase SDK and initialize authentication then do it first.

Create New User Account (Sign-up) :


  • createUserWithEmailAndPassword(auth, 'EMAIL', 'PASSWORD') : 
import { auth } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { createUserWithEmailAndPassword } from 'firebase/auth';

createUserWithEmailAndPassword(auth, 'EMAIL', 'PASSWORD');

  • EMAIL : Get Email address that is inputted by user and pass it in EMAIL or you can pass it manually by typing email in the method.
  • PASSWORD : Get Password that is inputted by user and it in PASSWORD or you can pass it manually by typing password in the method. 

For example,

You want to create new user and that's EMAIL is 'you@firebasemaster.com' and PASSWORD is '123456789'.

import { auth } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { createUserWithEmailAndPassword } from 'firebase/auth';

createUserWithEmailAndPassword(auth, 'you@firebasemaster.com', '123456789');

After creating user account, you can see it in 'Users' tab in Authentication dashboard. 

User of Firebase Auth

User will automatically login for first time after creating new user account. Firebase provides unique UID key to every user. You can see users email id but you can't see their password.
You can also add user through Authentication dashboard. 

This method return 'user credentials' with 'Promise' so can be handled by using '.then()' or 'async/await' and get user credentials for app.
  • .then( ) :
createUserWithEmailAndPassword(auth, 'EMAIL', 'PASSWORD')
  .then((UserCredential) => {
    const user = UserCredential.user;
  })
  .catch((error) => {
    console.log(error.message);
  });

  • async/await :
async function createNewUserAccount() {
  try {
    const userCredential = await createUserWithEmailAndPassword(
      auth,
      'EMAIL',
      'PASSWORD'
    );
    const user = userCredential.user;
  } catch (error) {
    console(error.message);
  }
}
createNewUserAccount();

Log-in Existing User : 


  • signInWithEmailAndPassword(auth, 'EMAIL', 'PASSWORD') :
import { auth } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { signInWithEmailAndPassword } from 'firebase/auth';

signInWithEmailAndPassword(auth, 'EMAIL', 'PASSWORD');

  • EMAIL & PASSWORD : Get Email address & password that is inputted by user and pass it in EMAIL & PASSWORD or you can pass it manually by typing it in the method.
This method return 'user credential' using 'Promise'. So using that credential you can populate or fetch its private data to show them.
It is also return Promise so it can be handled by '.then()' or 'async/await'.

Log-out User :


  • signOut(auth)
import { auth } from '<YOUR_FIREBASE_CONFIG_FILE_PATH>';
import { signOut } from 'firebase/auth';

signOut(auth);
To log-out any user you need to pass only 'auth' that is exported from 'firebaseConfig' after initializing Authentication. 

This method also return 'Promise' so you run functionality like redirecting user to log-in page after log-out using '.then()' or 'async/await'. 
signOut(auth)
  .then(() => {
    // code for redirect user to Log-in page
    // ...
  })
  .catch((error) => {
    console.log(error);
  });

Related Post
Setup and Initialize
Setup and Initialize Firebase Authentication
Setup and Initialize Firebase Authentication

1.Add Firebase SDK, 2.Initialize app, 3.Iniitialize Authentication using getAuth(app)

21 October 2024

Phone number
Firebase Phone Number Authentication
Firebase Phone Number Authentication

1. Enable Phone Number sign-in for your Firebase project, 2. Set up the reCAPTCHA verifier, 3. Send a verification code to the user's phone, 4. Sign in the user by verifying OTP code

21 October 2024

Email link (Passwordless)
Authenticate User Using Email Link (Passwordless)
Authenticate User Using Email Link (Passwordless)

1. Enable Email Link sign-in method, 2. Send an authentication link to the user's email address, 3. Sign-In user

21 October 2024

Email/Password
How to Signup/ Login/ Logout User In Firebase Authentication ?
How to Signup/ Login/ Logout User In Firebase Authentication ?

1.Create New User Account: 'createUserWithEmailAndPassword()' method, 2.Login: 'signInWithEmailAndPassword()' method, 3.Logout: 'signOut(auth)' method

21 October 2024

Google OAuth
Sign-In User using Google OAuth with Firebase
Sign-In User using Google OAuth with Firebase

Step 1. Enable Google sign-in method for your project, Step 2. Set up Sign-in flow.

21 October 2024

Company
Explore

Copyright © 2024, All rights reserved by Firebase Master

We value your privacy

We use cookies to enhance your browsing experience, serve personalized ads or content, and analyze our traffic. By clicking "Allow", you consent to our use of cookies. Here is our Privacy Policy.