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/hub/servers/app/set-cookies.ts
Views: 687
1
import Cookies from "cookies";
2
import { Router } from "express";
3
import { getLogger } from "@cocalc/hub/logger";
4
const { COOKIE_OPTIONS } = require("@cocalc/hub/client"); // import { COOKIE_OPTIONS } from "@cocalc/hub/client";
5
6
export default function init(router: Router) {
7
const winston = getLogger("set-cookie");
8
9
router.get("/cookies", (req, res) => {
10
if (req.query.set) {
11
// TODO: implement setting maxAge as part of query? not needed for now.
12
const maxAge = 1000 * 24 * 3600 * 30 * 6; // 6 months -- long is fine now since we support "sign out everywhere" ?
13
14
winston.debug(`${req.query.set}=${req.query.value}`);
15
// The option { secure: true } is needed if SSL happens outside the hub; see
16
// https://github.com/pillarjs/cookies/issues/51#issuecomment-568182639
17
// It basically tells the server to pretend the connection is secure, even though
18
// it's internal heuristic based on req says it is not secure.
19
const cookies = new Cookies(req, res, { secure: true });
20
const conf = { ...COOKIE_OPTIONS, maxAge };
21
winston.debug(`conf=${JSON.stringify(conf)}`);
22
cookies.set(req.query.set, req.query.value, conf);
23
}
24
res.end();
25
});
26
}
27
28