ios - 新用户与旧 Firebase Facebook 身份验证

标签 ios firebase

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 7 年前

我使用 Firebase 作为我的数据库。有没有办法判断用户是刚刚注册了 Facebook 身份验证还是已经创建了一个帐户并正在登录?我已经阅读了 documentation没有运气。

- (IBAction)facebook:(id)sender {
    [MyUser authWithFacebookFromVC:self withCompletionBlock:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (!error && !result.isCancelled) {
            [MyUser authFireBaseWithFacebookAccessToken:[[FBSDKAccessToken currentAccessToken] tokenString] withCompletionBlock:^(NSError *error, FAuthData *authData) {
                if (error) {
                    [self showAlertWithTitle:@"Error" withDescription:error.localizedDescription];
                } else {
                    // ***************
                    // User has logged in/signed up. Can't distinguish which
                    // ***************
                    [self moveToMain];
                }
            }];
        } else {
            [self showAlertWithTitle:@"Error" withDescription:error.localizedDescription];
        }
    }];
}


// In MyUser.m

+ (void)authWithFacebookFromVC:(UIViewController *)vc withCompletionBlock:(void (^)(FBSDKLoginManagerLoginResult *result, NSError *error))completion {
    FBSDKLoginManager *facebookLogin = [[FBSDKLoginManager alloc] init];
    [facebookLogin logInWithReadPermissions:@[@"email"] fromViewController:vc handler:completion];
}

#pragma mark - Facebook

+ (void)authFireBaseWithFacebookAccessToken:(NSString *)accessToken withCompletionBlock:(void (^)(NSError *error, FAuthData *authData))completion {
    Firebase *ref = [[Firebase alloc] initWithUrl:kFireBaseURL];
    [ref authWithOAuthProvider:@"facebook" token:accessToken withCompletionBlock:completion];
}

最佳答案

当您将 Firebase 身份验证与 OAuth 提供程序一起使用时,“注册”或“登录”之间没有区别。在 OAuth 中,这并不是真正的事情:要么用户允许应用程序访问用户数据,要么不允许。

您更有可能尝试检测这是否是用户第一次访问该应用程序。这在应用程序级别本身更容易处理。大多数开发者store information about their users in their Firebase Database .

您可以使用这个事实来检测用户之前是否使用过您的应用程序,方法是检查您的数据库中是否已经有关于该用户的信息:

Firebase *ref = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com"];
[ref observeAuthEventWithBlock:^(FAuthData *authData) {
  if (authData) {
    // user authenticated
    NSLog(@"%@", authData);

    // Create a child path with a key set to the uid underneath the "users" node
    // This creates a URL path like the following:
    //  - https://<YOUR-FIREBASE-APP>.firebaseio.com/users/<uid>
    Firebase *userRef = [[ref childByAppendingPath:@"users"]
                         childByAppendingPath:authData.uid];

    [userRef observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
      if (snapshot.value == [NSNull null]) {
        // Create a new user dictionary accessing the user's info
        // provided by the authData parameter
        NSDictionary *newUser = @{
                                  @"provider": authData.provider,
                                  @"displayName": authData.providerData[@"displayName"]
        };
        [userRef setValue:newUser]
      }
    }];
  }];
}];

此代码是根据我上面链接的页面修改的。

关于ios - 新用户与旧 Firebase Facebook 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35678559/

上一篇:ios - Objective-C - 简单数学的错误输出

下一篇:ios - 如何在 Mac/iOS 上获取当前语言环境的负号字符

相关文章:

iphone - 迭代 s UITableView 中的所有单元格

ios - 使用 Magical Record 进行核心数据迁移

android - 数据库错误 : The transaction was overridden by a subsequent set how to solve

angular - 如何将可观察值存储在变量或属性中

ios - DateFormatter 在设备上产生意外结果

ios - 限制用户只能输入七个逗号分隔的单词

ios - 数据被覆盖 : Core Data

ios - flutter myFriendlyChat 在 firebase google 登录集成后崩溃

android - lateinit RecyclerView是否需要在quickstart-android数据库应用中初始化?

firebase - 如何在节点中强制执行最大数量的子节点?