UNPKG

@cocalc/server

Version:

CoCalc server functionality: functions used by either the hub and the next.js server

52 lines 2.01 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const pool_1 = __importDefault(require("@cocalc/database/pool")); const api_1 = require("@cocalc/server/auth/api"); const remember_me_1 = require("@cocalc/server/auth/remember-me"); // Return account_id if they are signed in. // If not, returns undefined. // This is determined by looking in their cookie and checking // who it identifies in the database. async function getAccountId(req, noCache = false) { if (req == null) return; // caching a bit -- We thus want the query below to happen rarely. We also // get expire field as well (since it is usually there) so that the result isn't empty // (hence not cached) when a cookie has expired. const hash = (0, remember_me_1.getRememberMeHash)(req); if (!hash) { // not signed in via a cookie. // What about an api key? if (req.header("Authorization")) { try { return await (0, api_1.getAccountIdFromApiKey)(req); } catch (_err) { // non-fatal, at least for now... return; } } return; } const pool = (0, pool_1.default)(noCache ? "short" : undefined); // important to use CHAR(127) instead of TEXT for 100x performance gain. const result = await pool.query("SELECT account_id, expire FROM remember_me WHERE hash = $1::CHAR(127)", [hash]); if (result.rows.length == 0) { return; } const { account_id, expire } = result.rows[0]; if (expire <= new Date()) { // expired return; } return account_id; } exports.default = getAccountId; //# sourceMappingURL=get-account.js.map