-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frameworks): Add support for Koa
Co-authored-by: Valentina K <[email protected]>
- Loading branch information
Showing
12 changed files
with
2,674 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
parserOptions: { | ||
sourceType: 'module', | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Koa example application using iron-session | ||
|
||
This is a small example application providing basic API | ||
|
||
The tl;dr; on how to use `iron-session` with Koa is this: | ||
|
||
```js | ||
import Koa from 'koa'; | ||
import Router from '@koa/router'; | ||
import { ironSession } from 'iron-session/koa'; | ||
|
||
const app = new Koa(); | ||
const dev = (process.env.NODE_ENV || app.env) != 'production'; | ||
|
||
app.use(ironSession({ | ||
cookieName: 'iron-session/examples/koa', | ||
password: process.env.SECRET_COOKIE_PASSWORD, | ||
cookieOptions: { | ||
secure: !dev, | ||
}, | ||
})); | ||
|
||
app.use(async (ctx, next) => { //Error handling | ||
try { | ||
await next(); | ||
} catch(e) { | ||
ctx.status = e.statusCode || e.status || 500; | ||
ctx.body = { message: e.message }; | ||
console.log('[!] error', ctx.body); | ||
if (dev) { | ||
console.log(e); | ||
} | ||
} | ||
}); | ||
|
||
const router = new Router(); | ||
router | ||
.get('/', async ctx => { | ||
ctx.body = { hello: 'world' }; | ||
}) | ||
.get('/login', async ctx => { | ||
ctx.session.user = { id: 20 }; | ||
await ctx.session.save(); | ||
ctx.body = { message: 'ok' }; | ||
}) | ||
.get('/profile', async ctx => { | ||
if (ctx.session.user) { | ||
ctx.body = { user: ctx.session.user }; | ||
} | ||
else { | ||
ctx.throw(500, { message: 'restricted' }); | ||
} | ||
}) | ||
.post('/logout', async ctx => { | ||
ctx.session.destroy(); | ||
}); | ||
|
||
app.use(router.routes()); | ||
app.listen(3000, () => console.log('[*] listening on port', 3000)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Koa from 'koa'; | ||
import cors from '@koa/cors'; | ||
import body from 'koa-body'; | ||
import logger from 'koa-logger'; | ||
import { ironSession } from 'iron-session/koa'; | ||
|
||
import indexRouter from './routes/indexRouter'; | ||
|
||
const app = new Koa(); | ||
|
||
const dev = (process.env.NODE_ENV || app.env) != 'production'; | ||
|
||
app.use(async (ctx, next) => { //Error handling | ||
try { | ||
await next(); | ||
} catch(e) { | ||
ctx.status = e.statusCode || e.status || 500; | ||
ctx.body = { message: e.message }; | ||
console.log('[!] error', ctx.body); | ||
if (dev) { | ||
console.log(e); | ||
} | ||
} | ||
}); | ||
|
||
app.use(logger()); | ||
app.use(cors()); | ||
app.use(body()); | ||
app.use(ironSession({ | ||
cookieName: 'iron-session/examples/koa', | ||
password: process.env.SECRET_COOKIE_PASSWORD, | ||
cookieOptions: { | ||
secure: !dev, | ||
}, | ||
})); | ||
|
||
app.use(indexRouter.routes()); | ||
|
||
const port = process.env.PORT || 3000; | ||
app.listen(port, () => { | ||
console.log(`[*] listening on ${port}`); | ||
}); |
Oops, something went wrong.
a4c8ba5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
iron-session-example – ./
iron-session-example-vvoyer.vercel.app
iron-session-example-git-main-vvoyer.vercel.app
next-iron-session.vercel.app
iron-session-example.vercel.app