2023-03-13 13:42:43 +01:00
|
|
|
import * as userStorage from '../services/storage/userStorage.js';
|
|
|
|
|
import cookieSession from 'cookie-session';
|
|
|
|
|
import { nanoid } from 'nanoid';
|
2021-01-21 16:09:23 +01:00
|
|
|
const unauthorized = (res) => {
|
|
|
|
|
return res.send(401);
|
|
|
|
|
};
|
|
|
|
|
const isUnauthorized = (req) => {
|
|
|
|
|
return req.session.currentUser == null;
|
|
|
|
|
};
|
|
|
|
|
const isAdmin = (req) => {
|
|
|
|
|
if (!isUnauthorized(req)) {
|
|
|
|
|
const user = userStorage.getUser(req.session.currentUser);
|
|
|
|
|
return user != null && user.isAdmin;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
const authInterceptor = () => {
|
|
|
|
|
return (req, res, next) => {
|
|
|
|
|
if (isUnauthorized(req)) {
|
|
|
|
|
return unauthorized(res);
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
const adminInterceptor = () => {
|
|
|
|
|
return (req, res, next) => {
|
|
|
|
|
if (!isAdmin(req)) {
|
|
|
|
|
return unauthorized(res);
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-03-13 13:42:43 +01:00
|
|
|
const cookieSession$0 = (userId) => {
|
2021-01-21 16:09:23 +01:00
|
|
|
return cookieSession({
|
|
|
|
|
name: 'fredy-admin-session',
|
|
|
|
|
keys: ['fredy', 'super', 'fancy', 'key', nanoid()],
|
|
|
|
|
userId,
|
2021-01-21 16:14:47 +01:00
|
|
|
maxAge: 8 * 60 * 60 * 1000, // 8 hours
|
2021-01-21 16:09:23 +01:00
|
|
|
});
|
|
|
|
|
};
|
2023-03-13 13:42:43 +01:00
|
|
|
export { cookieSession$0 as cookieSession };
|
|
|
|
|
export { adminInterceptor };
|
|
|
|
|
export { authInterceptor };
|
|
|
|
|
export { isUnauthorized };
|
|
|
|
|
export { isAdmin };
|