使用Phone Auth注册时如何取消注册/取消注册Firebase用户

时间:2017-11-15 15:39:45

标签: objective-c firebase firebase-authentication

我已使用Firebase方便注册用户FUIPhoneAuth。当我尝试删除用户时:

[user deleteWithCompletion:^(NSError * _Nullable error) ..

我收到一条错误消息,指出可以删除最近才认证的用户。

"This operation is sensitive and requires recent authentication. Log in again before retrying this request."    

为了取消注册用户,我首先需要重新验证需要FIRAuthCredential“凭证”参数的用户:

[user reauthenticateWithCredential:credential completion:^(NSError *_Nullable error) {

Firebase doc提到在调用委托时应该保存FIRAuthCredential。(将其存储在NSUserDefaults中)

[[FIRPhoneAuthProvider provider] verifyPhoneNumber:userInput
                                    UIDelegate:nil
                                    completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {

我已将FUIPhoneAuth与signInWithPresentingViewController一起使用:并且在注册时未收到FIRAuthCredential。 我想如果我能掌握“FIRAuthCredential”,我可以重复使用它 取消注册用户。但是当使用 FUIPhoneAuth 时,这似乎是不可能的。 *如何取消 FIRAuthCredential 以取消注册用户?*

  1. 可以重新使用“FIRAuthCredential ”,或者在用户可以使用之前需要使用SMS进行完整的重新身份验证 删除吗

  2. 如何在使用 FUIPhoneAuth 进行注册时取消注册用户?

3 个答案:

答案 0 :(得分:0)

按照正常情况重新进行身份验证,然后调用deleteWithCompletion。 由于您已经拥有单元格编号,因此无需重新获取。

FIRAuth *auth = [(AppDelegate*)[UIApplication sharedApplication].delegate auth];
FIRPhoneAuthProvider *provider = [FIRPhoneAuthProvider providerWithAuth:auth];

[provider verifyPhoneNumber:user.phoneNumber
                 UIDelegate:nil
                 completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
                        if (error) {
                            return;
                        }

                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Verification"
                                                                                       message:@"Enter the SMS verfication code" preferredStyle:UIAlertControllerStyleAlert];

                        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                            textField.placeholder = @"SMS Code";
                            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
                            textField.borderStyle = UITextBorderStyleRoundedRect;
                        }];

                        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                                                style:UIAlertActionStyleDefault
                                                                              handler:^(UIAlertAction * action) {

                                                                                  NSArray *textfields = alert.textFields;
                                                                                  UITextField *codeField = textfields[0];

                                                                                  FIRAuthCredential *credential = [provider credentialWithVerificationID:verificationID verificationCode:codeField.text];

                                                                                  [user reauthenticateWithCredential:credential completion:^(NSError * _Nullable error) {
                                                                                      // unregister
                                                                                      [user deleteWithCompletion:^(NSError * _Nullable error) {
                                                                                          if (error) {
                                                                                              NSLog(@"FIR: User delete failed: %@",error.localizedDescription);

                                                                                          }
                                                                                          NSLog(@"FIR: User Deleted success");
                                                                                      }];
                                                                                  }];
                                                                              }];

                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler: ^(UIAlertAction * action) {
                        }];

                        [alert addAction:defaultAction];
                        [alert addAction:cancelAction];
                        [self presentViewController:alert animated:YES completion:nil];

                    }];

答案 1 :(得分:0)

我不确定文档在NSUserDefaults中提到保存凭据的位置,但通常不应该在那里存储敏感信息。如果你出于某种原因必须使用钥匙串。

您必须了解身份验证验证是两回事。每次登录应用程序时都会进行身份验证(生成凭据/令牌等)。另一方面,验证是可选的,在大多数情况下,应该在用户的应用内生命周期中仅发生一次(每种验证方法)。

例如,当您注册为Facebook用户时,您只验证一次电子邮件,您也可以只使用短信验证一次电话号码,但每次登录时都会通过身份验证。

第一回答: 因此,要回答您的第一个问题,删除用户所需的是重新身份验证,这完全独立于验证。在停用您的帐户或尝试进行任何重大更改之前,您可能会将此与Facebook相关联,询问您的密码(即使您已登录)。

第二回答: 从第二个问题开始,获取用户凭据取决于凭据的类型(您使用电子邮件/密码吗?Google / Facebook / Twitter?)。每种方法都有自己的“重新创建”凭证的方式,可以这么说。

Email/Password

FIRAuthCredential *credential = [FIREmailPasswordAuthProvider credentialWithEmail: email password: password];

Facebook

FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken: fbTokenString];

Google

FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken: token accessToken: accessToken];

Twitter

FIRAuthCredential *credential = [FIRTwitterAuthProvider credentialWithToken: session.authToken secret: session.authTokenSecret];

如果您使用电子邮件/密码凭据,我的回答here可能会提示用户重新输入密码。

您可以像在帖子中那样使用新生成的凭据重新进行身份验证(这里是relevant documentation),然后您可以在重新身份验证完成块中使用deleteWithCompletion

<强>更新

您提供的链接指出您应将“验证ID”存储在NSUserDefaults中,而不是身份验证凭据中。验证ID和验证码(在SMS中发送的内容)一起用于获取身份验证凭据然后登录。您没有 来存储它,他们只是建议你做以防万一切换到Messages应用程序时,应用程序将重新启动。您可以在用户登录后安全删除验证ID。

截至您的第二条评论,您可以通过重新启动验证过程(您提到的链接的part;发送短信,获取用户输入,然后拨打credentialWithVerificationID)来获取身份验证凭据。您应该在本地保存凭据,因为它存在安全风险,并且无论如何都会过期。仅临时保存验证ID,直到用户输入验证码并进行验证。

答案 2 :(得分:0)

请不要在设备上存储凭据。你误读了文档。它需要重新认证是有原因的。存储凭证胜过了这一点。您只需重新验证用户即可删除它们。这类似于要求电子邮件/密码用户在删除帐户,更新密码,更新电子邮件,更新送货地址,信用卡信息等之前再次提供其当前密码。

此外,即使您存储这些电话凭证:

  • 他们将在短短几分钟内过期。
  • 它们只能使用一次所以在登录后握住它们是没用的。
相关问题