we can now check if a session is authorized
This commit is contained in:
parent
5e24fc29fb
commit
d63a452f75
4 changed files with 51 additions and 8 deletions
9
src/middlewares/authMiddleware.js
Normal file
9
src/middlewares/authMiddleware.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
const sessionService = require('../services/sessionService');
|
||||
|
||||
async function rejectIfNotAuthorizedMiddleware(req, res, next) {
|
||||
console.log("I'm checking stuff, aight?");
|
||||
console.log(`Is session authorized: ${await sessionService.isSessionAuthorized(req.cookies.sessionUuid)}`)
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = rejectIfNotAuthorizedMiddleware;
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const authMiddleware = require('../middlewares/authMiddleware');
|
||||
const appInviteService = require('../services/appInviteService')
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
|
|
@ -32,7 +33,7 @@ router.get('/invite/:inviteUuid', async (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
router.get('/private', (req, res) => {
|
||||
router.get('/private', authMiddleware, (req, res) => {
|
||||
res.render('private', {});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -18,5 +18,16 @@ async function createInvitedNpub(inviteUuid, npub) {
|
|||
});
|
||||
}
|
||||
|
||||
async function isNpubInvited(npub) {
|
||||
|
||||
if (await InvitedNpub.findOne({
|
||||
where: { invited_npub: npub }
|
||||
})) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
exports.createInvitedNpub = createInvitedNpub;
|
||||
exports.isNpubInvited = isNpubInvited;
|
||||
|
|
@ -3,6 +3,8 @@ const uuid = require("uuid");
|
|||
const Session = require('../models/Session');
|
||||
const SessionNpubbed = require('../models/SessionNpubbed');
|
||||
|
||||
const invitedNpubService = require('./invitedNpubService');
|
||||
|
||||
const constants = require('../constants');
|
||||
|
||||
async function createSession(sessionUuid) {
|
||||
|
|
@ -52,17 +54,37 @@ async function isSessionAlreadyRelatedToNpub(sessionUuid, npub) {
|
|||
}
|
||||
|
||||
|
||||
/* async function getNpubRelatedToSession(sessionUuid) {
|
||||
if (SessionNpubbed.findOne({
|
||||
where: { 'sessionUuid': sessionUuid }
|
||||
}))
|
||||
async function getNpubRelatedToSession(sessionUuid) {
|
||||
|
||||
const mostRecentSession = await SessionNpubbed.findOne({
|
||||
where: { 'session_uuid': sessionUuid },
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
if (!mostRecentSession) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return mostRecentSession.npub;
|
||||
}
|
||||
|
||||
|
||||
async function isSessionAuthorized(sessionUuid) {
|
||||
|
||||
} */
|
||||
|
||||
if (await isSessionExpired(sessionUuid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const npub = await getNpubRelatedToSession(sessionUuid);
|
||||
|
||||
if (await invitedNpubService.isNpubInvited(npub)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
exports.createSession = createSession;
|
||||
exports.isSessionExpired = isSessionExpired;
|
||||
exports.relateSessionToNpub = relateSessionToNpub;
|
||||
exports.isSessionAlreadyRelatedToNpub = isSessionAlreadyRelatedToNpub;
|
||||
exports.isSessionAuthorized = isSessionAuthorized;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue