-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathsupertokens.middleware.ts
More file actions
105 lines (94 loc) · 3.18 KB
/
supertokens.middleware.ts
File metadata and controls
105 lines (94 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import cors from "cors";
import SuperTokens from "supertokens-node";
import Dashboard from "supertokens-node/recipe/dashboard";
import {
default as Session,
SessionContainer,
} from "supertokens-node/recipe/session";
import UserMetadata from "supertokens-node/recipe/usermetadata";
import {
APP_NAME,
PORT_DEFAULT_BACKEND,
PORT_DEFAULT_WEB,
} from "@core/constants/core.constants";
import { Status } from "@core/errors/status.codes";
import { Logger } from "@core/logger/winston.logger";
import { ENV } from "@backend/common/constants/env.constants";
import { SupertokensAccessTokenPayload } from "@backend/common/types/supertokens.types";
import { webSocketServer } from "@backend/servers/websocket/websocket.server";
const logger = Logger("app:supertokens.middleware");
export const initSupertokens = () => {
SuperTokens.init({
appInfo: {
appName: APP_NAME,
apiBasePath: "/api",
apiDomain: `http://localhost:${PORT_DEFAULT_BACKEND}`,
websiteBasePath: "/login",
websiteDomain: `http://localhost:${PORT_DEFAULT_WEB}`,
},
supertokens: {
connectionURI: ENV.SUPERTOKENS_URI,
apiKey: ENV.SUPERTOKENS_KEY,
},
framework: "express",
recipeList: [
Dashboard.init(),
Session.init({
errorHandlers: {
onTryRefreshToken: async (message, _request, response) => {
logger.warn(
`Session expired: ${message}. User tried to refresh the session.`,
);
response.setStatusCode(Status.UNAUTHORIZED);
response.sendJSONResponse({
error: "Session expired. Please log in again.",
});
},
},
override: {
apis(originalImplementation) {
return {
...originalImplementation,
async signOutPOST(input) {
const data: SupertokensAccessTokenPayload =
input.session.getAccessTokenPayload();
const socketId = data.sessionHandle;
return originalImplementation.signOutPOST!(input).then(
(res) => {
webSocketServer.handleUserSignOut(socketId!);
return res;
},
);
},
async refreshPOST(input) {
return originalImplementation.refreshPOST!(input).then(
async (session: SessionContainer) => {
const data: SupertokensAccessTokenPayload =
session.getAccessTokenPayload();
const socketId = data.sessionHandle;
webSocketServer.handleUserRefreshToken(socketId!);
logger.debug(
`Session refreshed for user ${data.sub} client.`,
);
return session;
},
);
},
};
},
},
}),
UserMetadata.init(),
],
});
};
export const supertokensCors = () =>
cors({
origin: `http://localhost:${PORT_DEFAULT_WEB}`,
allowedHeaders: [
"content-type",
"st-auth-mode",
...SuperTokens.getAllCORSHeaders(),
],
credentials: true,
});