Sharkey/src/server/api/common/signin.ts

25 lines
608 B
TypeScript
Raw Normal View History

2018-04-13 00:06:18 +03:00
import * as Koa from 'koa';
2018-04-02 07:15:53 +03:00
import config from '../../../config';
2018-04-13 00:06:18 +03:00
import { ILocalUser } from '../../../models/user';
2017-11-23 06:25:33 +02:00
2018-04-13 05:44:39 +03:00
export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) {
2017-11-23 06:25:33 +02:00
const expires = 1000 * 60 * 60 * 24 * 365; // One Year
2018-04-13 00:06:18 +03:00
ctx.cookies.set('i', user.token, {
2017-11-23 06:25:33 +02:00
path: '/',
2018-04-13 00:06:18 +03:00
domain: config.hostname,
2018-10-16 23:54:31 +03:00
// SEE: https://github.com/koajs/koa/issues/974
//secure: config.url.startsWith('https'),
secure: false,
2017-11-23 06:25:33 +02:00
httpOnly: false,
expires: new Date(Date.now() + expires),
maxAge: expires
});
if (redirect) {
2018-04-13 00:06:18 +03:00
ctx.redirect(config.url);
2018-04-13 05:44:39 +03:00
} else {
ctx.status = 204;
2017-11-23 06:25:33 +02:00
}
}