You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using cookie mode, only the session data itself can be enriched through the session hooks. If you want to extend the lifetime of the cookie itself, you could use a middleware that refreshes the cookie by re-setting the session data. (At this point you can also add any other custom data to your cookies)
520
+
521
+
Example middleware:
522
+
523
+
```ts
524
+
// server/middleware/cookie-lifetime-extend.ts
525
+
exportdefaultdefineEventHandler((event) => {
526
+
const session =awaitgetUserSession(event)
527
+
if (session&&Object.keys(session).length>0) {
528
+
awaitsetUserSession(event, session)
529
+
}
530
+
}
531
+
```
532
+
517
533
## Server-Side Rendering
518
534
519
535
You can make authenticated requests both from the client and the server. However, you must use `useRequestFetch()` to make authenticated requests during SSR if you are not using `useFetch()`
@@ -579,42 +595,158 @@ If you are caching your routes with `routeRules`, please make sure to use [Nitro
579
595
580
596
## Configuration
581
597
582
-
We leverage `runtimeConfig.session` to give the defaults option to [h3 `useSession`](https://h3.unjs.io/examples/handle-session).
598
+
### Session Storage
583
599
584
-
You can overwrite the options in your `nuxt.config.ts`:
600
+
Nuxt Auth Utils supports different session storage modes that can be configured in your `nuxt.config.ts`:
- **`cookie`** (default): Stores session data in encrypted cookies. This is the most secure option and works well for most use cases.
614
+
615
+
```ts
616
+
auth: {
617
+
storageType: 'cookie'
618
+
}
619
+
```
620
+
621
+
- **`cache`**: Uses Nitro's cache storage. Useful when you need to store larger session data that might exceed cookie size limits.
622
+
623
+
```ts
624
+
auth: {
625
+
storageType: 'cache'
626
+
}
627
+
```
628
+
629
+
- **`memory`**: Stores sessions in memory. Only suitable for development or testing.
630
+
631
+
```ts
632
+
auth: {
633
+
storageType: 'memory'
634
+
}
635
+
```
636
+
637
+
> [!WARNING]
638
+
> Memory storage is cleared when the server restarts and doesn't work with multiple server instances. Not recommended for production use.
639
+
640
+
- **`nuxt-session`**: Uses a custom storage mount named 'nuxt-session'. Useful when you want to use a different storage driver.
641
+
642
+
```ts
643
+
// nuxt.config.ts
644
+
exportdefaultdefineNuxtConfig({
645
+
auth: {
646
+
storageType: 'nuxt-session'
647
+
},
648
+
nitro: {
649
+
storage: {
650
+
'nuxt-session': {
651
+
driver: 'fsLite',
652
+
base: './.data/sessions'
653
+
}
654
+
}
655
+
}
656
+
})
657
+
```
658
+
659
+
> [!NOTE]
660
+
> This will store sessions in the `.data/sessions` directory. Make sure to add `.data` to your `.gitignore`.
661
+
662
+
#### Session Configuration
663
+
664
+
You can configure session behavior through the `auth` or `runtimeConfig` options:
665
+
666
+
```ts
667
+
exportdefaultdefineNuxtConfig({
668
+
auth: {
669
+
storageType: 'cookie'
670
+
},
589
671
runtimeConfig: {
590
672
session: {
591
-
maxAge: 60*60*24*7// 1 week
673
+
name: 'nuxt-session', // Cookie name
674
+
maxAge: 60*60*24*7, // 1 week
675
+
password: process.env.NUXT_SESSION_PASSWORD,
676
+
cookie: {
677
+
sameSite: 'lax',
678
+
// Additional cookie options
679
+
// secure: true,
680
+
// domain: 'example.com',
681
+
// path: '/'
682
+
}
592
683
}
593
684
}
594
685
})
595
686
```
596
687
597
-
Our defaults are:
688
+
We leverage `runtimeConfig.session` to give the defaults option to [h3 `useSession`](https://h3.unjs.io/examples/handle-session).
689
+
Checkout the [`SessionConfig`](https://github.com/unjs/h3/blob/c04c458810e34eb15c1647e1369e7d7ef19f567d/src/utils/session.ts#L20) for all options.
690
+
691
+
> [!NOTE]
692
+
> When using non-cookie storage types, the cookie only contains a session ID while the actual session data is stored in the selected storage.
693
+
694
+
When using a non-cookie mode
598
695
599
696
```ts
600
-
{
601
-
name: 'nuxt-session',
602
-
password: process.env.NUXT_SESSION_PASSWORD||'',
603
-
cookie: {
604
-
sameSite: 'lax'
697
+
exportdefaultdefineNuxtConfig({
698
+
auth: {
699
+
storageType: 'cache',
700
+
sessionInactivityMaxAge: 60*60*24*30, // Session timeout after inactivity (30 days)
701
+
autoExtendSession: true// Extend session on each request
702
+
},
703
+
runtimeConfig: {
704
+
session: {
705
+
password: process.env.NUXT_SESSION_PASSWORD,
706
+
}
605
707
}
606
-
}
708
+
})
709
+
```
710
+
711
+
> [!IMPORTANT]
712
+
> The `sessionInactivityMaxAge` option is specifically designed for non-cookie storage types to manage and cleanup inactive sessions. When using this configuration, cookies still respect the `maxAge` setting from the session configuration, if one is specified. Whether you need both `maxAge` and `sessionInactivityMaxAge` will depend on your specific application requirements and session management strategy.
713
+
714
+
## Session Cleanup
715
+
716
+
When using non-cookie storage types, you may want to clean up expired sessions periodically. This can be done using Nitro's scheduled tasks feature.
717
+
718
+
1. Create a task file:
719
+
720
+
```ts:server/tasks/clear-sessions.ts
721
+
exportdefaultdefineTask({
722
+
meta: {
723
+
name: 'clear-sessions',
724
+
description: 'Clear expired sessions',
725
+
},
726
+
run({ payload, context }) {
727
+
console.log('Running clear-sessions task...')
728
+
cleanupOrphanedUserSessions()
729
+
return { result: 'Success' }
730
+
},
731
+
})
607
732
```
608
733
609
-
You can also overwrite the session config by passing it as 3rd argument of the `setUserSession` and `replaceUserSession` functions:
734
+
2. Configure the task schedule in your `nuxt.config.ts`:
610
735
611
736
```ts
612
-
awaitsetUserSession(event, { ... } , {
613
-
maxAge: 60*60*24*7// 1 week
737
+
exportdefaultdefineNuxtConfig({
738
+
nitro: {
739
+
experimental: {
740
+
tasks: true
741
+
},
742
+
scheduledTasks: {
743
+
'*/5 * * * *': ['clear-sessions'] // Run every 5 minutes
744
+
}
745
+
}
614
746
})
615
747
```
616
748
617
-
Checkout the [`SessionConfig`](https://github.com/unjs/h3/blob/c04c458810e34eb15c1647e1369e7d7ef19f567d/src/utils/session.ts#L20) for all options.
749
+
This will automatically clean up any expired sessions based on your `sessionInactivityMaxAge` configuration.
0 commit comments