format project
This commit is contained in:
parent
90d8e39eb3
commit
c02cf8c12e
39 changed files with 2062 additions and 909 deletions
|
|
@ -1,13 +1,12 @@
|
|||
const sessionService = require('../services/sessionService');
|
||||
|
||||
async function attachPublicKeyMiddleware(req, res, next) {
|
||||
const publicKey = await sessionService.getPublicKeyRelatedToSession(
|
||||
req.cookies.sessionUuid
|
||||
);
|
||||
req.cookies.publicKey = publicKey;
|
||||
|
||||
const publicKey = await sessionService.getPublicKeyRelatedToSession(
|
||||
req.cookies.sessionUuid
|
||||
)
|
||||
req.cookies.publicKey = publicKey;
|
||||
|
||||
next();
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = attachPublicKeyMiddleware;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
const sessionService = require('../services/sessionService');
|
||||
|
||||
async function redirectIfNotAuthorizedMiddleware(req, res, next) {
|
||||
if (!(await sessionService.isSessionAuthorized(req.cookies.sessionUuid))) {
|
||||
res.redirect('/');
|
||||
}
|
||||
next();
|
||||
if (!(await sessionService.isSessionAuthorized(req.cookies.sessionUuid))) {
|
||||
res.redirect('/');
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = redirectIfNotAuthorizedMiddleware;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
const sessionService = require('../services/sessionService');
|
||||
|
||||
async function rejectIfNotAuthorizedMiddleware(req, res, next) {
|
||||
if (!(await sessionService.isSessionAuthorized(req.cookies.sessionUuid))) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
message: 'Your session is not authorized.'
|
||||
})
|
||||
}
|
||||
next();
|
||||
if (!(await sessionService.isSessionAuthorized(req.cookies.sessionUuid))) {
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
message: 'Your session is not authorized.',
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = rejectIfNotAuthorizedMiddleware;
|
||||
|
|
|
|||
|
|
@ -1,32 +1,33 @@
|
|||
const uuid = require("uuid");
|
||||
const uuid = require('uuid');
|
||||
|
||||
const sessionService = require('../services/sessionService');
|
||||
const constants = require('../constants');
|
||||
|
||||
async function setAndPersistNewSession(res) {
|
||||
const sessionUuid = uuid.v7();
|
||||
res.cookie('sessionUuid', sessionUuid, { httpOnly: true, maxAge: constants.DEFAULT_SESSION_DURATION_SECONDS * 1000 });
|
||||
return await sessionService.createSession(sessionUuid);
|
||||
const sessionUuid = uuid.v7();
|
||||
res.cookie('sessionUuid', sessionUuid, {
|
||||
httpOnly: true,
|
||||
maxAge: constants.DEFAULT_SESSION_DURATION_SECONDS * 1000,
|
||||
});
|
||||
return await sessionService.createSession(sessionUuid);
|
||||
}
|
||||
|
||||
async function createSessionMiddleware(req, res, next) {
|
||||
const sessionUuid = req.cookies.sessionUuid;
|
||||
|
||||
const sessionUuid = req.cookies.sessionUuid;
|
||||
|
||||
if (!sessionUuid) {
|
||||
const newSession = await setAndPersistNewSession(res);
|
||||
req.cookies.sessionUuid = newSession.uuid;
|
||||
if (!sessionUuid) {
|
||||
const newSession = await setAndPersistNewSession(res);
|
||||
req.cookies.sessionUuid = newSession.uuid;
|
||||
}
|
||||
|
||||
if (sessionUuid) {
|
||||
if (!(await sessionService.isSessionValid(sessionUuid))) {
|
||||
const newSession = await setAndPersistNewSession(res);
|
||||
req.cookies.sessionUuid = newSession.uuid;
|
||||
}
|
||||
}
|
||||
|
||||
if (sessionUuid) {
|
||||
if (!(await sessionService.isSessionValid(sessionUuid))) {
|
||||
const newSession = await setAndPersistNewSession(res);
|
||||
req.cookies.sessionUuid = newSession.uuid;
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = createSessionMiddleware;
|
||||
module.exports = createSessionMiddleware;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue