CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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