Home / Articles / Blog

Login with Email + Password in 4 steps
FirebaseGoogle LoginAuthenticationFrontendReact.js

Login with Email + Password in 4 steps

Add Email + Password login via Firebase in just 4 steps

DatabaseShrey Vijayvargiya

Section 1

If you have reached here directly, Here is the link to the article => Connect Firebase to react project

Steps!! -

  1. Connect Firebase to your project.
  2. Add initial signup method for the new user using email and password.
  3. Add signup and sign-in method using Google Gmail.
  4. Add sign-in methods for users using email and password.

Section 2

Sign up with an Email and Password!!

Creating signups for new users is pretty simple, Firebase gives you a method createUserWithEmailAndPassword. This method helps to create a new user in the Authentication section of the Firebase console.

Before adding this method make sure you enable the Firebase email and password authentication method in the Sign-in-method tab section.

Sign in a tab section
Sign in a tab section

Enable sign-in providers via Email Password
Enable sign-in providers via Email Password

Create a user with Email + Password

Create a user with an Email and Password method
Create a user with an Email and Password method

Signup code

Signup code to invoke create user method
Signup code to invoke create user method

User data returned by Firebase after generating and storing the user information in the Firebase console 
User data returned by Firebase after generating and storing the user information in the Firebase console 

The Firebase users tab in the console app
The Firebase users tab in the console app

Section 3

Sign In with an Email and Password!!

Firebase provides the signInWithEmailAndPassword method, which again takes only email and password and returns the response accordingly.

Login with Email and Password method
Login with Email and Password method

user returned by Firebase after entering the correct credentials.
user returned by Firebase after entering the correct credentials.

Section 4

Login with Google

I will use the react-icons npm package for all sorts of icons, it's pretty simple to install plus they have a variety of icons.

I have created the Google login button.

Google the login Button in the login form
Google the login Button in the login form

Now, do the same step we did with email and password sign-in providers, go to the authentication sign-in method tab and enable the Google login.

Enable Google login providers in the Firebase console
Enable Google login providers in the Firebase console

Add your localhost and production domain, so that Firebase can give the domain to use the above provider's permission.
Add your localhost and production domain, so that Firebase can give the domain to use the above provider's permission.

Link - https://react-icons.github.io/react-iconsI have created the Google login button.Google the login Button in the login form Select an ImageNow, do the same step we did with email and password sign-in providers, go to the authentication sign-in method tab and enable the Google login.Enable Google login providers in the Firebase console Select an ImageAdd your localhost and production domain, so that Firebase can give the domain to use the above provider's permission. Select an ImageFirebase return the user data once logged in via the credentials successfully

After clicking to Google login button user will be thrown to a new tab which has the list of emails (left-hand side image), after selecting one email user will be thrown back to the main page and in the response then you will get the user data(right side image).

Google login is completed, now you can use the response data from Firebase in any way you want. I prefer to store the uid return from Firebase and token in the cookie which helps me to detect whether the user is logged in or not, if the cookie does not contain uid then the user is logged out. Also do set some expired days, for example, 14 days for every cookie value you will save in the browser.

I have added a cool loading button in every button and some covered some edge cases like clicking the button without entering an email or password and so on. It’s always a good approach to cover as many edge cases as you can, which gives the user a good user experience.

Demo

External Links (14)

Explore topics

More in

Weekly letter

Subscribe to iHateReading

Our once-a-week newsletter on programming, jobs, AI, and building products online.

Our once a week newsletter on Programming, Jobs, AI, and Business