Angular7 AuthGuard Firebase Claims Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Angular 2 AuthGuard + Firebase AuthFirebase social login - verify emailAdding an API call to canActivate in Angular 2 AuthGuardangular 4 router auth-guard not invoked on same routeCreate users with roles on client side with firebaseHow to securely manage my own custom claims alongside Firebase AuthenticationFirebase Custom ClaimsAngular AuthGuard CanActivate not called when signing out - Firebase AuthHow to get users from Firebase auth based on custom claims?Multiple /Dashboard routes for different Roles With AuthGuard
My bank got bought out, am I now going to have to start filing tax returns in a different state?
Is accepting an invalid credit card number a security issue?
All ASCII characters with a given bit count
Where did Arya get these scars?
What is /etc/mtab in Linux?
Why did Israel vote against lifting the American embargo on Cuba?
Protagonist's race is hidden - should I reveal it?
Need of separate security plugins for both root and subfolder sites Wordpress?
A strange hotel
What is ls Largest Number Formed by only moving two sticks in 508?
Multiple fireplaces in an apartment building?
With indentation set to `0em`, when using a line break, there is still an indentation of a size of a space
Trumpet valves, lengths, and pitch
How to find the right literary agent in the USA?
Raising a bilingual kid. When should we introduce the majority language?
A Paper Record is What I Hamper
What is the term for a person whose job is to place products on shelves in stores?
What is it called when you ride around on your front wheel?
Can I criticise the more senior developers around me for not writing clean code?
Split coins into combinations of different denominations
Implementing 3DES algorithm in Java: is my code secure?
"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"
SQL Query not selecting all points that it should?
Are these square matrices always diagonalisable?
Angular7 AuthGuard Firebase Claims
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Angular 2 AuthGuard + Firebase AuthFirebase social login - verify emailAdding an API call to canActivate in Angular 2 AuthGuardangular 4 router auth-guard not invoked on same routeCreate users with roles on client side with firebaseHow to securely manage my own custom claims alongside Firebase AuthenticationFirebase Custom ClaimsAngular AuthGuard CanActivate not called when signing out - Firebase AuthHow to get users from Firebase auth based on custom claims?Multiple /Dashboard routes for different Roles With AuthGuard
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am able to login using firebase, however in my app I have 3 levels of users. Let's call them admin, user, editor. I keep user's role in custom claims provided by Firebase.
In AuthGuard, I pass data expectedRoles = ['admin', 'editor']
, adding roles I want to allow for a specific route. And I want to do redirecting to one's own page, such as editor trying admin route will be returned to editor dashboard.
This is my AuthGuard's canActivate
function:
return this.auth.user.pipe(
map(user =>
if (user)
user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Before adding user.getIdTokenResult().then(...)
it was working fine, and I am aware it is probably due to not waiting the async call to parse the custom claims. How can I work around this?
angular firebase asynchronous auth-guard
add a comment |
I am able to login using firebase, however in my app I have 3 levels of users. Let's call them admin, user, editor. I keep user's role in custom claims provided by Firebase.
In AuthGuard, I pass data expectedRoles = ['admin', 'editor']
, adding roles I want to allow for a specific route. And I want to do redirecting to one's own page, such as editor trying admin route will be returned to editor dashboard.
This is my AuthGuard's canActivate
function:
return this.auth.user.pipe(
map(user =>
if (user)
user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Before adding user.getIdTokenResult().then(...)
it was working fine, and I am aware it is probably due to not waiting the async call to parse the custom claims. How can I work around this?
angular firebase asynchronous auth-guard
add a comment |
I am able to login using firebase, however in my app I have 3 levels of users. Let's call them admin, user, editor. I keep user's role in custom claims provided by Firebase.
In AuthGuard, I pass data expectedRoles = ['admin', 'editor']
, adding roles I want to allow for a specific route. And I want to do redirecting to one's own page, such as editor trying admin route will be returned to editor dashboard.
This is my AuthGuard's canActivate
function:
return this.auth.user.pipe(
map(user =>
if (user)
user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Before adding user.getIdTokenResult().then(...)
it was working fine, and I am aware it is probably due to not waiting the async call to parse the custom claims. How can I work around this?
angular firebase asynchronous auth-guard
I am able to login using firebase, however in my app I have 3 levels of users. Let's call them admin, user, editor. I keep user's role in custom claims provided by Firebase.
In AuthGuard, I pass data expectedRoles = ['admin', 'editor']
, adding roles I want to allow for a specific route. And I want to do redirecting to one's own page, such as editor trying admin route will be returned to editor dashboard.
This is my AuthGuard's canActivate
function:
return this.auth.user.pipe(
map(user =>
if (user)
user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Before adding user.getIdTokenResult().then(...)
it was working fine, and I am aware it is probably due to not waiting the async call to parse the custom claims. How can I work around this?
angular firebase asynchronous auth-guard
angular firebase asynchronous auth-guard
asked Mar 22 at 15:53
AlpAlp
52211
52211
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use switchMap
instead of map, make the function async
and await the result. switchMap
accepts the promise that is returned by the async
function. Then inside you can user await
.
return this.auth.user.pipe(
// switchMap instead of map
// async function call
switchMap(async (user) =>
if (user)
// await here
await user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Just a hint unrelated to this solution - better than load token claim in the AuthGuard function, you should load it after the user logs in and store it in the store the same way you store the information from the login response. This will be much faster.
Thanks for your detailed answer. I wanted to the thing you mentioned in the bottom but I had a problem that it was getting destroyed when I open the app on new tab for example, while auth keeps there role is destroyed. I kept role variable in auth service and called to get it in constructor of the service. Do I have to do it other way?
– Alp
Mar 22 at 17:43
Yes, you need to load it each time when the app starts - even in new tab. You could use your auth service and store the roles in a behaviour subject so id does not load again. You could use localStorage to save it as well. But that's another discussion.
– kvetis
Mar 25 at 12:29
add a comment |
Try using the rxjs pipes for inner observable calls: flatMap and flatMapTo or mergeMap or mergeMapTo. In your specific case I think mergeMap is the way to go.
Feel free to read about those pipes in rxjs, they might be the solution
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',
autoActivateHeartbeat: false,
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%2f55303417%2fangular7-authguard-firebase-claims%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use switchMap
instead of map, make the function async
and await the result. switchMap
accepts the promise that is returned by the async
function. Then inside you can user await
.
return this.auth.user.pipe(
// switchMap instead of map
// async function call
switchMap(async (user) =>
if (user)
// await here
await user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Just a hint unrelated to this solution - better than load token claim in the AuthGuard function, you should load it after the user logs in and store it in the store the same way you store the information from the login response. This will be much faster.
Thanks for your detailed answer. I wanted to the thing you mentioned in the bottom but I had a problem that it was getting destroyed when I open the app on new tab for example, while auth keeps there role is destroyed. I kept role variable in auth service and called to get it in constructor of the service. Do I have to do it other way?
– Alp
Mar 22 at 17:43
Yes, you need to load it each time when the app starts - even in new tab. You could use your auth service and store the roles in a behaviour subject so id does not load again. You could use localStorage to save it as well. But that's another discussion.
– kvetis
Mar 25 at 12:29
add a comment |
Use switchMap
instead of map, make the function async
and await the result. switchMap
accepts the promise that is returned by the async
function. Then inside you can user await
.
return this.auth.user.pipe(
// switchMap instead of map
// async function call
switchMap(async (user) =>
if (user)
// await here
await user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Just a hint unrelated to this solution - better than load token claim in the AuthGuard function, you should load it after the user logs in and store it in the store the same way you store the information from the login response. This will be much faster.
Thanks for your detailed answer. I wanted to the thing you mentioned in the bottom but I had a problem that it was getting destroyed when I open the app on new tab for example, while auth keeps there role is destroyed. I kept role variable in auth service and called to get it in constructor of the service. Do I have to do it other way?
– Alp
Mar 22 at 17:43
Yes, you need to load it each time when the app starts - even in new tab. You could use your auth service and store the roles in a behaviour subject so id does not load again. You could use localStorage to save it as well. But that's another discussion.
– kvetis
Mar 25 at 12:29
add a comment |
Use switchMap
instead of map, make the function async
and await the result. switchMap
accepts the promise that is returned by the async
function. Then inside you can user await
.
return this.auth.user.pipe(
// switchMap instead of map
// async function call
switchMap(async (user) =>
if (user)
// await here
await user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Just a hint unrelated to this solution - better than load token claim in the AuthGuard function, you should load it after the user logs in and store it in the store the same way you store the information from the login response. This will be much faster.
Use switchMap
instead of map, make the function async
and await the result. switchMap
accepts the promise that is returned by the async
function. Then inside you can user await
.
return this.auth.user.pipe(
// switchMap instead of map
// async function call
switchMap(async (user) =>
if (user)
// await here
await user.getIdTokenResult().then((idTokenResult) =>
if (idTokenResult.claims.admin)
if (expectedRoles.indexOf('admin') > -1)
return true;
else
// return back to admin dashboard
this.router.navigate(['/admin']);
else if (idTokenResult.claims.editor)
if (expectedRoles.indexOf('editor') > -1)
return true;
else
// return back to editor dashboard
this.router.navigate(['/editor']);
else if (idTokenResult.claims.user)
if (expectedRoles.indexOf('user') > -1)
return true;
else
// return back to user dashboard
this.router.navigate(['/user']);
else
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
);
else
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1)
return true;
else
this.router.navigate(['/auth/login']);
)
).first();
Just a hint unrelated to this solution - better than load token claim in the AuthGuard function, you should load it after the user logs in and store it in the store the same way you store the information from the login response. This will be much faster.
edited Mar 22 at 16:02
answered Mar 22 at 15:56
kvetiskvetis
1,79411328
1,79411328
Thanks for your detailed answer. I wanted to the thing you mentioned in the bottom but I had a problem that it was getting destroyed when I open the app on new tab for example, while auth keeps there role is destroyed. I kept role variable in auth service and called to get it in constructor of the service. Do I have to do it other way?
– Alp
Mar 22 at 17:43
Yes, you need to load it each time when the app starts - even in new tab. You could use your auth service and store the roles in a behaviour subject so id does not load again. You could use localStorage to save it as well. But that's another discussion.
– kvetis
Mar 25 at 12:29
add a comment |
Thanks for your detailed answer. I wanted to the thing you mentioned in the bottom but I had a problem that it was getting destroyed when I open the app on new tab for example, while auth keeps there role is destroyed. I kept role variable in auth service and called to get it in constructor of the service. Do I have to do it other way?
– Alp
Mar 22 at 17:43
Yes, you need to load it each time when the app starts - even in new tab. You could use your auth service and store the roles in a behaviour subject so id does not load again. You could use localStorage to save it as well. But that's another discussion.
– kvetis
Mar 25 at 12:29
Thanks for your detailed answer. I wanted to the thing you mentioned in the bottom but I had a problem that it was getting destroyed when I open the app on new tab for example, while auth keeps there role is destroyed. I kept role variable in auth service and called to get it in constructor of the service. Do I have to do it other way?
– Alp
Mar 22 at 17:43
Thanks for your detailed answer. I wanted to the thing you mentioned in the bottom but I had a problem that it was getting destroyed when I open the app on new tab for example, while auth keeps there role is destroyed. I kept role variable in auth service and called to get it in constructor of the service. Do I have to do it other way?
– Alp
Mar 22 at 17:43
Yes, you need to load it each time when the app starts - even in new tab. You could use your auth service and store the roles in a behaviour subject so id does not load again. You could use localStorage to save it as well. But that's another discussion.
– kvetis
Mar 25 at 12:29
Yes, you need to load it each time when the app starts - even in new tab. You could use your auth service and store the roles in a behaviour subject so id does not load again. You could use localStorage to save it as well. But that's another discussion.
– kvetis
Mar 25 at 12:29
add a comment |
Try using the rxjs pipes for inner observable calls: flatMap and flatMapTo or mergeMap or mergeMapTo. In your specific case I think mergeMap is the way to go.
Feel free to read about those pipes in rxjs, they might be the solution
add a comment |
Try using the rxjs pipes for inner observable calls: flatMap and flatMapTo or mergeMap or mergeMapTo. In your specific case I think mergeMap is the way to go.
Feel free to read about those pipes in rxjs, they might be the solution
add a comment |
Try using the rxjs pipes for inner observable calls: flatMap and flatMapTo or mergeMap or mergeMapTo. In your specific case I think mergeMap is the way to go.
Feel free to read about those pipes in rxjs, they might be the solution
Try using the rxjs pipes for inner observable calls: flatMap and flatMapTo or mergeMap or mergeMapTo. In your specific case I think mergeMap is the way to go.
Feel free to read about those pipes in rxjs, they might be the solution
answered Mar 22 at 15:58
Francisco SantorelliFrancisco Santorelli
31714
31714
add a comment |
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.
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%2f55303417%2fangular7-authguard-firebase-claims%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