This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.ts
71 lines (59 loc) · 1.79 KB
/
session.ts
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
import { NextApiRequest, NextApiResponse } from 'next';
import { IAuthSession, withSessionRoute } from '@lib/AuthSession';
import logger from '@utils/logger';
/**
* Get Login session.
* If there's a session cookie, it will return ip address and port number.
* If there's no session cookie, it will return empty strings for ip address and port number.
*
* @param req - HTTP request provided by NextJS
* @param res - HTTP response provided by NextJS
*/
const handleGet = (req: NextApiRequest, res: NextApiResponse<IAuthSession>) => {
const getLogger = logger.scope('/api/auth/session', 'GET');
getLogger.info(`checking is auth session exists`);
const response: IAuthSession = {
ipAddress: '',
port: '',
password: '',
};
if (req.session.authSession) {
const { ipAddress, port } = req.session.authSession;
response.ipAddress = ipAddress;
response.port = port;
getLogger.info(`auth session found`);
} else {
getLogger.info(`auth session NOT found`);
}
getLogger.complete(`sending response`);
getLogger.debug(`response data`, response);
res.status(200).json(response);
};
/**
* Default method to run when executing this http api endpoint
*
* @remarks
* HTTP API endpoint `/api/auth/session`
*
* @remarks
* HTTP method allowed: `GET`
*/
const mainHandler = (req: NextApiRequest, res: NextApiResponse) => {
const { method = '' } = req;
// limit which HTTP methods are allowed
switch (method) {
case 'GET': {
handleGet(req, res);
break;
}
default: {
logger.error({
prefix: `/api/auth/session`,
message: `invalid HTTP method type '${method}'`,
});
res.setHeader('Allow', ['GET']);
res.status(405).end(`Method ${method} Not Allowed`);
}
}
};
export default withSessionRoute(mainHandler);