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

Feed update + profile pages #43

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
db11b44
folders setup and basic homepage added
JudeA94 Jan 10, 2023
783fe03
home component tests complete
JudeA94 Jan 10, 2023
ad82afb
home component tests improved
JudeA94 Jan 11, 2023
214957c
adds v basic navbar with only two links and no styling
aimeehorwood Jan 11, 2023
53c7d3b
adds more links and very basic styling
aimeehorwood Jan 11, 2023
fcfa6c5
further tweaks to nav bar text
aimeehorwood Jan 11, 2023
a8c3892
bcrypt working, some tests now failing
JudeA94 Jan 11, 2023
5b21788
Adds functioning navbar depending on whether the user is logged in or…
aimeehorwood Jan 12, 2023
5a0a980
Adds functioning nav-bar depending on whether user logged in or not
aimeehorwood Jan 12, 2023
6908b18
wider test coverage
JudeA94 Jan 12, 2023
7470ce1
more info added to schemas and models updated (likes, friends, about me)
JudeA94 Jan 12, 2023
2870574
more sign up info
JudeA94 Jan 13, 2023
beb7265
this is the new post bit
ashaw0192 Jan 13, 2023
aa0380a
Merge pull request #2 from JudeA94/nav-bar
miikehallcodes Jan 13, 2023
f60b552
test videos deleted
JudeA94 Jan 13, 2023
257f599
saved
ashaw0192 Jan 13, 2023
857c1ff
Merge pull request #4 from JudeA94/login-signup
miikehallcodes Jan 13, 2023
0f65693
Merge branch 'main' into new-post
miikehallcodes Jan 13, 2023
ea1e959
Merge pull request #5 from JudeA94/new-post
miikehallcodes Jan 13, 2023
aa07863
remove id and logout btn
JudeA94 Jan 13, 2023
f3071c4
some new feed features added
JudeA94 Jan 16, 2023
3307721
comments and likes routing
JudeA94 Jan 16, 2023
5aa2ef3
likes working and wider test coverage
JudeA94 Jan 17, 2023
9b01d96
my profile working
JudeA94 Jan 17, 2023
9b595bc
profile page
JudeA94 Jan 17, 2023
43f841b
other users profile page added
JudeA94 Jan 17, 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
Binary file added .DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,3 @@ typings/

# cypress.io
cypress/screenshots
cypress/videos
2 changes: 1 addition & 1 deletion api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ app.use(express.static(path.join(__dirname, "public")));
// middleware function to check for valid tokens
const tokenChecker = (req, res, next) => {

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

Expand All @@ -29,7 +30,6 @@ const tokenChecker = (req, res, next) => {

JWT.verify(token, process.env.JWT_SECRET, (err, payload) => {
if(err) {
console.log(err)
res.status(401).json({message: "auth error"});
} else {
req.user_id = payload.user_id;
Expand Down
28 changes: 27 additions & 1 deletion api/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,37 @@ const PostsController = {
if (err) {
throw err;
}

const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(201).json({ message: 'OK', token: token });
});
},
Update: async (req, res) => {
post_id = req.params.id
value = req.body.value
field = req.body.field
if (field === 'likes') {
const post = await Post.findOne({_id: post_id});
const likes = post.likes.toObject();
post.likes.includes(value) ? beenLiked = true : beenLiked = false
beenLiked ? update = await Post.findOneAndUpdate({_id: post_id},{$pull: {likes: value}} ) : update = await Post.findOneAndUpdate({_id: post_id},{$push: {likes: value}} )
} else if (field === 'comments') {
update = await Post.findOneAndUpdate({_id: post_id},{$push: {comments: value}} )
} else {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(200).json({ message: 'OK', token: token });
},
FindUsersPosts: async (req, res) => {
const id = req.params.id;
const posts = await Post.find({ author: id });
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(200).json({ message: "OK", token: token, posts: posts });
},
};

module.exports = PostsController;




12 changes: 8 additions & 4 deletions api/controllers/tokens.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
const User = require("../models/user");
const TokenGenerator = require("../models/token_generator")
const bcrypt = require('bcrypt')

const SessionsController = {
const TokensController = {

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")
res.status(401).json({ message: "auth error" });
} else if (user.password !== password) {
} else if (!await bcrypt.compare(password, user.password)) {
console.log("auth error: passwords do not match")
res.status(401).json({ message: "auth error" });
} else {
const token = await TokenGenerator.jsonwebtoken(user.id)
res.status(201).json({ token: token, message: "OK" });
res.status(201).json({ token: token, user: user, message: "OK" });
}
});
}
};

module.exports = SessionsController;
module.exports = TokensController;
51 changes: 38 additions & 13 deletions api/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
const TokenGenerator = require("../models/token_generator");
const User = require("../models/user");

const UsersController = {
Create: (req, res) => {
const user = new User(req.body);
user.save((err) => {
if (err) {
res.status(400).json({message: 'Bad request'})
} else {
res.status(201).json({ message: 'OK' });
}
});
},
};
const createToken = (_id) => {
return jwt.sign({_id}, process.env.SECRET, { expiresIn: '3d' })
}
const signupUser = async (req, res) => {
const {name, email, password, aboutMe} = req.body
try {
const user = await User.signup(name, email, password, aboutMe)
res.status(201).json({email})
} catch (error) {
res.status(400).json({error: error.message})
}
}

const loginUser = async (req, res) => {
const {email, password} = req.body
try {
const user = await User.login(email, password)
const token = await TokenGenerator.jsonwebtoken(user.id)
res.status(200).json({ token: token, user: user, message: "OK" })
} catch (error) {
res.status(400).json({error: error.message})
}
}

const findUser = async (req, res) => {
try {
user_id = req.params.id
const user = await User.findById(user_id)
res.status(200).json({user: user})
} catch (error) {
res.status(404).json({error: 'This user no longer exists'})
}
}



module.exports = { signupUser, loginUser, findUser }

module.exports = UsersController;
9 changes: 6 additions & 3 deletions api/models/post.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const mongoose = require("mongoose");

const PostSchema = new mongoose.Schema({
message: String
});
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
message: String,
likes: Array,
comments: Array,
}, { timestamps: true });

const Post = mongoose.model("Post", PostSchema);

module.exports = Post;
module.exports = Post;
55 changes: 54 additions & 1 deletion api/models/user.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
const mongoose = require("mongoose");
const bcrypt = require('bcrypt')
const validator = require('validator')


const UserSchema = new mongoose.Schema({
email: { type: String, required: true },
name: { type: String},
email: { type: String, required: true, unique: true},
password: { type: String, required: true },
aboutMe: {type: String},
friendRequests: { type: Array},
friends: { type: Array }
});


UserSchema.statics.signup = async function(name, email, password, aboutMe) {

// validation
if (!email || !password) {
throw Error('All fields must be filled')
}
if (!validator.isEmail(email)) {
throw Error('Email not valid')
}
if (!validator.isStrongPassword(password)) {
throw Error('Password not strong enough')
}

const exists = await this.findOne({ email })

if (exists) {
throw Error('Email already in use')
}

const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash(password, salt)
const user = await this.create({ name, aboutMe, email, password: hash })
return user
}


UserSchema.statics.login = async function(email, password) {

if (!email || !password) {
throw Error('All fields must be filled')
}

const user = await this.findOne({ email })
if (!user) {
throw Error('Incorrect email')
}

const match = await bcrypt.compare(password, user.password)
if (!match) {
throw Error('Incorrect password')
}
return user
}


const User = mongoose.model("User", UserSchema);

module.exports = User;
Loading