Path: blob/master/src/packages/database/postgres/remember-me.ts
1503 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Getting, setting, etc. of remember_me cookies should go here.78OF course, not everything is rewritten yet...9*/1011import { PostgreSQL } from "./types";12const { one_result } = require("../postgres-base");13import { callback } from "awaiting";14import { reuseInFlight } from "@cocalc/util/reuse-in-flight";1516async function _get_remember_me(17db: PostgreSQL,18hash: string,19cache: boolean20): Promise<string | undefined> {21// returns undefined for now account or the account_id of user we just authenticated2223function f(cb: Function): void {24db._query({25cache,26query: "SELECT account_id, expire FROM remember_me",27where: {28// db-schema/auth defines the hash field as a "bpchar", hence do not cast to TEXT – otherwise this is a 100x slowdown29"hash = $::CHAR(127)": hash.slice(0, 127),30},31retry_until_success: { max_time: 60000, start_delay: 10000 }, // since we want this to be (more) robust to database connection failures.32cb: one_result("account_id", cb),33});34}3536return await callback(f);37}3839export const get_remember_me = reuseInFlight(_get_remember_me, {40createKey: function (args) {41return args[1] + args[2];42},43});444546