Best practice around authentication checks in nested routes #12510
-
I am working on an application that requires authentication. I am using the cookie session storage to store the auth cookie with secrets. In my authenticated pages and layouts, I am using a My main concerns are:
In general, what is the best practice around authentication checks? Do I do it in my layouts only, will that suffice for all authenticated nested routes or do I need to do it in all layouts and nested authenticated routes? Of course, for my actions, I am also making the check once more to make sure it's an authenticated request. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hey, |
Beta Was this translation helpful? Give feedback.
-
You need to check for auth in every loader and actions that needs to be protected (it's not public), every loader/action can be fetched individually so not adding your auth check will leave them unprotected. Imagine every loader as an API route, you have
This can work, but it will depend on how strict your auth needs to be, if you're ok with only checking for the cookie to exists then it's ok to do that, but some apps also check if the user has the correct role and if it's still active, imagine the user is banned, you want to ensure it can't keep using the app until it's logged out. |
Beta Was this translation helpful? Give feedback.
You need to check for auth in every loader and actions that needs to be protected (it's not public), every loader/action can be fetched individually so not adding your auth check wil…