docs: add qwik-auth route protection example
What is it?
- [ ] Feature / enhancement
- [ ] Bug
- [x] Docs / tests / types / typos
Description
Add an example of how to provide route protection
Checklist:
- [x] My code follows the developer guidelines of this project
- [x] I have performed a self-review of my own code
- [ ] I have made corresponding changes to the documentation
- [ ] Added new tests to cover the fix / functionality
Run & review this pull request in StackBlitz Codeflow.
Seems like a good idea. I'm out of town for a while.
On Wed, May 17, 2023, 12:30 roman zanettin @.***> wrote:
@.**** commented on this pull request.
In packages/docs/src/routes/docs/integrations/authjs/index.mdx https://github.com/BuilderIO/qwik/pull/4119#discussion_r1196970887:
@@ -230,3 +230,18 @@ AUTH_SECRET=
- The application is now ready to impliment authentication using Auth.js.
- Enjoy!
+### Route Protection + +Session data can be accessed via the route
event.sharedMap. So a route can be protected and redirect using something like this located in alayout.tsxor pageindex.tsx: + +```tsx +export const onRequest: RequestHandler = (event) => {
- const session: Session | null = event.sharedMap.get('session');
- if (!session || new Date(session.expires) < new Date()) {
- throw event.redirect(302,
/api/auth/signin?callbackUrl=${event.url.href});- } +}; +```
+> Note: If placed in a layout.tsx ensure that the redirect destination does not share the same layout.tsx or a redirection loop could occur.
thank you @ulic75 https://github.com/ulic75 🙏 should we check the current location as well on the expression to avoid a potential loop? like when you're already on the sign in page, do not redirect?
— Reply to this email directly, view it on GitHub https://github.com/BuilderIO/qwik/pull/4119#pullrequestreview-1431418189, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLBN3BC7TPWXLADIA3E2QTXGURNZANCNFSM6AAAAAAX2XM5IU . You are receiving this because you were mentioned.Message ID: @.***>
This was a great help to me...took me a while to find this since it's still an open PR. But it works as expected for my route protections. I'm using a narrow layout-app.tsx and the [email protected] convention and it appears to work correctly.
Hi @ulic75 wave imo we could merge this. objections?
Yeah, let's do it.