Modify the order of beforeEach guards #2292
-
Hi! I would like to know if there is the possibility to modify the order of which the beforeEach guards are called. I know that they are called following their creation time, but I've the following problem:
The problem is that with this configuration the workflow is not producing the desired results. Under some circumstances the final result is not the expected one, and the reason is the order of the beforeEach guards. Indeed, moving our custom beforeEach guards after the one created in the authentication plugin produces the correct effect. Hence, is there a way to modify the order of this guards? If not, is there a way to define the priority of them? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
What about declaring your authentication guard first but ignoring it until the user is authenticated? Something like that: router.beforeEach((to, from) => {
if (isAuthenticated) {
// Do something
}
return true
}) Here |
Beta Was this translation helpful? Give feedback.
-
Thank you for your help.
The things not exactly so simple, that's why this solution would not work.
In detail the authentication guard works like this:
1. User tries to access a protected route
2. The router sees that the user if not authenticated (trying to obtain his
information and getting a 401 error) and automatically sends him to the
login screen (we still have to use a CAS authentication system)
3. After the authentication the CAS and then the application backend return
to the frontend to a special page (/home)
4. The router checks again the user, finds its information and allows the
route (returning true)
Then, normally, any custom guard that kicks in for a protected route (we
sign them with special meta attributes - better, the /home carries this
meta information, while the other routes are its children and get it
automatically) would normally work like this:
1.if that route comes, it means that the authentication was successful
(because the authentication guard allowed it)
2. According to the content of the user information a different route, or
just true, is returned
3. Any other situation (user information empty) should return false because
the route is protected
In our situation, given that the custom guard comes before the
authentication one, we have to do the things in a different way.
We still control the meta of the route and check for the user information.
But, instead of returning false if its empty, we must return true to allow
the next guard (the authentication one) to kick in and complete the
authentication process. Thus because the first and foremost route that this
guard sees is the /home one, that's exactly the one that is returned upon a
successful authentication.
I know that I could configure a per route guard. I've also tried and it
works. But I've to duplicate some logic (of perform some extra work) for
every sub route of the /home. And the whole 'routing rules' are scattered
in every sub route. With a more generic code this would be simpler to
understand and much concise.
Unfortunately I've not found a better solution than forcing to add any
custom guard after the authentication one.
…On Thu, 4 Jul 2024, 16:08 Arnaud Thomas D., ***@***.***> wrote:
What about declaring your authentication guard first but ignoring it until
the user is authenticated?
Something like that:
router.beforeEach((to, from) => {
if (isAuthenticated) {
// Do something
}
return true})
isAuthenticated could be something coming from your store or something
like that.
—
Reply to this email directly, view it on GitHub
<#2292 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEXPQCE2WU3CMIXVQ54ATY3ZKVJNDAVCNFSM6AAAAABKLIXDH2VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TSNJZGU4TC>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
There is currently no way to define guards priority other than changing the order in which you declare your guards.
If you can't find a way to reorganize your code, maybe you can try to use
beforeEach
return value which is a method to unregister the guard. That way you can kind of control the registration order of your guards (by removing and adding them again).But I truly suggest to see how you can adapt your code to declare guards in the correct order so you don't have to do that.