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

27 lines
731 B
TypeScript
Raw Normal View History

2018-04-12 23:06:18 +02:00
import * as Koa from 'koa';
2018-04-02 06:15:53 +02:00
import config from '../../../config';
2018-04-12 23:06:18 +02:00
import { ILocalUser } from '../../../models/user';
2017-11-23 05:25:33 +01:00
2018-04-13 04:44:39 +02:00
export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) {
2017-11-23 05:25:33 +01:00
if (redirect) {
2018-11-27 21:27:34 +01:00
//#region Cookie
const expires = 1000 * 60 * 60 * 24 * 365; // One Year
ctx.cookies.set('i', user.token, {
path: '/',
domain: config.hostname,
// SEE: https://github.com/koajs/koa/issues/974
// When using a SSL proxy it should be configured to add the "X-Forwarded-Proto: https" header
secure: config.url.startsWith('https'),
httpOnly: false,
expires: new Date(Date.now() + expires),
maxAge: expires
});
//#endregion
2018-04-12 23:06:18 +02:00
ctx.redirect(config.url);
2018-04-13 04:44:39 +02:00
} else {
ctx.status = 204;
2017-11-23 05:25:33 +01:00
}
}