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

สร้างผู้ใช้

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

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

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

วิธีที่แนะนำในการทำให้ได้ผู้ใช้ปัจจุบันคือโดยการตั้งค่า Listener บนอุปกรณ์ ออบเจ็กต์การตรวจสอบสิทธิ์:

Swift

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

Objective-C

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

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

นอกจากนี้ คุณยังหาผู้ใช้ที่ลงชื่อเข้าใช้อยู่ได้โดยใช้ currentUser หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ currentUser จะเป็นค่าว่าง:

Swift

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

Objective-C

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

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

หากต้องการรับข้อมูลโปรไฟล์ของผู้ใช้ ให้ใช้พร็อพเพอร์ตี้ของอินสแตนซ์ FIRUser เช่น

Swift

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 += " "
  }
  // ...
}

Objective-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 เช่น

Swift

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

Objective-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 สำหรับ ตัวอย่าง:

Swift

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

Objective-C

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

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

คุณจะตั้งค่าอีเมลของผู้ใช้ได้โดยใช้เมธอด updateEmail เช่น

Swift

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

Objective-C

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

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

คุณสามารถส่งอีเมลยืนยันที่อยู่ให้กับผู้ใช้ที่มี sendEmailVerificationWithCompletion: วิธี เช่น

Swift

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

Objective-C

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

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

นอกจากนี้คุณยังส่งผ่านสถานะ URL ต่อ เพื่อเปลี่ยนเส้นทางกลับ เมื่อส่งอีเมลยืนยัน

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

Swift

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

Objective-C

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

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

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

Swift

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

Objective-C

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

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

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

Swift

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

Objective-C

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

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

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

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

Swift

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

Objective-C

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

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

ลบผู้ใช้

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

Swift

let user = Auth.auth().currentUser

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

Objective-C

FIRUser *user = [FIRAuth auth].currentUser;

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

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

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

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

Swift

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.
  }
}

Objective-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