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
{{ message }}
This repository was archived by the owner on Dec 12, 2023. It is now read-only.
feat: rename to useSession, allow partial options via defu (#5)
* feat!: rename useNuxtSession to just useSession (thx, @atinux)
* feat!: make some api options optional, actually rename composable file
* chore: update changelog to point to releases page
> Nuxt session middleware to get a persistent session per app user, e.g., to store data across multiple requests. The nuxt session module provides the `useNuxtSession()` composable out of the box and sets up API endpoints to interact with your session to make working with sessions feel like a breeze.
12
+
> Nuxt session middleware to get a persistent session per app user, e.g., to store data across multiple requests. The nuxt session module provides the `useSession()` composable out of the box and sets up API endpoints to interact with your session to make working with sessions feel like a breeze.
13
13
14
14
## Quick start
15
15
16
16
1. Install the package:
17
17
```bash
18
-
npm i @sidebase/nuxt-session
18
+
npm i -D @sidebase/nuxt-session
19
19
```
20
20
2. Add the package to your `nuxt.config.ts`:
21
21
```bash
@@ -26,7 +26,7 @@
26
26
3. Done! Each client will now have a unique session you can access on the server- and client side:
// Reactive session object that updates after methods calls below
32
32
session.value
@@ -51,7 +51,7 @@ The `nuxt-session` library provide many helpers to interact with the session fro
51
51
## Features
52
52
53
53
- ✔️ Persistent sessions across requests using cookies
54
-
- ✔️ `useNuxtSession` composable for client side session-interaction
54
+
- ✔️ `useSession` composable for client side session-interaction
55
55
- ✔️ Configurable session endpoints out of the box:
56
56
- `GET /api/session`: Get the current session
57
57
- `DELETE /api/session`: Delete the current session
@@ -120,7 +120,7 @@ const {
120
120
reset,
121
121
update,
122
122
overwrite
123
-
} = await useNuxtSession()
123
+
} = await useSession()
124
124
125
125
// The session itself, a ref that automatically updates when you use the other methods below
126
126
session.value
@@ -145,7 +145,7 @@ Per default all of the above is enabled. Read on if you want to learn how to con
145
145
146
146
You can configure what endpoints and utilities `nuxt-session` adds for client-side use using the module configuration. The API is fully enabled per default. If you want to turn off the whole `nuxt-session` API you can set `session: { api: { isEnabled: false } }` in the module config in your `nuxt.config.ts`. If you want to keep the api enabled but allow just certain operation by the client-side, you can restrict the HTTP methods that should be allowed. E.g., `session: { api: { methods: ['get'] } }` would:
147
147
- add only one endpoint that allows reading the current session data (per default: `GET /api/session`)
148
-
- enable only the `session` and `refresh` properties of the `useNuxtSession` composable
148
+
- enable only the `session` and `refresh` properties of the `useSession` composable
149
149
150
150
After this, calling the `reset()` or `update()` functions from above would result in an error that the methods are not supported and the api endpoints would not be added to your nuxt-app. This way:
151
151
- you cannot accidentaly call a composable-method during development and it later does not work in production,
<p>With nuxt-session you can either access the current user session safely on the server side using `event.context.session`. You can also use the `update`-method of the `useNuxtSession` composable that allows arbitrary updating of session data from the client-side.</p>
36
+
<p>With nuxt-session you can either access the current user session safely on the server side using `event.context.session`. You can also use the `update`-method of the `useSession` composable that allows arbitrary updating of session data from the client-side.</p>
37
37
<p>Below both possible options are show-cased. One button triggers a request to the `/api/count` endpoint that increases the request count on the server side. The second button sends a JSON-payload to the nuxt-session API endpoint that allows arbitrary updating of session data with data sent from the client side.</p>
38
38
<p>When you increase the count on the server-side, the session here will not change. You need to hit the `refresh` button above to see the changes! When we update it from the client-side using the nuxt-session composable `update` for it, we immeadiatly see the updates reflected, neat!</p>
39
39
<p>NOTE: The server-side and client-side count update can collide with each other, e.g., if you update the count on the server side a couple of times and then update the count from the client side without refreshing the session. As the client only has the "old" count, it will send a different count than the count that the server currently knows and thus overwrite it.</p>
0 commit comments