First commit
This commit is contained in:
17
authserver/middlewares/authMiddleware.js
Normal file
17
authserver/middlewares/authMiddleware.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const restrictedPaths = ["/index"];
|
||||
|
||||
const authMiddleware = async (context, next) => {
|
||||
const user = await context.state.session.get("user");
|
||||
if (
|
||||
!user &&
|
||||
restrictedPaths.some((path) =>
|
||||
context.request.url.pathname.startsWith(path)
|
||||
)
|
||||
) {
|
||||
context.response.redirect("/auth/login");
|
||||
} else {
|
||||
await next();
|
||||
}
|
||||
};
|
||||
|
||||
export { authMiddleware };
|
||||
9
authserver/middlewares/errorMiddleware.js
Normal file
9
authserver/middlewares/errorMiddleware.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const errorMiddleware = async (context, next) => {
|
||||
try {
|
||||
await next();
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
};
|
||||
|
||||
export { errorMiddleware };
|
||||
24
authserver/middlewares/renderMiddleware.js
Normal file
24
authserver/middlewares/renderMiddleware.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import { configure, renderFile } from "../deps.js";
|
||||
|
||||
const renderMiddleware = async (context, next) => {
|
||||
configure({
|
||||
views: `${Deno.cwd()}/views/`,
|
||||
});
|
||||
|
||||
context.render = async (file, data) => {
|
||||
if (!data) {
|
||||
data = {};
|
||||
}
|
||||
|
||||
if (context.user) {
|
||||
data.user = context.user;
|
||||
}
|
||||
|
||||
context.response.headers.set("Content-Type", "text/html; charset=utf-8");
|
||||
context.response.body = await renderFile(file, data);
|
||||
};
|
||||
|
||||
await next();
|
||||
};
|
||||
|
||||
export { renderMiddleware };
|
||||
15
authserver/middlewares/serveStaticMiddleware.js
Normal file
15
authserver/middlewares/serveStaticMiddleware.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { send } from "../deps.js";
|
||||
|
||||
const serveStaticMiddleware = async (context, next) => {
|
||||
if (context.request.url.pathname.startsWith("/static")) {
|
||||
const path = context.request.url.pathname.substring(7);
|
||||
|
||||
await send(context, path, {
|
||||
root: `${Deno.cwd()}/static`,
|
||||
});
|
||||
} else {
|
||||
await next();
|
||||
}
|
||||
};
|
||||
|
||||
export { serveStaticMiddleware };
|
||||
14
authserver/middlewares/userMiddleware.js
Normal file
14
authserver/middlewares/userMiddleware.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import * as authService from "../services/authService.js";
|
||||
|
||||
const userMiddleware = async (context, next) => {
|
||||
const user = await context.state.session.get("user");
|
||||
|
||||
if (user) {
|
||||
const userFromDatabase = await authService.findUser(user.username);
|
||||
context.user = userFromDatabase[0];
|
||||
}
|
||||
|
||||
await next();
|
||||
};
|
||||
|
||||
export { userMiddleware };
|
||||
Reference in New Issue
Block a user