新用户与旧Firebase Facebook身份验证

时间:2016-02-28 04:06:54

标签: ios firebase

我使用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];
}

1 个答案:

答案 0 :(得分:2)

将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]
      }
    }];
  }];
}];

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