Fixed Logging In Issue
This commit was merged in pull request #9.
This commit is contained in:
+19
-39
@@ -1,35 +1,10 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
const checkAuthentication = async () => {
|
||||
const token = req.cookies["cmc-token"]; // Access the token from cookies
|
||||
console.log("checking token", token);
|
||||
const isAuthenticated = token ? true : false; // Check if the user is authenticated.
|
||||
return isAuthenticated;
|
||||
};
|
||||
import { getCookie, hasCookie } from "cookies-next";
|
||||
|
||||
const isTokenValid = () => {
|
||||
if (typeof window === "undefined") {
|
||||
return false; // Don't execute this code on the server-side
|
||||
}
|
||||
|
||||
const cookies = document.cookie.split("; "); // Get all cookies and split them into an array
|
||||
|
||||
for (const cookie of cookies) {
|
||||
const [name, value] = cookie.split("="); // Split the cookie into its name and value
|
||||
|
||||
if (name.trim() === "cmc-token" && value) {
|
||||
return true; // The cmc-token cookie exists
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export async function middleware(req) {
|
||||
const token = isTokenValid();
|
||||
// req.cookies["cmc-token"]; // Access the token from cookies
|
||||
const cookieList = cookies();
|
||||
export async function middleware(req, next) {
|
||||
const token = getCookie("cmc-token", { req }); // Access the token from cookies
|
||||
|
||||
const headers = new Headers(req.headers);
|
||||
headers.set("X-XSS-Protection", "1; mode=block");
|
||||
@@ -38,13 +13,19 @@ export async function middleware(req) {
|
||||
|
||||
const { origin, pathname } = req.nextUrl;
|
||||
|
||||
|
||||
try {
|
||||
if (pathname === "/auth/login" && token) {
|
||||
console.log("Test path", pathname, origin);
|
||||
if (token) {
|
||||
// Redirect to the home page if already authenticated
|
||||
return NextResponse.redirect(new URL("/"), { status: 307 });
|
||||
NextResponse.redirect(new URL("/"), { status: 201 });
|
||||
next();
|
||||
}
|
||||
|
||||
if (!authenticationPages.includes(pathname) && !token) {
|
||||
if (
|
||||
!authenticationPages.includes(pathname) ||
|
||||
(authenticationPages.includes(pathname) && !token)
|
||||
) {
|
||||
// Redirect to the login page if not authenticated
|
||||
return NextResponse.redirect(new URL("/auth/login", origin), {
|
||||
status: 307,
|
||||
@@ -53,15 +34,14 @@ export async function middleware(req) {
|
||||
|
||||
// Add authentication logic here (verify the token, etc.)
|
||||
// const isAuthenticated = verifyToken(token);
|
||||
const isAuthenticated = cookieList.has("cmc-token");
|
||||
console.log(token);
|
||||
// const isAuthenticated = hasCookie("cmc-token", { req });
|
||||
|
||||
if (!isAuthenticated) {
|
||||
// Handle unauthenticated users
|
||||
return NextResponse.error(new Error("Authentication failed"), {
|
||||
status: 401,
|
||||
});
|
||||
}
|
||||
// if (!isAuthenticated) {
|
||||
// // Handle unauthenticated users
|
||||
// return NextResponse.error(new Error("Authentication failed"), {
|
||||
// status: 401,
|
||||
// });
|
||||
// }
|
||||
|
||||
// Continue with the request if authenticated
|
||||
return NextResponse.next();
|
||||
|
||||
Reference in New Issue
Block a user