简体   繁体   中英

Google Firebase forget password

How do you implement forget password method in my method. I am creating a HTML project which is due soon. My code:

    function toggleSignIn() {
   if (!firebase.auth().currentUser) {
// [START createprovider]
var provider = new firebase.auth.GoogleAuthProvider();
// [END createprovider]
// [START addscopes]
provider.addScope('https://www.googleapis.com/auth/plus.login');
// [END addscopes]
// [START signin]
firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Google Access Token. You can use it to access the    Google API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  // [START_EXCLUDE]
  document.getElementById('quickstart-oauthtoken').textContent = token;
  // [END_EXCLUDE]
}).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential; 
  // [START_EXCLUDE]
  if (errorCode === 'auth/account-exists-with-different-credential') {
    alert("You have already signed up with a different auth provider for that email.");
    // If you are using multiple auth providers on your app you should handle linking
    // the user's accounts here.
  }
else if (errorCode === 'auth/auth-domain-config-required') {
alert("An auth domain configuration is required"); 
  }
  else if (errorCode === 'auth/cancelled-popup-request') {
      alert("Popup Google sign in was canceled");
  }
  else if (errorCode === 'auth/operation-not-allowed') {
      alert("Operation is not allowed");
  }
  else if (errorCode === 'auth/operation-not-supported-in-this-environment')      {
      alert("Operation is not supported in this environment");
  }
  else if (errorCode === 'auth/popup-blocked') {
      alert("Sign in popup got blocked");
  }
  else if (errorCode === 'auth/popup-closed-by-user') {
      alert("Google sign in popup got cancelled");
  }
  else if (errorCode === 'auth/unauthorized-domain') {
      alert("Unauthorized domain");
  }
   else {
    console.error(error);
  }
  // [END_EXCLUDE]
  });
  // [END signin]
  } else {
     // [START signout]
    firebase.auth().signOut();
    // [END signout]
    }
   // [START_EXCLUDE]
  document.getElementById('quickstart-sign-ing').disabled = false;
  // [END_EXCLUDE]
 }

Here is a link to give you guidance: https://firebase.google.com/docs/auth/web/manage-users#set_a_users_password Please can you help me

To implement a forgot password button, you have to call: firebase.auth().sendPasswordResetEmail('user@example.com')

Check the documentation for more details: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#sendPasswordResetEmail

The easiest way to implement recover forgot password functionality is to call

import { AngularFireAuth } from "@angular/fire/auth";

constructor(
    public afAuth: AngularFireAuth
  ) {  }

  // Reset Forggot password
  ForgotPassword(passwordResetEmail) {
    return this.afAuth.auth.sendPasswordResetEmail(passwordResetEmail)
    .then(() => {
      window.alert('Password reset email sent, check your inbox.');
    }).catch((error) => {
      window.alert(error)
    })
  }

To implement firebase reset you first need to send email to the user and for that we use sendPasswordResetEmail method. after that if user gonna click on the email, it will be redirected to a default page provided by firebase from there user can reset their password.

But if you want to have custom page for the password reset you can do that too, to do that you need to change the action url in the email template of your firebase project then you need to setup that custom action url, for this you can read this offical doc page: https://firebase.google.com/docs/auth/custom-email-handler?hl=en&authuser=0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM