Major bug fix
This commit is contained in:
18
drill-and-practice/middlewares/authMiddleware.js
Normal file
18
drill-and-practice/middlewares/authMiddleware.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const restrictedPaths = ["/quiz", "/topics"];
|
||||
|
||||
const authMiddleware = async (state, next) => {
|
||||
const user = await 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 };
|
||||
@@ -1,4 +1,4 @@
|
||||
const errorMiddleware = async (context, next) => {
|
||||
const errorMiddleware = async (next) => {
|
||||
try {
|
||||
await next();
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
import { configure, renderFile } from "../deps.js";
|
||||
|
||||
const renderMiddleware = async (context, next) => {
|
||||
configure({
|
||||
views: `${Deno.cwd()}/views/`,
|
||||
});
|
||||
configure({
|
||||
views: `${Deno.cwd()}/views/`,
|
||||
});
|
||||
|
||||
const renderMiddleware = async (context, next) => {
|
||||
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);
|
||||
};
|
||||
|
||||
14
drill-and-practice/middlewares/userMiddleware.js
Normal file
14
drill-and-practice/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.email);
|
||||
context.user = userFromDatabase[0];
|
||||
}
|
||||
|
||||
await next();
|
||||
};
|
||||
|
||||
export { userMiddleware };
|
||||
Reference in New Issue
Block a user