Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/client/account.ts
1503 views
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 { callback } from "awaiting";
7
declare const $: any; // jQuery
8
import { WebappClient } from "./client";
9
import type { ApiKey } from "@cocalc/util/db-schema/api-keys";
10
import api from "./api";
11
12
export class AccountClient {
13
private client: WebappClient;
14
15
constructor(client: WebappClient) {
16
this.client = client;
17
}
18
19
cookies = async (mesg): Promise<void> => {
20
const f = (cb) => {
21
const j = $.ajax({
22
url: mesg.url,
23
data: { id: mesg.id, set: mesg.set, get: mesg.get, value: mesg.value },
24
});
25
j.done(() => cb());
26
j.fail(() => cb("failed"));
27
};
28
await callback(f);
29
};
30
31
sign_out = async (everywhere: boolean = false): Promise<void> => {
32
await api("/accounts/sign-out", { all: everywhere });
33
delete this.client.account_id;
34
this.client.emit("signed_out");
35
};
36
37
change_password = async (
38
currentPassword: string,
39
newPassword: string = "",
40
): Promise<void> => {
41
await api("/accounts/set-password", { currentPassword, newPassword });
42
};
43
44
change_email = async (
45
new_email_address: string,
46
password: string = "",
47
): Promise<void> => {
48
if (this.client.account_id == null) {
49
throw Error("must be logged in");
50
}
51
await api("accounts/set-email-address", {
52
email_address: new_email_address,
53
password,
54
});
55
};
56
57
send_verification_email = async (
58
only_verify: boolean = true,
59
): Promise<void> => {
60
await this.client.conat_client.hub.system.sendEmailVerification({
61
only_verify,
62
});
63
};
64
65
// forget about a given passport authentication strategy for this user
66
unlink_passport = async (strategy: string, id: string): Promise<void> => {
67
await this.client.conat_client.hub.system.deletePassport({
68
strategy,
69
id,
70
});
71
};
72
73
// new interface: getting, setting, editing, deleting, etc., the api keys for a project
74
api_keys = async (opts: {
75
action: "get" | "delete" | "create" | "edit";
76
password?: string;
77
name?: string;
78
id?: number;
79
expire?: Date;
80
}): Promise<ApiKey[] | undefined> => {
81
return await this.client.conat_client.hub.system.manageApiKeys(opts);
82
};
83
}
84
85