Skip to content

Commit ffdca6e

Browse files
committed
fix possible memory leaks
1 parent 44d3b2d commit ffdca6e

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@
6363
"engines": {
6464
"node": "^22.11.0"
6565
}
66-
}
66+
}

src/app.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,22 @@ passport.serializeUser((user, done) => {
9393
done(null, user.id);
9494
});
9595

96-
passport.deserializeUser(async (id: string, done) => {
96+
const deserializeUser = async (id: string, done: (err: any, user?: User | null) => void) => {
9797
const user = await prisma.user.findUnique({ where: { id: id } });
9898
done(null, user);
99-
});
99+
};
100+
101+
passport.deserializeUser(deserializeUser);
100102

101103
// Serve the static files to be accessed by the docs app
102104
app.use(express.static(path.join(__dirname, '..', 'docs')));
103105

104-
// Public Endpoints
105-
app.get(`${API_URL}`, (req, res) => {
106+
const welcomeApi = (req: Request, res: Response) => {
106107
return res.status(200).send('Welcome to the TEACHING-WEBSITE-API V1.0');
107-
});
108+
};
109+
110+
// Public Endpoints
111+
app.get(`${API_URL}`, welcomeApi);
108112

109113
const SessionOauthStrategy = (req: Request, res: Response, next: NextFunction) => {
110114
if (req.isAuthenticated()) {
@@ -113,7 +117,7 @@ const SessionOauthStrategy = (req: Request, res: Response, next: NextFunction) =
113117
passport.authenticate('oauth-bearer', { session: true })(req, res, next);
114118
};
115119

116-
app.get(`${API_URL}/checklogin`, SessionOauthStrategy, async (req, res, next) => {
120+
const checkLogin = async (req: Request, res: Response, next: NextFunction) => {
117121
try {
118122
if (req.user) {
119123
return res.status(200).send('OK');
@@ -122,9 +126,11 @@ app.get(`${API_URL}/checklogin`, SessionOauthStrategy, async (req, res, next) =>
122126
} catch (error) {
123127
next(error);
124128
}
125-
});
129+
};
130+
131+
app.get(`${API_URL}/checklogin`, SessionOauthStrategy, checkLogin);
126132

127-
app.post(`${API_URL}/logout`, async (req, res, next) => {
133+
const logout = async (req: Request, res: Response, next: NextFunction) => {
128134
req.logout({ keepSessionInfo: false }, (err) => {
129135
if (err) {
130136
Logger.error(err);
@@ -135,7 +141,9 @@ app.post(`${API_URL}/logout`, async (req, res, next) => {
135141
Logger.info(req.session);
136142
// await prisma.sessions.delete({ where: { sid: req.session.id } });
137143
res.clearCookie(SESSION_KEY).send();
138-
});
144+
};
145+
146+
app.post(`${API_URL}/logout`, logout);
139147

140148
export const configure = (_app: typeof app) => {
141149
/**

0 commit comments

Comments
 (0)