Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

06/delete posts rebased #58

Open
wants to merge 51 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
3c5d5b4
input placeholders added, non functional
abodian Feb 8, 2023
77db070
added post button
MartaBia Feb 8, 2023
bef723d
added post text box
MartaBia Feb 8, 2023
e8a68c5
removed comments before pull request
MartaBia Feb 8, 2023
e266e84
token generator tests failing
abodian Feb 8, 2023
0893c2e
first and last name added and tests passing
abodian Feb 8, 2023
047cea6
added defaultValue to allow typing on text box
MartaBia Feb 8, 2023
47e5850
navbar ltd functionality
UserChi Feb 8, 2023
732a0ba
Merge pull request #1 from abodian/03/user-can-post
MartaBia Feb 8, 2023
f525c03
error message shows when login credentials incorrect
abodian Feb 8, 2023
6521097
made the navbar folder clearer
UserChi Feb 8, 2023
6964ada
Merge pull request #3 from abodian/05/login-page
UserChi Feb 9, 2023
f1b6306
merge resolution
sirdavy Feb 9, 2023
fdd46d1
Merge pull request #2 from abodian/01/sign-up-page
sirdavy Feb 9, 2023
18714ea
Merge branch 'main' into 02/navbar-non_functuality
sirdavy Feb 9, 2023
0f232fe
Merge pull request #4 from abodian/02/navbar-non_functuality
sirdavy Feb 9, 2023
520cd80
small conflict on sign up page resolved
sirdavy Feb 9, 2023
470d096
Merge pull request #5 from abodian/navbar2
MartaBia Feb 9, 2023
fcae856
added function to post text box/button and added nav bar
sirdavy Feb 9, 2023
c85e93c
Merge pull request #6 from abodian/4-add-functionality-to-post
sirdavy Feb 9, 2023
e889d71
login page fixed
IAmNotAbbas Feb 9, 2023
9dea689
Testing for posting
sirdavy Feb 9, 2023
1d0a3d1
Merge pull request #7 from abodian/fix_login
sirdavy Feb 9, 2023
1393486
Update making_a_post.cy.js
MartaBia Feb 9, 2023
44aa864
Merge pull request #8 from abodian/testingposting
MartaBia Feb 9, 2023
d5d067d
one extra test line
sirdavy Feb 9, 2023
2e6d054
Merge pull request #9 from abodian/testingposting2
UserChi Feb 9, 2023
2c837d8
cypress making post e2e test passes
abodian Feb 9, 2023
2de535c
non functional like and comment button added, timeline sits in center…
abodian Feb 9, 2023
da1c320
added text wrap to .postContent and improved overall look
abodian Feb 9, 2023
e58a989
Merge pull request #10 from abodian/fix-failing-cypress-post-tests
JamesJPaterson Feb 10, 2023
4979655
Merge pull request #11 from abodian/07/reformat-feed
sirdavy Feb 10, 2023
5b2a25a
username stored in database but only after manual input, shows on post
abodian Feb 10, 2023
7307cf2
user email stored in localStorage
abodian Feb 10, 2023
42651bf
get route added to users route file
abodian Feb 10, 2023
b765c54
user controller find method created and tested
abodian Feb 10, 2023
ef2a6d5
user first and last name now accessible in local storage
abodian Feb 10, 2023
285a5e4
user name is stored in the post document when submitted
abodian Feb 10, 2023
131ce40
username shows on posts
abodian Feb 13, 2023
5825156
ready for review
abodian Feb 13, 2023
5ebfab5
Merge branch 'main' into 06/add-username-jwt-method
abodian Feb 13, 2023
64491b3
Merge pull request #12 from abodian/06/add-username-jwt-method
abodian Feb 13, 2023
44c6fc6
minor changes
abodian Feb 13, 2023
0cc1e32
Added logic and route to delete a post
MartaBia Feb 9, 2023
0b7d7e3
updated route
MartaBia Feb 9, 2023
30b880e
added delete button with functionality
MartaBia Feb 9, 2023
b625b7f
refactored
MartaBia Feb 10, 2023
8afd8ef
Added cypress test
MartaBia Feb 13, 2023
8efdd22
post rebase commit
abodian Feb 13, 2023
d24b37a
merged with previous code
MartaBia Feb 13, 2023
00b4972
articles removed from post model
abodian Feb 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'plugin:react/recommended',
'xo',
],
overrides: [
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: [
'react',
],
rules: {
},
};
18 changes: 9 additions & 9 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ const usersRouter = require("./routes/users");
const app = express();

// setup for receiving JSON
app.use(express.json())
app.use(express.json());

app.use(logger("dev"));
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));

// middleware function to check for valid tokens
const tokenChecker = (req, res, next) => {

let token;
const authHeader = req.get("Authorization")
const authHeader = req.get("Authorization");

if(authHeader) {
token = authHeader.slice(7)
if (authHeader) {
token = authHeader.slice(7);
}
console.log("Test token checker" + token);

JWT.verify(token, process.env.JWT_SECRET, (err, payload) => {
if(err) {
console.log(err)
res.status(401).json({message: "auth error"});
if (err) {
console.log(err);
res.status(401).json({ message: "auth error" });
} else {
req.user_id = payload.user_id;
next();
Expand All @@ -55,7 +55,7 @@ app.use((err, req, res) => {
res.locals.error = req.app.get("env") === "development" ? err : {};

// respond with details of the error
res.status(err.status || 500).json({message: 'server error'})
res.status(err.status || 500).json({ message: "server error" });
});

module.exports = app;
20 changes: 17 additions & 3 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
const Post = require("../models/post");
const TokenGenerator = require("../models/token_generator");
const { post } = require("../routes/posts");

const PostsController = {
Index: (req, res) => {
Post.find(async (err, posts) => {
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id)
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(200).json({ posts: posts, token: token });
});
},
Create: (req, res) => {
const post = new Post(req.body);
console.log("Post Content: " + post);
post.save(async (err) => {
if (err) {
throw err;
}

const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(201).json({ message: 'OK', token: token });
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(201).json({ message: "OK", token: token });
});
},
Delete: (req, res) => {
const postId = req.params.id;
Post.deleteOne({ _id: postId }, async (err) => {
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res
.status(200)
.json({ message: "Post deleted successfully", token: token });
});
},
};
Expand Down
11 changes: 5 additions & 6 deletions api/controllers/tokens.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
const User = require("../models/user");
const TokenGenerator = require("../models/token_generator")
const TokenGenerator = require("../models/token_generator");

const SessionsController = {

Create: (req, res) => {
const email = req.body.email;
const password = req.body.password;

User.findOne({ email: email }).then(async (user) => {
if (!user) {
console.log("auth error: user not found")
console.log("auth error: user not found");
res.status(401).json({ message: "auth error" });
} else if (user.password !== password) {
console.log("auth error: passwords do not match")
console.log("auth error: passwords do not match");
res.status(401).json({ message: "auth error" });
} else {
const token = await TokenGenerator.jsonwebtoken(user.id)
const token = await TokenGenerator.jsonwebtoken(user.id);
res.status(201).json({ token: token, message: "OK" });
}
});
}
},
};

module.exports = SessionsController;
17 changes: 15 additions & 2 deletions api/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ const UsersController = {
const user = new User(req.body);
user.save((err) => {
if (err) {
res.status(400).json({message: 'Bad request'})
res.status(400).json({ message: "Bad request" });
} else {
res.status(201).json({ message: 'OK' });
res.status(201).json({ message: "OK" });
}
});
},

Find: (req, res) => {
const email = req.query.email;
User.findOne({ email: email }, "-password", (err, user) => {
if (err) {
res.status(500).json({ message: "Error finding the user" });
} else if (!user) {
res.status(404).json({ message: "User not found" });
} else {
res.status(200).json({ user });
}
});
},
Expand Down
3 changes: 2 additions & 1 deletion api/models/post.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const mongoose = require("mongoose");

const PostSchema = new mongoose.Schema({
message: String
message: String,
userName: String,
});

const Post = mongoose.model("Post", PostSchema);
Expand Down
17 changes: 10 additions & 7 deletions api/models/token_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ const secret = process.env.JWT_SECRET;

class TokenGenerator {
static jsonwebtoken(user_id) {
return JWT.sign({
user_id: user_id,
iat: Math.floor(Date.now() / 1000),

// Set the JWT token to expire in 10 minutes
exp: Math.floor(Date.now() / 1000) + (10 * 60)
}, secret);
return JWT.sign(
{
user_id: user_id,
iat: Math.floor(Date.now() / 1000),

// Set the JWT token to expire in 10 minutes
exp: Math.floor(Date.now() / 1000) + 10 * 60,
},
secret
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions api/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
email: { type: String, required: true },
password: { type: String, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
});

const User = mongoose.model("User", UserSchema);
Expand Down
1 change: 1 addition & 0 deletions api/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const PostsController = require("../controllers/posts");

router.get("/", PostsController.Index);
router.post("/", PostsController.Create);
router.delete("/:id", PostsController.Delete);

module.exports = router;
1 change: 1 addition & 0 deletions api/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ const router = express.Router();
const UsersController = require("../controllers/users");

router.post("/", UsersController.Create);
router.get("/", UsersController.Find);

module.exports = router;
Loading