Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/accounts/sign-out.ts
1452 views
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
import { apiRoute, apiRouteOperation } from "lib/api";
16
import { SuccessStatus } from "lib/api/status";
17
import {
18
AccountSignOutInputSchema,
19
AccountSignOutOutputSchema,
20
} from "lib/api/schema/accounts/sign-out";
21
import {
22
ACCOUNT_ID_COOKIE_NAME,
23
REMEMBER_ME_COOKIE_NAME,
24
} from "@cocalc/backend/auth/cookie-names";
25
26
async function handle(req, res) {
27
try {
28
await signOut(req, res);
29
res.json(SuccessStatus);
30
} catch (err) {
31
res.json({ error: err.message });
32
}
33
}
34
35
async function signOut(req, res): Promise<void> {
36
const { all } = getParams(req);
37
if (all) {
38
// invalidate all remember me cookies for this account.
39
const account_id = await getAccountId(req);
40
if (!account_id) return; // not signed in
41
await deleteAllRememberMe(account_id);
42
} else {
43
const hash = getRememberMeHash(req);
44
if (!hash) return; // not signed in
45
await deleteRememberMe(hash);
46
}
47
// also delete any security relevant cookies for safety and to avoid confusion.
48
res.clearCookie(REMEMBER_ME_COOKIE_NAME);
49
res.clearCookie(ACCOUNT_ID_COOKIE_NAME);
50
}
51
52
export default apiRoute({
53
signOut: apiRouteOperation({
54
method: "POST",
55
openApiOperation: {
56
tags: ["Accounts"],
57
},
58
})
59
.input({
60
contentType: "application/json",
61
body: AccountSignOutInputSchema,
62
})
63
.outputs([
64
{
65
status: 200,
66
contentType: "application/json",
67
body: AccountSignOutOutputSchema,
68
},
69
])
70
.handler(handle),
71
});
72
73