-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configures middleware to handle auth
- Loading branch information
Showing
4 changed files
with
46 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
.container | ||
padding: 0 100px | ||
padding: 0 120px | ||
.button | ||
margin-bottom: 10px | ||
.label | ||
margin-top: 0 | ||
margin-bottom: 12px | ||
font-size: 14px | ||
text-align: center |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
import { NextRequest } from "next/server"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { getToken } from "next-auth/jwt"; | ||
|
||
// Limit the middleware to paths starting with `/api/` | ||
export const config = { | ||
matcher: "/api/:function*", | ||
}; | ||
const PROTECTED_API_ROUTES = ["/api/todo"]; | ||
const PROTECTED_ROUTES = ["/dashboard"]; | ||
|
||
export async function middleware(request: NextRequest) { | ||
const token = await getToken({ req: request }); | ||
const isProtectedApiRoute = PROTECTED_API_ROUTES.some((route: string) => request.nextUrl?.pathname?.startsWith(route)); | ||
const isProtectedRoute = PROTECTED_ROUTES.some((route: string) => request.nextUrl?.pathname?.startsWith(route)); | ||
|
||
// TODO :: Token is availble here but need to handle auth for currect routes | ||
|
||
const isAuthenticated = true; // TODO :: Implement Role based Auth | ||
if (isProtectedApiRoute) { | ||
if (!isAuthenticated(request)) { | ||
return Response.json({ success: false, message: "Authentication failed" }, { status: 401 }); | ||
} | ||
} | ||
|
||
if (!isAuthenticated) { | ||
return Response.json({ success: false, message: "Authentication failed" }, { status: 401 }); | ||
if (isProtectedRoute) { | ||
if (!isAuthenticated(request)) { | ||
return NextResponse.redirect(new URL("/", request.url)); | ||
} | ||
} | ||
|
||
return NextResponse.next(); | ||
} | ||
|
||
const isAuthenticated = async (request: NextRequest) => { | ||
const token = await getToken({ req: request }); | ||
return !!token && Date.now() <= token.exp * 1000; | ||
}; |