iOS Firebase propagate custom claims for anonymous auth
up vote
1
down vote
favorite
In my Security Rules for Firestore database I use custom claims check. They work fine. In my app I use anonymous authentication. I followed the instruction, and in my first view I have:
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult,NSError * _Nullable error) {
FIRUser *user = authResult.user;
NSLog(@"userId: %@", user.uid);}];
It works and I get a userId which I can also see in console.Then I use a callable Cloud Functions which adds one custom claim to the user, using it's uid. It works, because in my iOS completion I request getIDToken:
[user getIDTokenResultWithCompletion:^(FIRAuthTokenResult *result,NSError *error) {
if (error != nil) {
}
else{
NSLog(@"claims: %@",result.claims);
//here I start my queries}];
In this log I see that my custom claim was applied, the uid is correct. Only after that I start my queries to the Firestore database, but I get an error related to Security Rules: Code=7 "Missing or insufficient permissions."
If I stop the app and just restart it with this queries - they work perfectly. So there is no question about the timing. I even postponed my queries for a minute after getting response from Cloud Functions (with claim applied) but in the first time (not restarting the app) queries do not pass security rules.
Is it possible to update Anonymous user in client without restarting the app?
UPDATE
Seems to be solved by updating pods to the latest version. In my case I had 5.0.2 and updated to 5.0.5
ios objective-c firebase firebase-authentication
add a comment |
up vote
1
down vote
favorite
In my Security Rules for Firestore database I use custom claims check. They work fine. In my app I use anonymous authentication. I followed the instruction, and in my first view I have:
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult,NSError * _Nullable error) {
FIRUser *user = authResult.user;
NSLog(@"userId: %@", user.uid);}];
It works and I get a userId which I can also see in console.Then I use a callable Cloud Functions which adds one custom claim to the user, using it's uid. It works, because in my iOS completion I request getIDToken:
[user getIDTokenResultWithCompletion:^(FIRAuthTokenResult *result,NSError *error) {
if (error != nil) {
}
else{
NSLog(@"claims: %@",result.claims);
//here I start my queries}];
In this log I see that my custom claim was applied, the uid is correct. Only after that I start my queries to the Firestore database, but I get an error related to Security Rules: Code=7 "Missing or insufficient permissions."
If I stop the app and just restart it with this queries - they work perfectly. So there is no question about the timing. I even postponed my queries for a minute after getting response from Cloud Functions (with claim applied) but in the first time (not restarting the app) queries do not pass security rules.
Is it possible to update Anonymous user in client without restarting the app?
UPDATE
Seems to be solved by updating pods to the latest version. In my case I had 5.0.2 and updated to 5.0.5
ios objective-c firebase firebase-authentication
Hope you have enabled the Anonymous user login for your firebase project.
– Prashant
Nov 20 at 12:00
Of couser, otherwise nothing would work: neither Cloud Functions (setCustomClaims) nor iOS code (signInAnonymously, getIdToken)
– Basylio
Nov 20 at 12:06
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In my Security Rules for Firestore database I use custom claims check. They work fine. In my app I use anonymous authentication. I followed the instruction, and in my first view I have:
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult,NSError * _Nullable error) {
FIRUser *user = authResult.user;
NSLog(@"userId: %@", user.uid);}];
It works and I get a userId which I can also see in console.Then I use a callable Cloud Functions which adds one custom claim to the user, using it's uid. It works, because in my iOS completion I request getIDToken:
[user getIDTokenResultWithCompletion:^(FIRAuthTokenResult *result,NSError *error) {
if (error != nil) {
}
else{
NSLog(@"claims: %@",result.claims);
//here I start my queries}];
In this log I see that my custom claim was applied, the uid is correct. Only after that I start my queries to the Firestore database, but I get an error related to Security Rules: Code=7 "Missing or insufficient permissions."
If I stop the app and just restart it with this queries - they work perfectly. So there is no question about the timing. I even postponed my queries for a minute after getting response from Cloud Functions (with claim applied) but in the first time (not restarting the app) queries do not pass security rules.
Is it possible to update Anonymous user in client without restarting the app?
UPDATE
Seems to be solved by updating pods to the latest version. In my case I had 5.0.2 and updated to 5.0.5
ios objective-c firebase firebase-authentication
In my Security Rules for Firestore database I use custom claims check. They work fine. In my app I use anonymous authentication. I followed the instruction, and in my first view I have:
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult,NSError * _Nullable error) {
FIRUser *user = authResult.user;
NSLog(@"userId: %@", user.uid);}];
It works and I get a userId which I can also see in console.Then I use a callable Cloud Functions which adds one custom claim to the user, using it's uid. It works, because in my iOS completion I request getIDToken:
[user getIDTokenResultWithCompletion:^(FIRAuthTokenResult *result,NSError *error) {
if (error != nil) {
}
else{
NSLog(@"claims: %@",result.claims);
//here I start my queries}];
In this log I see that my custom claim was applied, the uid is correct. Only after that I start my queries to the Firestore database, but I get an error related to Security Rules: Code=7 "Missing or insufficient permissions."
If I stop the app and just restart it with this queries - they work perfectly. So there is no question about the timing. I even postponed my queries for a minute after getting response from Cloud Functions (with claim applied) but in the first time (not restarting the app) queries do not pass security rules.
Is it possible to update Anonymous user in client without restarting the app?
UPDATE
Seems to be solved by updating pods to the latest version. In my case I had 5.0.2 and updated to 5.0.5
ios objective-c firebase firebase-authentication
ios objective-c firebase firebase-authentication
edited Nov 20 at 12:39
asked Nov 20 at 11:10
Basylio
2117
2117
Hope you have enabled the Anonymous user login for your firebase project.
– Prashant
Nov 20 at 12:00
Of couser, otherwise nothing would work: neither Cloud Functions (setCustomClaims) nor iOS code (signInAnonymously, getIdToken)
– Basylio
Nov 20 at 12:06
add a comment |
Hope you have enabled the Anonymous user login for your firebase project.
– Prashant
Nov 20 at 12:00
Of couser, otherwise nothing would work: neither Cloud Functions (setCustomClaims) nor iOS code (signInAnonymously, getIdToken)
– Basylio
Nov 20 at 12:06
Hope you have enabled the Anonymous user login for your firebase project.
– Prashant
Nov 20 at 12:00
Hope you have enabled the Anonymous user login for your firebase project.
– Prashant
Nov 20 at 12:00
Of couser, otherwise nothing would work: neither Cloud Functions (setCustomClaims) nor iOS code (signInAnonymously, getIdToken)
– Basylio
Nov 20 at 12:06
Of couser, otherwise nothing would work: neither Cloud Functions (setCustomClaims) nor iOS code (signInAnonymously, getIdToken)
– Basylio
Nov 20 at 12:06
add a comment |
1 Answer
1
active
oldest
votes
up vote
-1
down vote
Here this code for get and set data in firebaseDB.
**view.h**
@property (strong, nonatomic) 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"];
SET OBSERVER FOR STATUS CHECK
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);
}];
DATABASE LIKE THIS.
1
I believe it has nothing in common with the question
– Basylio
Nov 20 at 12:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391704%2fios-firebase-propagate-custom-claims-for-anonymous-auth%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
Here this code for get and set data in firebaseDB.
**view.h**
@property (strong, nonatomic) 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"];
SET OBSERVER FOR STATUS CHECK
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);
}];
DATABASE LIKE THIS.
1
I believe it has nothing in common with the question
– Basylio
Nov 20 at 12:08
add a comment |
up vote
-1
down vote
Here this code for get and set data in firebaseDB.
**view.h**
@property (strong, nonatomic) 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"];
SET OBSERVER FOR STATUS CHECK
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);
}];
DATABASE LIKE THIS.
1
I believe it has nothing in common with the question
– Basylio
Nov 20 at 12:08
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Here this code for get and set data in firebaseDB.
**view.h**
@property (strong, nonatomic) 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"];
SET OBSERVER FOR STATUS CHECK
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);
}];
DATABASE LIKE THIS.
Here this code for get and set data in firebaseDB.
**view.h**
@property (strong, nonatomic) 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"];
SET OBSERVER FOR STATUS CHECK
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);
}];
DATABASE LIKE THIS.
edited Nov 20 at 16:11
Mina Samy
7,2201275134
7,2201275134
answered Nov 20 at 11:41
Khushal iOS
755
755
1
I believe it has nothing in common with the question
– Basylio
Nov 20 at 12:08
add a comment |
1
I believe it has nothing in common with the question
– Basylio
Nov 20 at 12:08
1
1
I believe it has nothing in common with the question
– Basylio
Nov 20 at 12:08
I believe it has nothing in common with the question
– Basylio
Nov 20 at 12:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391704%2fios-firebase-propagate-custom-claims-for-anonymous-auth%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hope you have enabled the Anonymous user login for your firebase project.
– Prashant
Nov 20 at 12:00
Of couser, otherwise nothing would work: neither Cloud Functions (setCustomClaims) nor iOS code (signInAnonymously, getIdToken)
– Basylio
Nov 20 at 12:06