Skip to content

Commit

Permalink
Cookie Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Satyabrat-Ojha committed Jun 24, 2023
1 parent 293679d commit 6cbdf29
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@paypal/react-paypal-js": "^7.8.3",
"@reduxjs/toolkit": "^1.9.5",
"axios": "^1.4.0",
"cookie": "^0.5.0",
"eslint": "8.39.0",
"eslint-config-next": "13.3.4",
"mongodb": "^5.4.0",
Expand Down
13 changes: 12 additions & 1 deletion pages/admin/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ const Index = ({ orders, products }) => {
);
};

export const getServerSideProps = async () => {
export const getServerSideProps = async (ctx) => {
const myCookie = ctx.req?.cookies || "";

if (myCookie.token !== process.env.TOKEN) {
return {
redirect: {
destination: "/admin/login",
permanent: false,
},
};
}

const productRes = await axios.get("http://localhost:3000/api/products");
const orderRes = await axios.get("http://localhost:3000/api/orders");

Expand Down
48 changes: 48 additions & 0 deletions pages/admin/login.jsx
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;
25 changes: 25 additions & 0 deletions pages/api/login.js
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;
32 changes: 32 additions & 0 deletions styles/Login.module.css
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;
}

0 comments on commit 6cbdf29

Please sign in to comment.