Skip to content

Commit

Permalink
small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 6, 2023
1 parent 1076dbc commit 3eba51a
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@ icon: ri:arrow-left-right-line

# Sessions

Nitro, through [unjs/h3](https://github.com/h3), supports sessions out of the box.
Nitro, supports user sessions out of the box. (through [unjs/h3](https://github.com/h3))

::alert{type="info"}
A session is a series of interactions between a client and server. Because HTTP is a stateless protocol, sessions allow association of multiple requests to the same client using a cookie which is automatically sent by the browser to the server on each request.
::

## Composables available
## Session Utilities

There is many composables you can use to manage sessions:
You can use to manage sessions with available auto imported utils:

- `useSession(event, sessionConfig)`: Initializes a session and returns a wrapper to control it.
- `getSession(event, sessionConfig)`: Initializes or gets the current user session.
- `updateSession(event, sessionConfig)`: Updates data of the current session.
- `clearSession(event, sessionConfig)`: Clears the current session.

Advanced utils:

- `sealSession(event, sessionConfig)`: Encrypts the current user session.
- `unsealSession(event, sessionConfig, sealed)`: Decryptes the current user session.

- `useSession`, the most importante one since it's a wrapper around all other composables
- `getSession`, to get the current session
- `updateSession`, to update the data of the current session
- `sealSession`, to protect the current session from being modified
- `unsealSession`, to access to the current session
- `clearSession`, to clear the current session

## Use sessions

To use sessions, you've just to use the `useSession` composable and provide a string password in one of your event handler. each time a client will hit this endpoint, the composable will first try to get the session from a header named `x-h3-session` then from a cookie named `h3`. If no session is found, a new one will be created.
To use sessions, you've just to use the `useSession` composable and provide a strong password in one of your event handler. each time a client will hit this endpoint, the composable will first try to get the session from a header named `x-h3-session` then from a cookie named `h3`. If no session is found, a new one will be created.

```ts [routes/index.ts]
export default defineEventHandler(async (event) => {
Expand Down

0 comments on commit 3eba51a

Please sign in to comment.