-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from CS3219-AY2425S1/ryan/auth-guard
Auth Guarding webpages
- Loading branch information
Showing
7 changed files
with
72 additions
and
79 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
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,15 +1,26 @@ | ||
"use client"; | ||
|
||
const KEY: string = "TOKEN"; | ||
|
||
export function setToken(token: string): void { | ||
window.localStorage[KEY] = token | ||
document.cookie = `${KEY}=${token}` | ||
} | ||
|
||
export function getToken(): string { | ||
return KEY in window.localStorage ? window.localStorage[KEY] : "" | ||
const keyValue = document.cookie.split("; ").find(kv => kv.startsWith(`${KEY}=`)) | ||
if (keyValue == undefined) { | ||
return ""; | ||
} | ||
|
||
const [_, value] = keyValue.split("="); | ||
|
||
if (value == undefined) { | ||
return ""; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
export function deleteToken() { | ||
if (KEY in window.localStorage) { | ||
window.localStorage.removeItem(KEY); | ||
} | ||
document.cookie = `${KEY}=nothinghere;expires=0`; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { NextURL } from 'next/dist/server/web/next-url'; | ||
import { type NextRequest, NextResponse } from 'next/server'; | ||
|
||
const PUBLIC_ROUTES = ["/login", "/register"]; | ||
|
||
async function isValidToken(TOKEN: string): Promise<boolean> { | ||
const { status } = await fetch( | ||
`${process.env.NEXT_PUBLIC_USER_SERVICE_URL}auth/verify-token`, | ||
{ | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${TOKEN}`, | ||
}, | ||
} | ||
); | ||
return status === 200; | ||
} | ||
|
||
export default async function middleware(request: NextRequest) { | ||
const REDIRECT_TO_LOGIN = NextResponse.redirect(new NextURL("/login", request.url)); | ||
const TOKEN = request.cookies.get("TOKEN"); | ||
if (TOKEN == undefined) { | ||
return REDIRECT_TO_LOGIN; | ||
} | ||
|
||
if (!await isValidToken(TOKEN.value)) { | ||
REDIRECT_TO_LOGIN.cookies.delete("TOKEN"); | ||
return REDIRECT_TO_LOGIN; | ||
} | ||
|
||
return NextResponse.next(); | ||
|
||
} | ||
|
||
export const config = { | ||
matcher: "/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|login|register).*)", | ||
// matcher: [ | ||
// "/matching", | ||
// "/", | ||
// "/profile", | ||
// "/question", | ||
// "/question/.*", | ||
// ], | ||
} | ||
|