Skip to content

Commit

Permalink
JWT Authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward-J-Xu committed Feb 25, 2023
1 parent 909a18f commit 777e7b3
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 78 deletions.
65 changes: 37 additions & 28 deletions client/src/pages/LoginRegister/Login.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
import "./LoginRegister.css"
import "./LoginRegister.css";
import React, { useState } from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";

function Login() {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
let history = useNavigate();

const login = () => {
const data = { username: username, password: password };
axios.post("http://localhost:3001/auth/login", data).then((response) => {
console.log(response.data);
});
};
return (
<div className="loginContainer">
<label>Username:</label>
<input
type="text"
onChange={(event) => {
setUsername(event.target.value);
}}
/>
<label>Password:</label>
<input
type="password"
onChange={(event) => {
setPassword(event.target.value);
}}
/>
const login = () => {
const data = { username: username, password: password };
axios
.post("http://localhost:3001/auth/login", data)
.then((response) => {
if (response.data.error) {
alert(response.data.error);
} else {
sessionStorage.setItem("accessToken", response.data);
history.push("/");
}
});
};
return (
<div className="loginContainer">
<label>Username:</label>
<input
type="text"
onChange={(event) => {
setUsername(event.target.value);
}}
/>
<label>Password:</label>
<input
type="password"
onChange={(event) => {
setPassword(event.target.value);
}}
/>

<button onClick={login}> Login </button>
</div>
);
<button onClick={login}> Login </button>
</div>
);
}

export default Login;
37 changes: 28 additions & 9 deletions client/src/pages/posts/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,41 @@ function Post() {

const addComment = () => {
axios
.post("http://localhost:3001/comments", {
commentBody: newComment,
postId: id,
})
.post(
"http://localhost:3001/comments",
{
commentBody: newComment,
postId: id,
},
{
headers: {
accessToken: sessionStorage.getItem("accessToken"),
},
}
)
.then((response) => {
const commentToAdd = { commentBody: newComment };
setComments([...comments, commentToAdd]);
setNewComment("");
if (response.data.error) {
console.log(response.data.error);
} else {
const commentToAdd = { commentBody: newComment };
setComments([...comments, commentToAdd]);
setNewComment("");
}
});
};

return (
<div className="postPage">
<div className="navbar1">
<Link to="/games/posts/createpost" style={{ textDecoration: "none" }}><span>Create A Post</span></Link>
<Link to="/games/posts" style={{ textDecoration: "none" }}><span>Posts Home Page</span></Link>
<Link
to="/games/posts/createpost"
style={{ textDecoration: "none" }}
>
<span>Create A Post</span>
</Link>
<Link to="/games/posts" style={{ textDecoration: "none" }}>
<span>Posts Home Page</span>
</Link>
</div>
<div className="leftSide">
<div className="post" id="individual">
Expand Down
4 changes: 2 additions & 2 deletions server/sql.sql → server/createTable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ create table if not exists posts

create table if not exists users
(
id int primary key auto_increment,
username varchar(15) unique not null,
password varchar(60) not null,
primary key (username)
password varchar(60) not null
);

create table if not exists comments
Expand Down
19 changes: 19 additions & 0 deletions server/middlewares/AuthMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { verify } = require("jsonwebtoken");

const validateToken = (req, res, next) => {
const accessToken = req.header("accessToken");

if (!accessToken) return res.json({ error: "User not logged in!" });

try {
const validToken = verify(accessToken, "importantsecret");

if (validToken) {
return next();
}
} catch (err) {
return res.json({ error: err });
}
};

module.exports = { validateToken };
2 changes: 1 addition & 1 deletion server/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ db.pool = pool.promise();
// .then(rev => console.log(rev))
// .catch(err => console.log(err));

let dataSql = fs.readFileSync(path.join(__dirname, "../sql.sql")).toString();
let dataSql = fs.readFileSync(path.join(__dirname, "../createTable.sql")).toString();
dataSql = dataSql.replace(/(\r\n|\n|\r)/gm, "");
dataSql.split(";").forEach((sql) => {
console.log("sql: ", sql)
Expand Down
154 changes: 154 additions & 0 deletions server/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 server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"bcrypt": "^5.1.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"mysql2": "^3.1.2",
"nodemon": "^2.0.20",
"sequelize": "^6.28.2",
Expand Down
Loading

0 comments on commit 777e7b3

Please sign in to comment.