Google 致力于为黑人社区推动种族平等。查看具体举措

在 Firebase 中管理用户

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

创建用户

您可以通过调用createUserWithEmailAndPassword方法或使用联合身份提供商(例如Google Sign-InFacebook Login )首次登录用户来在 Firebase 项目中创建新用户。

您还可以从Firebase 控制台的身份验证部分、用户页面或使用Admin SDK创建新的密码身份验证用户。

获取当前登录的用户

获取当前用户的推荐方法是在 Auth 对象上设置观察者:

Web version 9

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

Web version 8

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    var uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

通过使用观察者,您可以确保 Auth 对象在获取当前用户时不处于中间状态(例如初始化)。当您使用signInWithRedirect时, onAuthStateChanged观察者会等到getRedirectResult在触发之前解决。

您还可以使用currentUser属性获取当前登录的用户。如果用户未登录,则currentUser为空:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

if (user) {
  // User is signed in, see docs for a list of available properties
  // https://firebase.google.com/docs/reference/js/firebase.User
  // ...
} else {
  // No user is signed in.
}

Web version 8

const user = firebase.auth().currentUser;

if (user) {
  // User is signed in, see docs for a list of available properties
  // https://firebase.google.com/docs/reference/js/firebase.User
  // ...
} else {
  // No user is signed in.
}

获取用户的个人资料

要获取用户的个人资料信息,请使用User实例的属性。例如:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;
if (user !== null) {
  // The user object has basic properties such as display name, email, etc.
  const displayName = user.displayName;
  const email = user.email;
  const photoURL = user.photoURL;
  const emailVerified = user.emailVerified;

  // 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.
  const uid = user.uid;
}

Web version 8

const user = firebase.auth().currentUser;
if (user !== null) {
  // The user object has basic properties such as display name, email, etc.
  const displayName = user.displayName;
  const email = user.email;
  const photoURL = user.photoURL;
  const emailVerified = user.emailVerified;

  // 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.getIdToken() instead.
  const uid = user.uid;
}

获取用户的提供商特定的个人资料信息

要获取从链接到用户的登录提供程序检索的配置文件信息,请使用providerData属性。例如:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

if (user !== null) {
  user.providerData.forEach((profile) => {
    console.log("Sign-in provider: " + profile.providerId);
    console.log("  Provider-specific UID: " + profile.uid);
    console.log("  Name: " + profile.displayName);
    console.log("  Email: " + profile.email);
    console.log("  Photo URL: " + profile.photoURL);
  });
}

Web version 8

const user = firebase.auth().currentUser;

if (user !== null) {
  user.providerData.forEach((profile) => {
    console.log("Sign-in provider: " + profile.providerId);
    console.log("  Provider-specific UID: " + profile.uid);
    console.log("  Name: " + profile.displayName);
    console.log("  Email: " + profile.email);
    console.log("  Photo URL: " + profile.photoURL);
  });
}

更新用户的个人资料

您可以使用updateProfile方法更新用户的基本个人资料信息(用户的显示名称和个人资料照片 URL)。例如:

Web version 9

import { getAuth, updateProfile } from "firebase/auth";
const auth = getAuth();
updateProfile(auth.currentUser, {
  displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
  // Profile updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;

user.updateProfile({
  displayName: "Jane Q. User",
  photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
  // Update successful
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});  

设置用户的电子邮件地址

您可以使用updateEmail方法设置用户的电子邮件地址。例如:

Web version 9

import { getAuth, updateEmail } from "firebase/auth";
const auth = getAuth();
updateEmail(auth.currentUser, "user@example.com").then(() => {
  // Email updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;

user.updateEmail("user@example.com").then(() => {
  // Update successful
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

向用户发送验证电子邮件

您可以使用sendEmailVerification方法向用户发送地址验证电子邮件。例如:

Web version 9

import { getAuth, sendEmailVerification } from "firebase/auth";

const auth = getAuth();
sendEmailVerification(auth.currentUser)
  .then(() => {
    // Email verification sent!
    // ...
  });

Web version 8

firebase.auth().currentUser.sendEmailVerification()
  .then(() => {
    // Email verification sent!
    // ...
  });

您可以在电子邮件模板页面上自定义Firebase 控制台的身份验证部分中使用的电子邮件模板。请参阅 Firebase 帮助中心的电子邮件模板

发送验证电子邮件时,也可以通过继续 URL传递状态以重定向回应用程序。

此外,您可以通过在发送电子邮件之前更新 Auth 实例上的语言代码来本地化验证电子邮件。例如:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
auth.languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

Web version 8

firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

设置用户密码

您可以使用updatePassword方法设置用户的密码。例如:

Web version 9

import { getAuth, updatePassword } from "firebase/auth";

const auth = getAuth();

const user = auth.currentUser;
const newPassword = getASecureRandomPassword();

updatePassword(user, newPassword).then(() => {
  // Update successful.
}).catch((error) => {
  // An error ocurred
  // ...
});

Web version 8

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

user.updatePassword(newPassword).then(() => {
  // Update successful.
}).catch((error) => {
  // An error ocurred
  // ...
});

发送密码重置电子邮件

您可以使用sendPasswordResetEmail方法向用户发送密码重置电子邮件。例如:

Web version 9

import { getAuth, sendPasswordResetEmail } from "firebase/auth";

const auth = getAuth();
sendPasswordResetEmail(auth, email)
  .then(() => {
    // Password reset email sent!
    // ..
  })
  .catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
    // ..
  });

Web version 8

firebase.auth().sendPasswordResetEmail(email)
  .then(() => {
    // Password reset email sent!
    // ..
  })
  .catch((error) => {
    var errorCode = error.code;
    var errorMessage = error.message;
    // ..
  });

您可以在电子邮件模板页面上自定义Firebase 控制台的身份验证部分中使用的电子邮件模板。请参阅 Firebase 帮助中心的电子邮件模板

发送密码重置电子邮件时,还可以通过继续 URL传递状态以重定向回应用程序。

此外,您可以通过在发送电子邮件之前更新 Auth 实例上的语言代码来本地化密码重置电子邮件。例如:

Web version 9

import { getAuth } from "firebase/auth";

const auth = getAuth();
auth.languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

Web version 8

firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

您还可以从 Firebase 控制台发送密码重置电子邮件。

删除用户

您可以使用delete方法删除用户帐户。例如:

Web version 9

import { getAuth, deleteUser } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

deleteUser(user).then(() => {
  // User deleted.
}).catch((error) => {
  // An error ocurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;

user.delete().then(() => {
  // User deleted.
}).catch((error) => {
  // An error ocurred
  // ...
});

您还可以在Firebase 控制台的“用户”页面上的“身份验证”部分删除用户。

重新验证用户

某些安全敏感操作(例如删除帐户设置主电子邮件地址更改密码)要求用户最近登录。如果您执行这些操作之一,而用户登录时间过长,则操作失败并出现错误。发生这种情况时,通过从用户那里获取新的登录凭据并将凭据传递给reauthenticateWithCredential来重新验证用户。例如:

Web version 9

import { getAuth, reauthenticateWithCredential } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

// TODO(you): prompt the user to re-provide their sign-in credentials
const credential = promptForCredentials();

reauthenticateWithCredential(user, credential).then(() => {
  // User re-authenticated.
}).catch((error) => {
  // An error ocurred
  // ...
});

Web version 8

const user = firebase.auth().currentUser;

// TODO(you): prompt the user to re-provide their sign-in credentials
const credential = promptForCredentials();

user.reauthenticateWithCredential(credential).then(() => {
  // User re-authenticated.
}).catch((error) => {
  // An error occurred
  // ...
});

导入用户帐户

您可以使用 Firebase CLI 的auth:import命令将用户帐户从文件导入您的 Firebase 项目。例如:

firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14