-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
43 lines (33 loc) · 1.15 KB
/
middleware.js
File metadata and controls
43 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { NextResponse } from "next/server";
const isAuthenticated = false;
export function middleware(req) {
// check that user is authenticated or not by using native response api
/* if (isAuthenticated) {
console.log("user authenticated");
return new Response("", { status: 200 });
} else {
console.log("user not authenticated");
return Response.redirect(new URL("/login", req.url));
} */
// check that user is authenticated or not by using NextResponse api
if (req.url.includes("/dashboard")) {
console.log("this middleware called from dashboard page");
if (isAuthenticated) {
return NextResponse.next();
} else {
return NextResponse.redirect(new URL("/login", req.url));
}
}
// use sapareate middleware to saparate routes
if (req.url.includes("/login")) {
console.log("this middleware called from login page");
}
if (req.url.includes("/")) {
console.log("this middleware called from home page");
}
}
// single middleware by using config object
/* export const config = {
matcher: ["/dashboard"],
};
*/