-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (25 loc) · 1.05 KB
/
index.js
File metadata and controls
36 lines (25 loc) · 1.05 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
import { createRouter } from "next-connect";
import controller from "infra/controller.js";
import authentication from "models/authentication.js";
import session from "models/session.js";
const router = createRouter();
router.post(postHandler);
router.delete(deleteHandler);
export default router.handler(controller.errorHandlers);
async function postHandler(request, response) {
const userInputValues = request.body;
const authenticatedUser = await authentication.getAuthenticatedUser(
userInputValues.email,
userInputValues.password,
);
const newSession = await session.create(authenticatedUser.id);
controller.setSessionCookie(newSession.token, response);
return response.status(201).json(newSession);
}
async function deleteHandler(request, response) {
const sessionToken = request.cookies.session_id;
const sessionObject = await session.findOneValidByToken(sessionToken);
const expiredSession = await session.expireById(sessionObject.id);
controller.clearSessionCookie(response);
return response.status(200).json(expiredSession);
}