Skip to content

feat: add SvelteKit support #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

- **[Providers](#grant)**
- **Handlers**
- [Express](#handlers) / [Koa](#handlers) / [Hapi](#handlers) / [Fastify](#handlers)
- [Express](#handlers) / [Koa](#handlers) / [Hapi](#handlers) / [Fastify](#handlers) / [SvelteKit](#handlers)
- [AWS Lambda](#handlers) / [Azure Function](#handlers) / [Google Cloud Function](#handlers) / [Vercel](#handlers)
- **Configuration**
- [Basics](#configuration-basics) / [Description](#configuration-description) / [Values](#configuration-values) / [Scopes](#configuration-scopes)
Expand Down Expand Up @@ -102,6 +102,17 @@ fastify()
```
</details>

<details><summary>SvelteKit</summary>

```js
import grant from 'grant';

export const handle = grant.sveltekit({
config: {/*configuration - see below*/}, session: {secret: 'grant'}
});
```
</details>

### Serverless Functions

<details><summary>AWS Lambda</summary>
Expand Down
8 changes: 8 additions & 0 deletions grant.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ function grant ({handler, ...rest}) {
else if (handler === 'vercel') {
return require('./lib/handler/vercel')(rest)
}
else if (handler === 'sveltekit') {
return require('./lib/handler/sveltekit')(rest)
}
}

grant.express = (options) => {
Expand Down Expand Up @@ -135,5 +138,10 @@ grant.vercel = (options) => {
return options ? handler(options) : handler
}

grant.sveltekit = (options) => {
var handler = require('./lib/handler/sveltekit')
return options ? handler(options) : handler
}

grant.default = grant
module.exports = grant
51 changes: 51 additions & 0 deletions lib/handler/sveltekit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var qs = require('qs')
var Grant = require('../grant')
var Session = require('../session')

module.exports = function (args = {}) {
var grant = Grant(args.config ? args : {config: args})
app.config = grant.config

var regex = new RegExp([
'^',
app.config.defaults.prefix,
/(?:\/([^\/\?]+?))/.source, // /:provider
/(?:\/([^\/\?]+?))?/.source, // /:override?
/(?:\/$|\/?\?+.*)?$/.source, // querystring
].join(''), 'i')

var store = Session(args.session)

async function app ({ event, resolve }) {
var session = store(event.request)
var match = regex.exec(event.url.pathname + event.url.search)
if (!match) {
return resolve(event)
}

var {location, session:sess, state} = await grant({
method: event.request.method,
params: {provider: match[1], override: match[2]},
query: qs.parse(event.url.searchParams.toString()),
body: qs.parse(await event.request.text()),
state,
session: (await session.get()).grant,
})

await session.set({grant: sess})

if (location) {
return new Response(null, {
status: 307,
headers: {
location,
'set-cookie': session.headers['set-cookie']
}
})
} else {
return resolve(event)
}
}

return app
}
8 changes: 4 additions & 4 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ module.exports = ({name, secret, cookie:options, store}) => {
var embed = !store

return (req) => {
var headers = Object.keys(req.headers)
var headerObj = req.headers instanceof Headers;
var headers = (headerObj ? Array.from(req.headers.keys()) : Object.keys(req.headers))
.filter((key) => /(?:set-)?cookie/i.test(key))
.reduce((all, key) => (all[key.toLowerCase()] = req.headers[key], all), {})

.reduce((all, key) => (all[key.toLowerCase()] = headerObj ? req.headers.get(key) : req.headers[key], all), {})
headers['set-cookie'] =
headers['set-cookie'] ||
(headerObj ? headers.get('set-cookie') : headers['set-cookie']) ||
(req.multiValueHeaders && req.multiValueHeaders['Set-Cookie']) ||
[]

Expand Down