-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refine login and implement middleware
- Loading branch information
1 parent
d65998f
commit 3c12cc8
Showing
13 changed files
with
203 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Application variables | ||
NEXT_PUBLIC_APP_ENV=production | ||
NEXT_PUBLIC_FISDASWEB_API_URL=http://localhost:8080 | ||
NEXT_PUBLIC_JWT_SECRET=supersecretkey | ||
ACCESS_TOKEN_SECRET=supersecretkey |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 +1,32 @@ | ||
"use client"; | ||
import { useLogout } from "@/auth"; | ||
import { Button } from "@mantine/core"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
export default function Home() { | ||
return <div>Home page</div>; | ||
const logout = useLogout(); | ||
|
||
const router = useRouter(); | ||
|
||
function handleLogout() { | ||
logout.mutate(); | ||
|
||
router.push("/login"); | ||
} | ||
|
||
return ( | ||
<> | ||
<div>Home page</div> | ||
|
||
<Button | ||
color="red" | ||
variant="light" | ||
onClick={() => { | ||
handleLogout(); | ||
}} | ||
> | ||
Log out | ||
</Button> | ||
</> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { stringToUint8Array } from "@/common"; | ||
import { NextRequest } from "next/server"; | ||
import * as jose from "jose"; | ||
|
||
export function isProtectedPath(path: string) { | ||
const PROTECTED_PATHS = ["/"]; | ||
|
||
return PROTECTED_PATHS.includes(path); | ||
} | ||
|
||
export async function verifyAccessTokenFromRequest(request: NextRequest) { | ||
if (!request.cookies.has("access_token")) { | ||
return false; | ||
} | ||
|
||
const key = stringToUint8Array(process.env.ACCESS_TOKEN_SECRET || ""); | ||
|
||
const isTokenVerified = await jose.jwtVerify( | ||
request.cookies.get("access_token")?.value || "", | ||
key | ||
); | ||
|
||
return isTokenVerified; | ||
} |
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,2 +1,3 @@ | ||
export * from "./hooks"; | ||
export * from "./types"; | ||
export * from "./helpers"; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import axios from "axios"; | ||
|
||
export const apiClient = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_FISDASWEB_API_URL, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { isProtectedPath, verifyAccessTokenFromRequest } from "@/auth"; | ||
import { NextResponse } from "next/server"; | ||
import type { NextRequest } from "next/server"; | ||
|
||
// This function can be marked `async` if using `await` inside | ||
export async function middleware(request: NextRequest) { | ||
const path = request.nextUrl.pathname; | ||
|
||
const isAuthenticated = await verifyAccessTokenFromRequest(request); | ||
|
||
const LOGIN_PATH = "/login"; | ||
|
||
if (isProtectedPath(path)) { | ||
if (isAuthenticated) { | ||
return NextResponse.next(); | ||
} | ||
|
||
return NextResponse.redirect(new URL(LOGIN_PATH, request.url)); | ||
} | ||
|
||
if (path === LOGIN_PATH && isAuthenticated) { | ||
return NextResponse.redirect(new URL("/", request.url)); | ||
} | ||
|
||
return NextResponse.next(); | ||
} |