新用户与旧的Firebase Facebook身份验证 [英] New User vs Old Firebase Facebook Authentication
问题描述
[MyUser authWithFacebookFromVC:self withCompletionBlock:^(FBSDKLoginManagerLoginResult * result, NSError错误){
if(!error&&!result.isCancelled){
[MyUser authFireBaseWithFacebookAccessToken:[[FBSDKAccessToken currentAccessToken] tokenString] withCompletionBlock:^(NSError * error,FAuthData * authData) {
if(error){
[self showAlertWithTitle:@ErrorwithDescription:error.localizedDescription];
} else {
// ***************
//用户已经登录/注册。无法区分哪个
// ***************
[self moveToMain];
}
}];
} else {
[self showAlertWithTitle:@ErrorwithDescription:error.localizedDescription];
}
}];
$ b $ //在MyUser.m
+(void)authWithFacebookFromVC:(UIViewController *)vc withCompletionBlock :( void(^)( FBSDKLoginManagerLoginResult *结果,NSError *错误))完成{
FBSDKLoginManager * facebookLogin = [[FBSDKLoginManager alloc] init];
[facebookLogin logInWithReadPermissions:@ [@email] fromViewController:vc handler:completion];
$ b #pragma mark - Facebook
$ b +(void)authFireBaseWithFacebookAccessToken:(NSString *)accessToken withCompletionBlock:(void(^)(NSError * error,FAuthData * authData))完成{
Firebase * ref = [[Firebase alloc] initWithUrl:kFireBaseURL];
[ref authWithOAuthProvider:@facebooktoken:accessToken withCompletionBlock:completion];
}
OAuth提供者在注册或登录之间没有区别。在OAuth中,这不是一件真正的事情:用户允许应用程序访问用户数据,或者它没有。
更有可能您尝试检测是否这是用户第一次访问应用程序。这在应用程序级别上更容易处理。大多数开发人员将其用户的信息存储在Firebase数据库中
a>。您可以使用这个事实来检测用户是否曾经使用过您的应用程序,方法是检查您的数据库中是否已经存在有关该用户的信息: Firebase * ref = [[Firebase alloc] initWithUrl:@https://< YOUR-FIREBASE-APP> .firebaseio。 COM];
[ref observeAuthEventWithBlock:^(FAuthData * authData){
if(authData){
//用户认证
NSLog(@%@,authData);
//在users节点下面用uid创建一个子路径
//创建一个如下所示的URL路径:
// - https ://<您-火力-APP> .firebaseio.com /用户/< UID>
Firebase * userRef = [[ref childByAppendingPath:@users]
childByAppendingPath:authData.uid];
$ b $ [userRef observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot * snapshot){
if(snapshot.value == [NSNull null]){
//创建一个新的用户词典访问用户信息
//由authData参数提供
NSDictionary * newUser = @ {
@provider:authData.provider,
@displayName:authData.providerData [@displayName]
};
[userRef setValue:newUser]
}
}];
}];
}];
这段代码是从上面链接的页面中修改的。
I'm using Firebase as my database. Is there a way to tell if a user has just signed up with Facebook authentication or already created an account and is logging in? I've read through the documentation with no luck.
- (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];
}
When you use Firebase Authentication with an OAuth provider there is no difference between "signing up" or "signing on". In OAuth that is not really a thing: either the user allowed the application access to the user data, or it didn't.
More likely you are trying to detect if this is the first time the user accesses the application. That is much easier to handle on the application level itself. Most developers store information about their users in their Firebase Database.
You can use this fact to detect if the user has used your application before, by checking if you already have information about that user in your 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]
}
}];
}];
}];
This code is modified from the page I linked above.
这篇关于新用户与旧的Firebase Facebook身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!