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

สร้างผู้ใช้

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

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

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

วิธีที่แนะนำในการรับผู้ใช้ปัจจุบันคือการตั้งค่า Listener บนออบเจ็กต์ 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().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