[英]iOS Firebase propagate custom claims for anonymous auth
在“ Firestore数据库安全规则”中,我使用自定义声明检查。 他们工作正常。 在我的应用程序中,我使用匿名身份验证。 我按照指示进行操作,在我的第一个视图中,我有:
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult,NSError * _Nullable error) {
FIRUser *user = authResult.user;
NSLog(@"userId: %@", user.uid);}];
它可以正常工作,并且我也可以在控制台中看到一个userId。然后使用可调用的Cloud Functions,该函数使用其uid向用户添加一个自定义声明。 它有效,因为在我的iOS完成中,我请求getIDToken:
[user getIDTokenResultWithCompletion:^(FIRAuthTokenResult *result,NSError *error) {
if (error != nil) {
}
else{
NSLog(@"claims: %@",result.claims);
//here I start my queries}];
在此日志中,我看到我的自定义声明已应用,uid是正确的。 只有在此之后,我才开始查询Firestore数据库,但出现与安全规则有关的错误: Code = 7“缺少权限或权限不足”。
如果我停止了该应用程序并仅通过此查询重新启动它-它们将完美运行。 因此,时间安排毫无疑问。 我什至在从Cloud Functions响应(应用了索赔)后将查询推迟了一分钟,但是在第一次(不重新启动应用程序)中,查询未通过安全规则。
是否可以在不重新启动应用程序的情况下更新客户端中的匿名用户?
UPDATE似乎可以通过将Pod更新到最新版本来解决。 就我而言,我有5.0.2并更新为5.0.5
在这里,此代码用于获取和设置firebaseDB中的数据。
**view.h**
@属性(强,非原子)FIRDatabaseReference * refOnline;
**view.m**
NSUserDefaults* standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *userID = [NSString stringWithFormat:@"%@",[standardUserDefaults valueForKey:@"UserID"]];
self.refOnline = [[FIRDatabase database] reference];
self.refOnline =[[self.refOnline child:userID] child:@"isOnline"];
//[self.refOnline setValue:@"0"];
[self.refOnline observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSDictionary *dict = snapshot.value;
NSLog(@"%@",dict);
} withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
}];
NSLog(@" %@",[self.refOnline child:@"isOnline"]);
**For Set Value**
[self.refOnline setValue:@"0"];
设置观察者进行状态检查
FIRDatabaseReference *observRver = [[FIRDatabase database] reference];
observRver =[observRver child:[[mutDict valueForKey:@"UserID"] objectAtIndex:indexPath.row]];
[observRver observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
if (snapshot.value != [NSNull null]){
NSMutableArray *dict = snapshot.value;
NSString *isOnline = [dict valueForKey:@"isOnline"];
if([isOnline isEqualToString:@"1"]){
cell.img_on_offline.image = [UIImage imageNamed:@"green_on"];
cell.lbl_on_offline.text=@"Online";
} else {
cell.img_on_offline.image = [UIImage imageNamed:@"red_off"];
cell.lbl_on_offline.text=@"Offline";
}
} else {
FIRDatabaseReference *newRefrence = [[FIRDatabase database] reference];
newRefrence =[[newRefrence child:[[mutDict valueForKey:@"UserID"] objectAtIndex:indexPath.row]] child:@"isOnline"];
//[newRefrence setValue:@"1"];
NSLog(@" %@",[newRefrence child:@"isOnline"]);
}
} withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.