-
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
1 parent
293679d
commit 6cbdf29
Showing
6 changed files
with
127 additions
and
1 deletion.
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
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,48 @@ | ||
import styles from "@/styles/Login.module.css"; | ||
import { useState } from "react"; | ||
import { useRouter } from "next/router"; | ||
import axios from "axios"; | ||
|
||
const Login = () => { | ||
const [username, setUsername] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
const [error, setError] = useState(false); | ||
const router = useRouter(); | ||
|
||
const handleClick = async () => { | ||
try { | ||
await axios.post("http://localhost:3000/api/login", { | ||
username, | ||
password, | ||
}); | ||
router.push("/admin"); | ||
} catch (err) { | ||
setError(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<div className={styles.wrapper}> | ||
<h1>Admin Dashboard</h1> | ||
<input | ||
placeholder="username" | ||
className={styles.input} | ||
onChange={(e) => setUsername(e.target.value)} | ||
/> | ||
<input | ||
placeholder="password" | ||
type="password" | ||
className={styles.input} | ||
onChange={(e) => setPassword(e.target.value)} | ||
/> | ||
<button onClick={handleClick} className={styles.button}> | ||
Sign in | ||
</button> | ||
{error && <span className={styles.error}>Invalid credentials!</span>} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Login; |
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,25 @@ | ||
import cookie from "cookie"; | ||
|
||
const handler = async (req, res) => { | ||
if (req.method === "POST") { | ||
const { username, password } = req.body; | ||
if ( | ||
username === process.env.ADMIN_USERNAME && | ||
password === process.env.ADMIN_PASSWORD | ||
) { | ||
res.setHeader( | ||
"Set-Cookie", | ||
cookie.serialize("token", process.env.TOKEN, { | ||
maxAge: 60 * 60, | ||
sameSite: "strict", | ||
path: "/", | ||
}) | ||
); | ||
res.status(200).json("Successful"); | ||
} else { | ||
res.status(400).json("Invalid credentials"); | ||
} | ||
} | ||
}; | ||
|
||
export default handler; |
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,32 @@ | ||
.container { | ||
height: calc(100vh - 100px); | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.wrapper { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.input { | ||
height: 40px; | ||
margin-bottom: 20px; | ||
padding: 0 10px; | ||
} | ||
|
||
.button { | ||
height: 40px; | ||
margin-bottom: 20px; | ||
border: none; | ||
background-color: teal; | ||
color: white; | ||
font-weight: 600; | ||
cursor: pointer; | ||
} | ||
|
||
.error { | ||
color: red; | ||
font-size: 13px; | ||
} |