Path: blob/master/src/packages/frontend/client/account.ts
1503 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { callback } from "awaiting";6declare const $: any; // jQuery7import { WebappClient } from "./client";8import type { ApiKey } from "@cocalc/util/db-schema/api-keys";9import api from "./api";1011export class AccountClient {12private client: WebappClient;1314constructor(client: WebappClient) {15this.client = client;16}1718cookies = async (mesg): Promise<void> => {19const f = (cb) => {20const j = $.ajax({21url: mesg.url,22data: { id: mesg.id, set: mesg.set, get: mesg.get, value: mesg.value },23});24j.done(() => cb());25j.fail(() => cb("failed"));26};27await callback(f);28};2930sign_out = async (everywhere: boolean = false): Promise<void> => {31await api("/accounts/sign-out", { all: everywhere });32delete this.client.account_id;33this.client.emit("signed_out");34};3536change_password = async (37currentPassword: string,38newPassword: string = "",39): Promise<void> => {40await api("/accounts/set-password", { currentPassword, newPassword });41};4243change_email = async (44new_email_address: string,45password: string = "",46): Promise<void> => {47if (this.client.account_id == null) {48throw Error("must be logged in");49}50await api("accounts/set-email-address", {51email_address: new_email_address,52password,53});54};5556send_verification_email = async (57only_verify: boolean = true,58): Promise<void> => {59await this.client.conat_client.hub.system.sendEmailVerification({60only_verify,61});62};6364// forget about a given passport authentication strategy for this user65unlink_passport = async (strategy: string, id: string): Promise<void> => {66await this.client.conat_client.hub.system.deletePassport({67strategy,68id,69});70};7172// new interface: getting, setting, editing, deleting, etc., the api keys for a project73api_keys = async (opts: {74action: "get" | "delete" | "create" | "edit";75password?: string;76name?: string;77id?: number;78expire?: Date;79}): Promise<ApiKey[] | undefined> => {80return await this.client.conat_client.hub.system.manageApiKeys(opts);81};82}838485