Skip to content

Commit

Permalink
feat(frameworks): Add support for Koa
Browse files Browse the repository at this point in the history
Co-authored-by: Valentina K <[email protected]>
  • Loading branch information
vaguue and Valentina K authored Oct 21, 2022
1 parent c63b9d2 commit a4c8ba5
Show file tree
Hide file tree
Showing 12 changed files with 2,674 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ _Table of contents:_
- [Session wrappers](#session-wrappers)
- [Typing session data with TypeScript](#typing-session-data-with-typescript)
- [Express](#express)
- [Koa](#koa)
- [Handle password rotation/update the password](#handle-password-rotationupdate-the-password)
- [Magic links](#magic-links)
- [Impersonation, login as someone else](#impersonation-login-as-someone-else)
Expand Down Expand Up @@ -405,6 +406,10 @@ We've taken this technique from [express-session types](https://github.com/Defin

See [examples/express](examples/express) for an example of how to use this with Express.

### Koa

See [examples/koa](examples/koa) for an example of how to use this with Koa.

### Handle password rotation/update the password

When you want to:
Expand Down
5 changes: 5 additions & 0 deletions examples/koa/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
parserOptions: {
sourceType: 'module',
}
};
60 changes: 60 additions & 0 deletions examples/koa/README.md
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));
```
42 changes: 42 additions & 0 deletions examples/koa/app.js
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}`);
});
Loading

1 comment on commit a4c8ba5

@vercel
Copy link

@vercel vercel bot commented on a4c8ba5 Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.