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/next/pages/api/v2/accounts/set-email-address.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Set email address for an existing account.
8
9
You must include both the email address and password. If there is no password
10
currently set for the account, you have to set one as part of this request.
11
*/
12
13
import setEmailAddress from "@cocalc/server/accounts/set-email-address";
14
import getAccountId from "lib/account/get-account";
15
import getParams from "lib/api/get-params";
16
17
import { apiRoute, apiRouteOperation } from "lib/api";
18
import { SuccessStatus } from "lib/api/status";
19
import {
20
SetAccountEmailAddressInputSchema,
21
SetAccountEmailAddressOutputSchema,
22
} from "lib/api/schema/accounts/set-email-address";
23
24
async function handle(req, res) {
25
const account_id = await getAccountId(req);
26
if (account_id == null) {
27
res.json({ error: "must be signed in" });
28
return;
29
}
30
const { email_address, password } = getParams(req);
31
try {
32
await setEmailAddress({ account_id, email_address, password });
33
res.json(SuccessStatus);
34
} catch (err) {
35
if (err.message.includes("duplicate key")) {
36
err = Error(
37
`The email address "${email_address}" is already in use by another account.`,
38
);
39
}
40
res.json({ error: err.message });
41
}
42
}
43
44
export default apiRoute({
45
setEmailAddress: apiRouteOperation({
46
method: "POST",
47
openApiOperation: {
48
tags: ["Accounts"],
49
},
50
})
51
.input({
52
contentType: "application/json",
53
body: SetAccountEmailAddressInputSchema,
54
})
55
.outputs([
56
{
57
status: 200,
58
contentType: "application/json",
59
body: SetAccountEmailAddressOutputSchema,
60
},
61
])
62
.handler(handle),
63
});
64
65