Authenticate with Firebase using Password-Based Accounts using Javascript Stay organized with collections Save and categorize content based on your preferences.
You can useFirebase Authentication to let your users authenticate withFirebase using their email addresses and passwords, and to manage your app'spassword-based accounts.
Before you begin
- Add Firebase to your JavaScript project.
- If you haven't yet connected your app to your Firebase project, do so from theFirebase console.
- Enable Email/Password sign-in:
- In theFirebase console, open theAuth section.
- On theSign in method tab, enable theEmail/password sign-in method and clickSave.
Create a password-based account
To create a new user account with a password, complete the following steps inyour app's sign-up page:
- When a new user signs up using your app's sign-up form, complete any new account validation steps that your app requires, such as verifying that the new account's password was correctly typed and meets your complexity requirements.
- Create a new account by passing the new user's email address and password to
createUserWithEmailAndPassword:If the new account was created, the user is signed in automatically. Have a look at the Next steps section below to get the signed in user details.Web
import{getAuth,createUserWithEmailAndPassword}from"firebase/auth";constauth=getAuth();createUserWithEmailAndPassword(auth,email,password).then((userCredential)=>{// Signed upconstuser=userCredential.user;// ...}).catch((error)=>{consterrorCode=error.code;consterrorMessage=error.message;// ..});
Web
firebase.auth().createUserWithEmailAndPassword(email,password).then((userCredential)=>{// Signed invaruser=userCredential.user;// ...}).catch((error)=>{varerrorCode=error.code;varerrorMessage=error.message;// ..});
This is also where you can catch and handle errors. For a list of error codes have a look at theAuth Reference Docs.
Sign in a user with an email address and password
The steps for signing in a user with a password are similar to the steps forcreating a new account. In your app's sign-in page, do the following:
- When a user signs in to your app, pass the user's email address and password to
signInWithEmailAndPassword:Have a look at the Next steps section below to get the signed in user details.Web
import{getAuth,signInWithEmailAndPassword}from"firebase/auth";constauth=getAuth();signInWithEmailAndPassword(auth,email,password).then((userCredential)=>{// Signed inconstuser=userCredential.user;// ...}).catch((error)=>{consterrorCode=error.code;consterrorMessage=error.message;});
Web
firebase.auth().signInWithEmailAndPassword(email,password).then((userCredential)=>{// Signed invaruser=userCredential.user;// ...}).catch((error)=>{varerrorCode=error.code;varerrorMessage=error.message;});
This is also where you can catch and handle errors. For a list of error codes have a look at theAuth Reference Docs.
Recommended: Set a password policy
You can improve account security by enforcing password complexity requirements.
To configure a password policy for your project, open thePassword policytab on the Authentication Settings page of theFirebase console:
Firebase Authentication password policies support the following password requirements:
Lowercase character required
Uppercase character required
Numeric character required
Non-alphanumeric character required
The following characters satisfy the non-alphanumeric character requirement:
^ $ * . [ ] { } ( ) ? " ! @ # % & / \ , > < ' : ; | _ ~Minimum password length (ranges from 6 to 30 characters; defaults to 6)
Maximum password length (maximum length of 4096 characters)
You can enable password policy enforcement in two modes:
Require: Attempts to sign up fail until the user updates to a passwordthat complies with your policy.
Notify: Users are allowed to sign up with a non-compliant password. Whenusing this mode, you should check if the user's password complies with thepolicy on the client side and prompt the user in some way to update theirpassword if it does not comply.
New users are always required to choose a password that complies with yourpolicy.
If you have active users, we recommend not enabling force upgrade on sign inunless you intend to block access to users whose passwords don't comply withyour policy. Instead, use notify mode, which allows users to sign in with theircurrent passwords, and inform them of the requirements their password lacks.
Validating a password on the client
import{getAuth,validatePassword}from"firebase/auth";conststatus=awaitvalidatePassword(getAuth(),passwordFromUser);if(!status.isValid){// Password could not be validated. Use the status to show what// requirements are met and which are missing.// If a criterion is undefined, it is not required by policy. If the// criterion is defined but false, it is required but not fulfilled by// the given password. For example:constneedsLowerCase=status.containsLowercaseLetter!==true;}Recommended: Enable email enumeration protection
SomeFirebase Authentication methods that take email addresses as parameters throwspecific errors if the email address is unregistered when it must be registered(for example, when signing in with an email address and password), or registeredwhen it must be unused (for example, when changing a user's email address).While this can be helpful for suggesting specific remedies to users, it can alsobe abused by malicious actors to discover the email addresses registered by yourusers.
To mitigate this risk, we recommend youenable email enumeration protectionfor your project using the Google Cloudgcloud tool. Note that enabling thisfeature changesFirebase Authentication's error reporting behavior: be sure your appdoesn't rely on the more specific errors.
Next steps
After a user signs in for the first time, a new user account is created andlinked to the credentials—that is, the user name and password, phonenumber, or auth provider information—the user signed in with. This newaccount is stored as part of your Firebase project, and can be used to identifya user across every app in your project, regardless of how the user signs in.
In your apps, the recommended way to know the auth status of your user is toset an observer on the
Authobject. You can then get the user'sbasic profile information from theUserobject. SeeManage Users.In yourFirebase Realtime Database andCloud StorageSecurity Rules, you can get the signed-in user's unique user ID from the
authvariable, and use it to control what data a user can access.
You can allow users to sign in to your app using multiple authenticationproviders bylinking auth provider credentials to anexisting user account.
To sign out a user, callsignOut:
Web
import{getAuth,signOut}from"firebase/auth";constauth=getAuth();signOut(auth).then(()=>{// Sign-out successful.}).catch((error)=>{// An error happened.});
Web
firebase.auth().signOut().then(()=>{// Sign-out successful.}).catch((error)=>{// An error happened.});
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-05 UTC.