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
/
logout.ts
63 lines (55 loc) · 1.54 KB
/
logout.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
import { NextApiRequest, NextApiResponse } from 'next';
import { withSessionRoute } from '@lib/AuthSession';
import logger from '@utils/logger';
/**
* Data sent as a Response
*/
export interface DeleteResponseData {
/**
* Message sent to the requester.
* Could be a success message.
*/
message: string;
}
/**
* Remove Session cookie used for Pi-hole authentication
*
* @param req - HTTP request provided by NextJS
* @param res - HTTP response provided by NextJS
*/
const handleDelete = (req: NextApiRequest, res: NextApiResponse<DeleteResponseData>) => {
const deleteLogger = logger.scope('/api/auth/logout', 'DELETE');
deleteLogger.info('removing Session cookie');
req.session.destroy();
deleteLogger.success('Session cookie removed');
const responseMessage: DeleteResponseData = {
message: 'success',
};
deleteLogger.complete(`sending response`);
deleteLogger.debug(`response data`, responseMessage);
res.status(200).json(responseMessage);
};
/**
* Default method to run when executing this http api endpoint
*
* @remarks
* HTTP API endpoint `/api/auth/logout`
*
* @remarks
* HTTP method allowed: `DELETE`
*/
const mainHandler = (req: NextApiRequest, res: NextApiResponse) => {
const { method = '' } = req;
// limit which HTTP methods are allowed
switch (method) {
case 'DELETE': {
handleDelete(req, res);
break;
}
default: {
res.setHeader('Allow', ['DELETE']);
res.status(405).end(`Method ${method} Not Allowed`);
}
}
};
export default withSessionRoute(mainHandler);