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

New post #36

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const PostsController = {
},
Create: (req, res) => {
const post = new Post(req.body);
console.log(req.body);
post.save(async (err) => {
if (err) {
throw err;
Expand Down
26 changes: 22 additions & 4 deletions api/controllers/tokens.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
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;


// const loginUser = async (req, res) => {
// const {email, password} = req.body
// try {
// const user = await User.login(email, password)
// //create token here and add it to response
// console.log(user)
// 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})
// }
// }
module.exports = TokensController;
43 changes: 30 additions & 13 deletions api/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
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
console.log(email)
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)
//create token here and add it to response
console.log(user)
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})
}
}


module.exports = { signupUser, loginUser }

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
});
message: String,
author: { type: mongoose.Schema.Types.ObjectId, ref: 'user' },
likes: Array,
comments: Array,
}, { timestamps: true });

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

module.exports = Post;
module.exports = Post;
57 changes: 56 additions & 1 deletion api/models/user.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
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 })
console.log(user)
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')
}
console.log(user)
return user
}


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

module.exports = User;
Loading