Skip to content

Commit

Permalink
Merge pull request #21 from pixelmund/express-integration
Browse files Browse the repository at this point in the history
Express/Connect Integration
  • Loading branch information
pixelmund authored Dec 2, 2021
2 parents 7c2b3cd + 65a482f commit b8a9633
Show file tree
Hide file tree
Showing 11 changed files with 415 additions and 76 deletions.
66 changes: 56 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ npm i svelte-kit-cookie-session
yarn add svelte-kit-cookie-session
```



> :warning: **Because of some vite issues [#14](https://github.com/pixelmund/svelte-kit-cookie-session/issues/14) [#15](https://github.com/pixelmund/svelte-kit-cookie-session/issues/15)**: you should add the following to your `svelte.config`!

```js
const config = {
kit: {
vite: {
optimizeDeps: {
exclude: ['svelte-kit-cookie-session'],
},
kit: {
vite: {
optimizeDeps: {
exclude: ["svelte-kit-cookie-session"],
},
},
},
};
```

Expand Down Expand Up @@ -94,8 +91,7 @@ Then you can use multiple secrets:

```js
export const handle = handleSession({
secret:
"SOME_COMPLEX_SECRET_AT_LEAST_32_CHARS",
secret: "SOME_COMPLEX_SECRET_AT_LEAST_32_CHARS",
});
```

Expand Down Expand Up @@ -126,6 +122,8 @@ Notes:

`If the session already exists, the data get's updated but the expiration time stays the same`

`The only way to set the session is setting the locals.session.data to an object`

> src/routes/login.ts
```js
Expand All @@ -139,6 +137,28 @@ export async function post({ locals, body }) {
}
```

### Accessing The Session

`After initializing the session, your locals will be filled with a session JS Proxy, this Proxy automatically sets the cookie if you set the locals.session.data to something and receive the current data via locals.session.data only. To see this in action add a console.log(locals.session) it will be empty. Only if you add an console.log(locals.session.data) and access the data it will output the current data. So if you wonder why is my session not filled, this is why`

> src/routes/api/me.ts
```js
/** @type {import('@sveltejs/kit').RequestHandler} */
export async function get({ locals, body }) {
// console.log(locals.session) will be empty

// Access your data via locals.session.data -> this should always be an object.
const currentUser = locals.session.data?.user;

return {
body: {
me: currentUser,
},
};
}
```
### Destroying the Session
> src/routes/logout.ts
Expand Down Expand Up @@ -180,3 +200,29 @@ handleSession({
rolling: true,
});
```
### Express/Connect Integration
This library can integrate with express, polka or any other connect compatible middleware layer.
```ts
import express from "express";
import { sessionMiddleware } from "svelte-kit-cookie-session";

const app = express();

app.use(
sessionMiddleware({ secret: "A_VERY_SECRET_SECRET_AT_LEAST_32_CHARS_LONG" })
);

app.get("/", (req, res) => {
const sessionData = req.session.data;
const views = sessionData.views ?? 0;
req.session.data = { views: views + 1 };
return res.json({ views: req.session.data.views });
});

app.listen(4004, () => {
console.log("Listening on http://localhost:4004");
});
```
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
},
"type": "module",
"dependencies": {
"svelte-kit-cookie-session": "^1.3.2"
"svelte-kit-cookie-session": "^1.4.0"
}
}
8 changes: 4 additions & 4 deletions example/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion loadr.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const loaders = ['ts-node/esm'];
export const loaders = ['tsm'];
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
{
"name": "svelte-kit-cookie-session",
"version": "1.3.3",
"version": "1.4.0",
"description": "⚒️ Encrypted 'stateless' cookie sessions for SvelteKit",
"repository": {
"type": "git",
"url": "https://github.com/pixelmund/svelte-kit-cookie-session.git"
},
"type": "module",
"module": "dist/index.js",
"types": "./dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js"
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./package.json": "./package.json"
},
"scripts": {
"build": "rimraf ./dist && tsc",
"build": "rimraf ./dist && tsc -p tsconfig-cjs.json && tsc -p tsconfig.json && node ./postbuild.mjs",
"dev": "tsc -w",
"test": "loadr -- uvu tests",
"prepublishOnly": "npm run build && npm run test"
Expand All @@ -38,7 +40,7 @@
"loadr": "^0.1.1",
"rimraf": "^3.0.2",
"svelte": "^3.35.0",
"ts-node": "^9.1.1",
"tsm": "^2.2.1",
"typescript": "^4.2.3",
"uvu": "^0.6.0-next.1"
}
Expand Down
Loading

0 comments on commit b8a9633

Please sign in to comment.