-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tanmay4l
authored and
tanmay4l
committed
Jul 14, 2024
1 parent
984cb3f
commit 2c0e3d4
Showing
10 changed files
with
178 additions
and
21 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// middleware/auth.ts | ||
|
||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { NextHandler } from "next-connect"; | ||
|
||
export function isAuthenticated( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
next: NextHandler | ||
) { | ||
const session = req.cookies.session; | ||
|
||
if (session === "admin") { | ||
next(); | ||
} else { | ||
res.status(401).json({ message: "Unauthorized" }); | ||
} | ||
} |
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,18 @@ | ||
// utils/auth.ts | ||
|
||
import { useEffect } from "react"; | ||
import { useRouter } from "next/router"; | ||
|
||
export function useAuth() { | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
async function checkAuth() { | ||
const response = await fetch("/api/auth/check"); | ||
if (!response.ok) { | ||
router.push("/admin"); | ||
} | ||
} | ||
checkAuth(); | ||
}, [router]); | ||
} |
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,15 @@ | ||
// pages/api/auth/check.ts | ||
|
||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { isAuthenticated } from "../../admin/ middleware/auth"; | ||
|
||
export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method === "GET") { | ||
isAuthenticated(req, res, () => { | ||
res.status(200).json({ message: "Authenticated" }); | ||
}); | ||
} else { | ||
res.setHeader("Allow", ["GET"]); | ||
res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
} |
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 @@ | ||
// pages/api/auth/login.ts | ||
|
||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method === "POST") { | ||
const { username, password } = req.body; | ||
|
||
// In a real application, you would check these credentials against a database | ||
if (username === "admin" && password === "securepassword") { | ||
// Set a secure, HTTP-only cookie | ||
res.setHeader( | ||
"Set-Cookie", | ||
"session=admin; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=3600" | ||
); | ||
res.status(200).json({ message: "Logged in successfully" }); | ||
} else { | ||
res.status(401).json({ message: "Invalid credentials" }); | ||
} | ||
} else { | ||
res.setHeader("Allow", ["POST"]); | ||
res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
} |
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