Refactor to make it login protected first, and public route only if specified
This commit is contained in:
parent
3e3c8e4b6a
commit
c0cc015c81
|
@ -155,7 +155,7 @@ app.use((_req, res, next) => {
|
||||||
|
|
||||||
const router = asyncRouter(app);
|
const router = asyncRouter(app);
|
||||||
|
|
||||||
router.get("/", async (req, res) => {
|
router.get("/", { public: true }, async (req, res) => {
|
||||||
if (!req.context.profile) {
|
if (!req.context.profile) {
|
||||||
return res.render("index");
|
return res.render("index");
|
||||||
}
|
}
|
||||||
|
@ -173,11 +173,11 @@ router.get("/", async (req, res) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/login", (_req, res) => {
|
router.get("/login", { public: true }, (_req, res) => {
|
||||||
res.render("login", { mode });
|
res.render("login", { mode });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/login", async (req, res) => {
|
router.post("/login", { public: true }, async (req, res) => {
|
||||||
const submittedKey =
|
const submittedKey =
|
||||||
req.files && req.files.ssb_key
|
req.files && req.files.ssb_key
|
||||||
? req.files.ssb_key.data.toString()
|
? req.files.ssb_key.data.toString()
|
||||||
|
@ -199,7 +199,7 @@ router.post("/login", async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/download", (_req, res) => {
|
router.get("/download", { public: true }, (_req, res) => {
|
||||||
res.render("download");
|
res.render("download");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -208,7 +208,7 @@ router.get("/logout", async (_req, res) => {
|
||||||
res.redirect("/");
|
res.redirect("/");
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/signup", (req, res) => {
|
router.get("/signup", { public: true }, (req, res) => {
|
||||||
if (req.context.profile) {
|
if (req.context.profile) {
|
||||||
return res.redirect("/");
|
return res.redirect("/");
|
||||||
}
|
}
|
||||||
|
@ -216,7 +216,7 @@ router.get("/signup", (req, res) => {
|
||||||
res.render("signup", { mode });
|
res.render("signup", { mode });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/signup", async (req, res) => {
|
router.post("/signup", { public: true }, async (req, res) => {
|
||||||
const name = req.body.name;
|
const name = req.body.name;
|
||||||
const picture = req.files && req.files.pic;
|
const picture = req.files && req.files.pic;
|
||||||
|
|
||||||
|
@ -294,10 +294,6 @@ router.get("/keys/download", async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/profile/:id(*)", async (req, res) => {
|
router.get("/profile/:id(*)", async (req, res) => {
|
||||||
if (!req.context.profile) {
|
|
||||||
return res.render("index");
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
|
|
||||||
if (id == req.context.profile.id) {
|
if (id == req.context.profile.id) {
|
||||||
|
@ -448,10 +444,6 @@ router.post("/pubs/add", async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/about", (req, res) => {
|
router.get("/about", (req, res) => {
|
||||||
if (!req.context.profile) {
|
|
||||||
return res.render("index");
|
|
||||||
}
|
|
||||||
|
|
||||||
res.render("about");
|
res.render("about");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -489,9 +481,6 @@ router.post("/about", async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/communities", async (req, res) => {
|
router.get("/communities", async (req, res) => {
|
||||||
if (!req.context.profile) {
|
|
||||||
return res.render("index");
|
|
||||||
}
|
|
||||||
const communities = await queries.getCommunities(ssbServer);
|
const communities = await queries.getCommunities(ssbServer);
|
||||||
|
|
||||||
res.render("communities/list", { communities });
|
res.render("communities/list", { communities });
|
||||||
|
@ -507,9 +496,7 @@ const communityData = (req) => {
|
||||||
|
|
||||||
router.get("/communities/:name", async (req, res) => {
|
router.get("/communities/:name", async (req, res) => {
|
||||||
const name = req.params.name;
|
const name = req.params.name;
|
||||||
if (!req.context.profile) {
|
|
||||||
return res.render("index");
|
|
||||||
}
|
|
||||||
const [community, posts] = await Promise.all([
|
const [community, posts] = await Promise.all([
|
||||||
communityData(req),
|
communityData(req),
|
||||||
queries.getCommunityPosts(ssbServer, name),
|
queries.getCommunityPosts(ssbServer, name),
|
||||||
|
@ -555,10 +542,6 @@ router.post("/communities/:name/:key(*)/publish", async (req, res) => {
|
||||||
const key = req.params.key;
|
const key = req.params.key;
|
||||||
const reply = req.body.reply;
|
const reply = req.body.reply;
|
||||||
|
|
||||||
if (!req.context.profile) {
|
|
||||||
return res.render("index");
|
|
||||||
}
|
|
||||||
|
|
||||||
await ssbServer.identities.publishAs({
|
await ssbServer.identities.publishAs({
|
||||||
id: req.context.profile.id,
|
id: req.context.profile.id,
|
||||||
private: false,
|
private: false,
|
||||||
|
@ -577,9 +560,6 @@ router.get("/communities/:name/:key(*)", async (req, res) => {
|
||||||
const name = req.params.name;
|
const name = req.params.name;
|
||||||
const key = "%" + req.params.key;
|
const key = "%" + req.params.key;
|
||||||
|
|
||||||
if (!req.context.profile) {
|
|
||||||
return res.render("index");
|
|
||||||
}
|
|
||||||
const [community, posts] = await Promise.all([
|
const [community, posts] = await Promise.all([
|
||||||
communityData(req),
|
communityData(req),
|
||||||
queries.getPostWithReplies(ssbServer, name, key),
|
queries.getPostWithReplies(ssbServer, name, key),
|
||||||
|
@ -593,10 +573,6 @@ router.get("/communities/:name/:key(*)", async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/search", async (req, res) => {
|
router.get("/search", async (req, res) => {
|
||||||
if (!req.context.profile) {
|
|
||||||
return res.render("index");
|
|
||||||
}
|
|
||||||
|
|
||||||
const query = req.query.query;
|
const query = req.query.query;
|
||||||
|
|
||||||
let results = {
|
let results = {
|
||||||
|
@ -612,7 +588,7 @@ router.get("/search", async (req, res) => {
|
||||||
res.render("search", { ...results, query });
|
res.render("search", { ...results, query });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/blob/*", (req, res) => {
|
router.get("/blob/*", { public: true }, (req, res) => {
|
||||||
serveBlobs(ssbServer)(req, res);
|
serveBlobs(ssbServer)(req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,16 @@ const metrics = require("./metrics");
|
||||||
module.exports.asyncRouter = (app) => {
|
module.exports.asyncRouter = (app) => {
|
||||||
const debug = require("debug")("router");
|
const debug = require("debug")("router");
|
||||||
|
|
||||||
let wrapper = (method, path, fn) => async (req, res, next) => {
|
let wrapper = (method, path, opts, fn) => async (req, res, next) => {
|
||||||
|
if (typeof opts == "function") fn = opts;
|
||||||
|
if (!opts.public && !req.context.profile) {
|
||||||
|
if (method == "POST") {
|
||||||
|
res.status(401);
|
||||||
|
return res.send("You are not logged in");
|
||||||
|
}
|
||||||
|
return res.redirect("/");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
debug(`${method} ${path}`);
|
debug(`${method} ${path}`);
|
||||||
metrics.router.inc({ method, path });
|
metrics.router.inc({ method, path });
|
||||||
|
@ -17,12 +26,12 @@ module.exports.asyncRouter = (app) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
get: (path, fn) => {
|
get: (path, fn, opts) => {
|
||||||
app.get(path, wrapper("GET", path, fn));
|
app.get(path, wrapper("GET", path, fn, opts));
|
||||||
},
|
},
|
||||||
post: (path, fn) => {
|
post: (path, fn, opts) => {
|
||||||
debug(`POST ${path}`);
|
debug(`POST ${path}`);
|
||||||
app.post(path, wrapper("POST", path, fn));
|
app.post(path, wrapper("POST", path, fn, opts));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue