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/sign-out.ts
Views: 687
1
/*
2
Sign out of the current session or all sessions.
3
4
This invalidates 1 or more remember me cookies for
5
the account that is making the API request.
6
*/
7
8
import getAccountId from "lib/account/get-account";
9
import { getRememberMeHash } from "@cocalc/server/auth/remember-me";
10
import {
11
deleteRememberMe,
12
deleteAllRememberMe,
13
} from "@cocalc/server/auth/remember-me";
14
import getParams from "lib/api/get-params";
15
16
import { apiRoute, apiRouteOperation } from "lib/api";
17
import { SuccessStatus } from "lib/api/status";
18
import {
19
AccountSignOutInputSchema,
20
AccountSignOutOutputSchema,
21
} from "lib/api/schema/accounts/sign-out";
22
23
async function handle(req, res) {
24
try {
25
await signOut(req);
26
res.json(SuccessStatus);
27
} catch (err) {
28
res.json({ error: err.message });
29
}
30
}
31
32
async function signOut(req): Promise<void> {
33
const { all } = getParams(req);
34
if (all) {
35
// invalidate all remember me cookies for this account.
36
const account_id = await getAccountId(req);
37
if (!account_id) return; // not signed in
38
await deleteAllRememberMe(account_id);
39
} else {
40
const hash = getRememberMeHash(req);
41
if (!hash) return; // not signed in
42
await deleteRememberMe(hash);
43
}
44
}
45
46
export default apiRoute({
47
signOut: apiRouteOperation({
48
method: "POST",
49
openApiOperation: {
50
tags: ["Accounts"],
51
},
52
})
53
.input({
54
contentType: "application/json",
55
body: AccountSignOutInputSchema,
56
})
57
.outputs([
58
{
59
status: 200,
60
contentType: "application/json",
61
body: AccountSignOutOutputSchema,
62
},
63
])
64
.handler(handle),
65
});
66
67