From cf33e483f7e6f40e8cbbbc0118a7df70bdaf651f Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 29 Mar 2018 20:32:18 +0900 Subject: 整理した MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/auth-session.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/models/auth-session.ts (limited to 'src/models/auth-session.ts') diff --git a/src/models/auth-session.ts b/src/models/auth-session.ts new file mode 100644 index 0000000000..6fe3468a7b --- /dev/null +++ b/src/models/auth-session.ts @@ -0,0 +1,48 @@ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import db from '../db/mongodb'; +import { pack as packApp } from './app'; + +const AuthSession = db.get('authSessions'); +export default AuthSession; + +export interface IAuthSession { + _id: mongo.ObjectID; + createdAt: Date; + appId: mongo.ObjectID; + userId: mongo.ObjectID; + token: string; +} + +/** + * Pack an auth session for API response + * + * @param {any} session + * @param {any} me? + * @return {Promise} + */ +export const pack = ( + session: any, + me?: any +) => new Promise(async (resolve, reject) => { + let _session: any; + + // TODO: Populate session if it ID + _session = deepcopy(session); + + // Me + if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) { + if (typeof me === 'string') { + me = new mongo.ObjectID(me); + } else { + me = me._id; + } + } + + delete _session._id; + + // Populate app + _session.app = await packApp(_session.appId, me); + + resolve(_session); +}); -- cgit v1.2.3-freya