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










share|improve this question
























  • 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

















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










share|improve this question
























  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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.
enter image description here






share|improve this answer



















  • 1




    I believe it has nothing in common with the question
    – Basylio
    Nov 20 at 12:08











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
});


}
});














draft saved

draft discarded


















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.
enter image description here






share|improve this answer



















  • 1




    I believe it has nothing in common with the question
    – Basylio
    Nov 20 at 12:08















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.
enter image description here






share|improve this answer



















  • 1




    I believe it has nothing in common with the question
    – Basylio
    Nov 20 at 12:08













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.
enter image description here






share|improve this answer














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.
enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








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














  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'