2018-02-02 00:06:01 +01:00
|
|
|
import * as mongo from 'mongodb';
|
2019-03-19 10:47:14 +01:00
|
|
|
import * as deepcopy from 'deepcopy';
|
2018-03-29 13:32:18 +02:00
|
|
|
import db from '../db/mongodb';
|
2018-10-16 04:38:09 +02:00
|
|
|
import isObjectId from '../misc/is-objectid';
|
2018-02-02 00:06:01 +01:00
|
|
|
import { pack as packApp } from './app';
|
2017-01-17 01:12:33 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
const AuthSession = db.get<IAuthSession>('authSessions');
|
2018-02-02 00:06:01 +01:00
|
|
|
export default AuthSession;
|
|
|
|
|
|
|
|
export interface IAuthSession {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: Date;
|
|
|
|
appId: mongo.ObjectID;
|
|
|
|
userId: mongo.ObjectID;
|
|
|
|
token: string;
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack an auth session for API response
|
|
|
|
*
|
|
|
|
* @param {any} session
|
|
|
|
* @param {any} me?
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
session: any,
|
|
|
|
me?: any
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
let _session: any;
|
|
|
|
|
|
|
|
// TODO: Populate session if it ID
|
|
|
|
_session = deepcopy(session);
|
|
|
|
|
|
|
|
// Me
|
2018-10-16 04:38:09 +02:00
|
|
|
if (me && !isObjectId(me)) {
|
2018-02-02 00:06:01 +01:00
|
|
|
if (typeof me === 'string') {
|
|
|
|
me = new mongo.ObjectID(me);
|
|
|
|
} else {
|
|
|
|
me = me._id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete _session._id;
|
|
|
|
|
|
|
|
// Populate app
|
2018-03-29 07:48:47 +02:00
|
|
|
_session.app = await packApp(_session.appId, me);
|
2018-02-02 00:06:01 +01:00
|
|
|
|
|
|
|
resolve(_session);
|
|
|
|
});
|