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/frontend/client/handle-target.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
import { hasRememberMe } from "@cocalc/frontend/misc/remember-me";
7
import { join } from "path";
8
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
9
import { encode_path } from "@cocalc/util/misc";
10
11
export const IS_EMBEDDED = new URL(location.href).pathname.endsWith(
12
"embed.html"
13
);
14
15
function handleTarget(): string {
16
// See src/packages/hub/servers/app/app-redirect.ts where if
17
// there is a path after the base url, we redirect to static/app.html
18
// and put that path in a query param called 'target'.
19
const u = new URL(location.href);
20
let t = u.searchParams.get("target") ?? "";
21
// We use the URL object and a fake host to parse things, since it's much
22
// more secure/robust than parsing it directly.
23
const url = new URL("http://" + join("host", t));
24
let target = decodeURIComponent(url.pathname.slice(1));
25
if (!target) {
26
// if no target is encoded in the url:
27
if (hasRememberMe(appBasePath)) {
28
// probably signed in -- show user their list of projects
29
target = "projects";
30
} else {
31
// not signed in -- show sign in page (which is currently part of "settings", which is dumb)
32
target = "settings";
33
}
34
}
35
if (!IS_EMBEDDED) {
36
u.searchParams.delete("target");
37
u.searchParams.forEach((val, key) => {
38
url.searchParams.set(key, val);
39
});
40
// Write the full url for the given target, preserving any search (except target) and fragment parts of the url.
41
const fullUrl =
42
document.location.origin +
43
join(appBasePath, encode_path(target)) +
44
url.search +
45
u.hash;
46
history.pushState({}, "", fullUrl);
47
}
48
return target;
49
}
50
51
const target: string = handleTarget();
52
53
export default target;
54
55