Skip to content

Commit

Permalink
docs: add qwik-auth route protection example (QwikDev#4119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulic75 authored Jun 3, 2023
1 parent d08999f commit 877edc4
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/docs/src/routes/docs/integrations/authjs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,18 @@ AUTH_SECRET=
4. The application is now ready to impliment authentication using Auth.js.
5. 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 a `layout.tsx` or page `index.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.

0 comments on commit 877edc4

Please sign in to comment.