Skip to content

Latest commit

 

History

History
234 lines (168 loc) · 6.05 KB

3.1. Firebase Auth : Email and Password.md

File metadata and controls

234 lines (168 loc) · 6.05 KB

Firebase Authentication

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.

Firebase Authentication integrates tightly with other Firebase services, and it leverages industry standards like OAuth 2.0 and OpenID Connect, so it can be easily integrated with your custom backend.


Auth with Email and Password

1) Sign Up New User

Create a form that allows new users to register with your app using their email address and a password. When a user completes the form, validate the email address and password provided by the user, then pass them to the createUserWithEmailAndPassword method:

<script>
 
  var email="[email protected]";
  var password="password";
  
  //Create User with Email and Password
  firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    console.log(errorCode);
    console.log(errorMessage);
  });
  
</script>

2) Sign In User

Create a form that allows existing users to sign in using their email address and password. When a user completes the form, call the signInWithEmailAndPassword method:

<script>
  
  var email="[email protected]";
  var password="password";
  
  //Sign In User with Email and Password
  firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    console.log(errorCode);
    console.log(errorMessage);
});

  
</script>

3) Set an authentication state observer and get user data

For each of your app's pages that need information about the signed-in user, attach an observer to the global authentication object. This observer gets called whenever the user's sign-in state changes.

Attach the observer using the onAuthStateChanged method. When a user successfully signs in, you can get information about the user in the observer.

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    var displayName = user.displayName;
    var email = user.email;
    var emailVerified = user.emailVerified;
    var photoURL = user.photoURL;
    var isAnonymous = user.isAnonymous;
    var uid = user.uid;
    var providerData = user.providerData;
    // ...
  } else {
    // User is signed out.
    // ...
  }
});

4) Sign Out User

To sign out a user, call signOut:

firebase.auth().signOut().then(function() {
  // Sign-out successful.
  console.log('User Logged Out!');
}).catch(function(error) {
  // An error happened.
  console.log(error);
});

5) Get Current Signed-In User Details

You can also get the currently signed-in user by using the currentUser property. If a user isn't signed in, currentUser is null:

var user = firebase.auth().currentUser;

if (user) {
  // User is signed in.
  if (user != null) {
    name = user.displayName;
    email = user.email;
    photoUrl = user.photoURL;
    emailVerified = user.emailVerified;
    uid = user.uid;  // 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 User.getToken() instead.
  }
} else {
  // No user is signed in.
}

6) Update User Details

You can update a user's basic profile information—the user's display name and profile photo URL—with the updateProfile method. For example:

var user = firebase.auth().currentUser;

user.updateProfile({
  displayName: "Updated User's Name",
  photoURL: "https://example.com/user/profile.jpg"
}).then(function() {
  // Update successful.
  console.log('User Profile Updated Successfully');
}).catch(function(error) {
  // An error happened.
});

7) Set a user's email address

You can set a user's email address with the updateEmail method. For example:

var user = firebase.auth().currentUser;

user.updateEmail("[email protected]").then(function() {
  // Update successful.
}).catch(function(error) {
  // An error happened.
});

8) Send a user a verification email

You can send an address verification email to a user with the sendEmailVerification method. For example:

var user = firebase.auth().currentUser;

user.sendEmailVerification().then(function() {
  // Email sent.
}).catch(function(error) {
  // An error happened.
});

9) Set a user's password

You can set a user's password with the updatePassword method. For example:

var user = firebase.auth().currentUser;
var newPassword = getASecureRandomPassword();

user.updatePassword(newPassword).then(function() {
  // Update successful.
}).catch(function(error) {
  // An error happened.
});

10) Send a password reset email

You can send a password reset email to a user with the sendPasswordResetEmail method. For example:

var auth = firebase.auth();
var emailAddress = "[email protected]";

auth.sendPasswordResetEmail(emailAddress).then(function() {
  // Email sent.
  console.log('Email Sent');
}).catch(function(error) {
  // An error happened.
});

11) Delete User

You can delete a user account with the delete method. For example:

var user = firebase.auth().currentUser;

user.delete().then(function() {
  // User deleted.
  console.log('User Deleted');
}).catch(function(error) {
  // An error happened.
});

12) Retrieve ID tokens on clients

You can re-use that ID token to identify the user or device on your custom backend server.

firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
  // Send token to your backend via HTTPS
  // ...
}).catch(function(error) {
  // Handle error
});

You can also delete users from the Authentication section of the Firebase console, on the Users page.