จัดการผู้ใช้ใน Firebase

สร้างผู้ใช้

คุณสร้างผู้ใช้ใหม่ในโครงการ Firebase ของคุณโดยเรียกเมธอด createUser หรือโดยการลงชื่อเข้าใช้ผู้ใช้เป็นครั้งแรกโดยใช้ผู้ให้บริการข้อมูลประจำตัวแบบรวมศูนย์ เช่น Google Sign-In หรือ Facebook Login

คุณยังสามารถสร้างผู้ใช้ที่ตรวจสอบสิทธิ์ด้วยรหัสผ่านใหม่ได้จากส่วนการตรวจสอบสิทธิ์ของ คอนโซล Firebase ในหน้าผู้ใช้

รับผู้ใช้ที่ลงชื่อเข้าใช้อยู่ในปัจจุบัน

วิธีที่แนะนำในการรับผู้ใช้ปัจจุบันคือการตั้งค่าผู้ฟังบนวัตถุ Auth:

สวิฟต์

handle = Auth.auth().addStateDidChangeListener { auth, user in
  // ...
}

วัตถุประสงค์-C

self.handle = [[FIRAuth auth]
    addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) {
      // ...
    }];

เมื่อใช้ Listener คุณจะมั่นใจได้ว่าวัตถุ Auth ไม่ได้อยู่ในสถานะระดับกลาง เช่น การเริ่มต้น เมื่อคุณได้รับผู้ใช้ปัจจุบัน

คุณยังสามารถรับผู้ใช้ที่ลงชื่อเข้าใช้อยู่โดยใช้คุณสมบัติ currentUser หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ ผู้ใช้ currentUser จะเป็นศูนย์:

สวิฟต์

if Auth.auth().currentUser != nil {
  // User is signed in.
  // ...
} else {
  // No user is signed in.
  // ...
}

วัตถุประสงค์-C

if ([FIRAuth auth].currentUser) {
  // User is signed in.
  // ...
} else {
  // No user is signed in.
  // ...
}

รับโปรไฟล์ของผู้ใช้

ในการรับข้อมูลโปรไฟล์ของผู้ใช้ ให้ใช้คุณสมบัติของอินสแตนซ์ของ FIRUser ตัวอย่างเช่น:

สวิฟต์

let user = Auth.auth().currentUser
if let user = user {
  // 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 getTokenWithCompletion:completion: instead.
  let uid = user.uid
  let email = user.email
  let photoURL = user.photoURL
  var multiFactorString = "MultiFactor: "
  for info in user.multiFactor.enrolledFactors {
    multiFactorString += info.displayName ?? "[DispayName]"
    multiFactorString += " "
  }
  // ...
}

วัตถุประสงค์-C

FIRUser *user = [FIRAuth auth].currentUser;
if (user) {
  // 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 getTokenWithCompletion:completion: instead.
  NSString *email = user.email;
  NSString *uid = user.uid;
  NSMutableString *multiFactorString = [NSMutableString stringWithFormat:@"MultiFactor: "];
  for (FIRMultiFactorInfo *info in user.multiFactor.enrolledFactors) {
    [multiFactorString appendString:info.displayName];
    [multiFactorString appendString:@" "];
  }
  NSURL *photoURL = user.photoURL;
  // ...
}

รับข้อมูลโปรไฟล์เฉพาะของผู้ให้บริการของผู้ใช้

หากต้องการรับข้อมูลโปรไฟล์ที่ดึงมาจากผู้ให้บริการลงชื่อเข้าใช้ที่ลิงก์กับผู้ใช้ ให้ใช้คุณสมบัติ providerData ตัวอย่างเช่น:

สวิฟต์

let userInfo = Auth.auth().currentUser?.providerData[indexPath.row]
cell?.textLabel?.text = userInfo?.providerID
// Provider-specific UID
cell?.detailTextLabel?.text = userInfo?.uid

วัตถุประสงค์-C

id<FIRUserInfo> userInfo = [FIRAuth auth].currentUser.providerData[indexPath.row];
cell.textLabel.text = [userInfo providerID];
// Provider-specific UID
cell.detailTextLabel.text = [userInfo uid];

อัปเดตโปรไฟล์ของผู้ใช้

คุณสามารถอัปเดตข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ ได้แก่ ชื่อที่แสดงของผู้ใช้และ URL รูปภาพโปรไฟล์ ด้วยคลาส UserProfileChangeRequest ตัวอย่างเช่น:

สวิฟต์

let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = displayName
changeRequest?.commitChanges { error in
  // ...
}

วัตถุประสงค์-C

FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest];
changeRequest.displayName = userInput;
[changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
  // ...
}];

ตั้งค่าที่อยู่อีเมลของผู้ใช้

คุณสามารถตั้งค่าที่อยู่อีเมลของผู้ใช้ด้วยวิธี updateEmail ตัวอย่างเช่น:

สวิฟต์

Auth.auth().currentUser?.updateEmail(to: email) { error in
  // ...
}

วัตถุประสงค์-C

[[FIRAuth auth].currentUser updateEmail:userInput completion:^(NSError *_Nullable error) {
  // ...
}];

ส่งอีเมลยืนยันถึงผู้ใช้

คุณสามารถส่งอีเมลยืนยันที่อยู่ไปยังผู้ใช้ด้วยเมธอด sendEmailVerificationWithCompletion: ตัวอย่างเช่น:

สวิฟต์

Auth.auth().currentUser?.sendEmailVerification { error in
  // ...
}

วัตถุประสงค์-C

[[FIRAuth auth].currentUser sendEmailVerificationWithCompletion:^(NSError *_Nullable error) {
  // ...
}];

คุณสามารถปรับแต่งเทมเพลตอีเมลที่ใช้ในส่วนการรับรองความถูกต้องของ คอนโซล Firebase ในหน้าเทมเพลตอีเมล ดู เทมเพลตอีเมล ในศูนย์ช่วยเหลือ Firebase

นอกจากนี้ยังสามารถส่งผ่านสถานะผ่าน URL ดำเนิน การต่อเพื่อเปลี่ยนเส้นทางกลับไปที่แอปเมื่อส่งอีเมลยืนยัน

นอกจากนี้ คุณสามารถแปลอีเมลยืนยันเป็นภาษาท้องถิ่นได้โดยอัปเดตรหัสภาษาในอินสแตนซ์ Auth ก่อนส่งอีเมล ตัวอย่างเช่น:

สวิฟต์

Auth.auth().languageCode = "fr"
// To apply the default app language instead of explicitly setting it.
// Auth.auth().useAppLanguage()

วัตถุประสงค์-C

[FIRAuth auth].languageCode = @"fr";
// To apply the default app language instead of explicitly setting it.
// [[FIRAuth auth] useAppLanguage];

ตั้งรหัสผ่านของผู้ใช้

คุณสามารถตั้งรหัสผ่านของผู้ใช้ด้วยวิธี updatePassword ตัวอย่างเช่น:

สวิฟต์

Auth.auth().currentUser?.updatePassword(to: password) { error in
  // ...
}

วัตถุประสงค์-C

[[FIRAuth auth].currentUser updatePassword:userInput completion:^(NSError *_Nullable error) {
  // ...
}];

ส่งอีเมลรีเซ็ตรหัสผ่าน

คุณสามารถส่งอีเมลรีเซ็ตรหัสผ่านให้กับผู้ใช้ด้วยวิธี sendPasswordReset ตัวอย่างเช่น:

สวิฟต์

Auth.auth().sendPasswordReset(withEmail: email) { error in
  // ...
}

วัตถุประสงค์-C

[[FIRAuth auth] sendPasswordResetWithEmail:userInput completion:^(NSError *_Nullable error) {
  // ...
}];

คุณสามารถปรับแต่งเทมเพลตอีเมลที่ใช้ในส่วนการรับรองความถูกต้องของ คอนโซล Firebase ในหน้าเทมเพลตอีเมล ดู เทมเพลตอีเมล ในศูนย์ช่วยเหลือ Firebase

นอกจากนี้ยังสามารถส่งผ่านสถานะผ่าน URL ดำเนิน การต่อเพื่อเปลี่ยนเส้นทางกลับไปที่แอปเมื่อส่งอีเมลรีเซ็ตรหัสผ่าน

นอกจากนี้ คุณสามารถแปลอีเมลรีเซ็ตรหัสผ่านเป็นภาษาท้องถิ่นได้โดยอัปเดตรหัสภาษาบนอินสแตนซ์ Auth ก่อนส่งอีเมล ตัวอย่างเช่น:

สวิฟต์

Auth.auth().languageCode = "fr"
// To apply the default app language instead of explicitly setting it.
// Auth.auth().useAppLanguage()

วัตถุประสงค์-C

[FIRAuth auth].languageCode = @"fr";
// To apply the default app language instead of explicitly setting it.
// [[FIRAuth auth] useAppLanguage];

คุณยังสามารถส่งอีเมลรีเซ็ตรหัสผ่านจากคอนโซล Firebase

ลบผู้ใช้

คุณสามารถลบบัญชีผู้ใช้ด้วยวิธีการ delete ตัวอย่างเช่น:

สวิฟต์

let user = Auth.auth().currentUser

user?.delete { error in
  if let error = error {
    // An error happened.
  } else {
    // Account deleted.
  }
}

วัตถุประสงค์-C

FIRUser *user = [FIRAuth auth].currentUser;

[user deleteWithCompletion:^(NSError *_Nullable error) {
  if (error) {
    // An error happened.
  } else {
    // Account deleted.
  }
}];

คุณยังสามารถลบผู้ใช้จากส่วนการรับรองความถูกต้องของ คอนโซล Firebase ในหน้าผู้ใช้

ตรวจสอบผู้ใช้อีกครั้ง

การดำเนินการบางอย่างที่คำนึงถึงความปลอดภัย เช่น การลบบัญชี การตั้งค่าที่อยู่อีเมลหลัก และ การเปลี่ยนรหัสผ่าน กำหนดให้ผู้ใช้เพิ่งลงชื่อเข้าใช้ หากคุณดำเนินการอย่างใดอย่างหนึ่งเหล่านี้ และผู้ใช้ลงชื่อเข้าใช้นานเกินไป การดำเนินการล้มเหลวด้วยข้อผิดพลาด FIRAuthErrorCodeCredentialTooOld เมื่อสิ่งนี้เกิดขึ้น ให้ตรวจสอบสิทธิ์ผู้ใช้อีกครั้งโดยรับข้อมูลรับรองการลงชื่อเข้าใช้ใหม่จากผู้ใช้ และส่งข้อมูลรับรองเพื่อ reauthenticate ตัวตนอีกครั้ง ตัวอย่างเช่น:

สวิฟต์

let user = Auth.auth().currentUser
var credential: AuthCredential

// Prompt the user to re-provide their sign-in credentials

user?.reauthenticate(with: credential) { error in
  if let error = error {
    // An error happened.
  } else {
    // User re-authenticated.
  }
}

วัตถุประสงค์-C

FIRUser *user = [FIRAuth auth].currentUser;
FIRAuthCredential *credential;

// Prompt the user to re-provide their sign-in credentials

[user reauthenticateWithCredential:credential completion:^(NSError *_Nullable error) {
  if (error) {
    // An error happened.
  } else {
    // User re-authenticated.
  }
}];

นำเข้าบัญชีผู้ใช้

คุณสามารถนำเข้าบัญชีผู้ใช้จากไฟล์ไปยังโปรเจ็กต์ Firebase ได้โดยใช้คำสั่ง auth:import ของ Firebase CLI ตัวอย่างเช่น:

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