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';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { ILocalUser } from '../../../models/entities/user';
|
2019-07-17 22:22:44 +03:00
|
|
|
import { Signins } from '../../../models';
|
|
|
|
import { genId } from '../../../misc/gen-id';
|
|
|
|
import { publishMainStream } from '../../../services/stream';
|
2017-11-23 06:25:33 +02:00
|
|
|
|
2019-11-24 10:09:32 +02:00
|
|
|
export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) {
|
2017-11-23 06:25:33 +02:00
|
|
|
if (redirect) {
|
2018-11-27 22:27:34 +02:00
|
|
|
//#region Cookie
|
2020-03-20 06:56:22 +02:00
|
|
|
ctx.cookies.set('igi', user.token, {
|
2018-11-27 22:27:34 +02:00
|
|
|
path: '/',
|
|
|
|
// 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'),
|
2020-03-20 06:56:22 +02:00
|
|
|
httpOnly: false
|
2018-11-27 22:27:34 +02:00
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
|
2018-04-13 00:06:18 +03:00
|
|
|
ctx.redirect(config.url);
|
2018-04-13 05:44:39 +03:00
|
|
|
} else {
|
2020-01-29 21:37:25 +02:00
|
|
|
ctx.body = {
|
|
|
|
id: user.id,
|
|
|
|
i: user.token
|
|
|
|
};
|
2018-11-28 09:19:02 +02:00
|
|
|
ctx.status = 200;
|
2017-11-23 06:25:33 +02:00
|
|
|
}
|
2019-07-17 22:22:44 +03:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
// Append signin history
|
|
|
|
const record = await Signins.save({
|
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: user.id,
|
|
|
|
ip: ctx.ip,
|
|
|
|
headers: ctx.headers,
|
|
|
|
success: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Publish signin event
|
|
|
|
publishMainStream(user.id, 'signin', await Signins.pack(record));
|
|
|
|
})();
|
2017-11-23 06:25:33 +02:00
|
|
|
}
|