Skip to content
Merged
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
5 changes: 5 additions & 0 deletions Full Stack Projects/Blog-Application/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.env
package-lock.json


25 changes: 25 additions & 0 deletions Full Stack Projects/Blog-Application/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "blog",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node src/app.js",
"devStart": "nodemon src/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cookie-parser": "^1.4.6",
"dotenv": "^16.3.1",
"ejs": "^3.1.9",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"mongoose": "^7.3.1",
"multer": "^1.4.5-lts.1"
},
"devDependencies": {
"nodemon": "^2.0.22"
}
}
41 changes: 41 additions & 0 deletions Full Stack Projects/Blog-Application/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require('dotenv').config();
const express = require('express');
const path = require('path');
const app = express();
const PORT = 3000;
const mongoose = require('mongoose');
const userRouter = require('./routes/user.js');
const blogRouter = require('./routes/blog.js');
const cookieParser = require('cookie-parser');
const checkForAuthenticationCookie = require('./middleware/authentication.js');
const Blog = require('./models/blog.js');

app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

mongoose.connect('mongodb://127.0.0.1:27017/Blogify').then(() => {
console.log('connected to db');
});

app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(express.urlencoded({ extended: false })); // for parsing application/x-www-form-urlencoded
app.use(cookieParser());
app.use(checkForAuthenticationCookie('token'));
app.use(express.static(path.join(__dirname, './public')));

app.use('/user', userRouter);
app.use('/blog', blogRouter);

app.get('/', async (req, res) => {
const allBlogs = await Blog.find({});
res.render('home', {
user: req.user,
blogs: allBlogs,

});
});


Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const User = require('../../models/user.js');

const login = async (req, res) => {
const { email, password } = req.body;
try {
const token = await User.matchPasswordAndGenerateToken(email, password);
return res.cookie('token', token).redirect('/');
} catch (err) {
res.render('signIn', { error: err.message });
}
};

module.exports = login;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

const logout = (req,res)=>{
res.clearCookie('token').redirect('/')
}

module.exports = logout;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const User = require('../../models/user')

const register = async(req,res)=>{
const {fullName,email,password} = req.body;
await User.create({
fullName,
email,
password
});
return res.redirect('/user/signin')
}

module.exports = register;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const signIn = (req,res)=>{
return res.render('signIn')
}

module.exports = signIn;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

const signUp = (req,res)=>{
return res.render('signUp')
}

module.exports = signUp;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const multer = require('multer');
const path = require('path');

const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.resolve(`./src/public/uploads/`));
},
filename: function (req, file, cb) {
const fileName = `${Date.now()}-${file.originalname}`;
cb(null, fileName);
},
});

module.exports = storage;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

const addBlog = (req,res)=>{
return res.render('addBlog',{
user: req.user
})
}

module.exports = addBlog;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Blog = require('../../models/blog');
const Comment = require('../../models/comment');

const blogId = async (req, res) => {
const blog = await Blog.findById(req.params.id).populate('CreatedBy');
const comments = await Comment.find({ blogId: req.params.id }).populate(
'createdBy'
);
return res.render('blog', {
user: req.user,
blog,
comments,
});
};

module.exports = blogId;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Comment = require('../../models/comment');

const cmt = async (req, res) => {
await Comment.create({
content: req.body.content,
blogId: req.params.blogId,
createdBy: req.user._id,
});
return res.redirect(`/blog/${req.params.blogId}`);
};
module.exports = cmt;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Blog = require('../../models/blog');

const postBlog = async (req, res) => {
const { title, body } = req.body;
const blog = await Blog.create({
body,
title,
CreatedBy: req.user._id,
coverImageUrl: `/uploads/${req.file.filename}`,
});
return res.redirect(`/blog/${blog._id}`);
};
module.exports = postBlog;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { validateToken } = require("../services/authentication");

function checkForAuthenticationCookie(cookieName){
return (req,res,next) =>{
const tokenCookieValue = req.cookies[cookieName];
if(!tokenCookieValue) {
return next();
}
try{
const UserPaload = validateToken(tokenCookieValue);
req.user = UserPaload
}
catch(err){};
return next();
}
}

module.exports = checkForAuthenticationCookie;
26 changes: 26 additions & 0 deletions Full Stack Projects/Blog-Application/src/models/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { Schema, model } = require('mongoose');

const blogSchema = new Schema(
{
title: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
coverImageUrl: {
type: String,
required: false,
},
CreatedBy: {
type: Schema.Types.ObjectId,
ref: 'user',
},
},
{ timestamps: true }
);

const Blog = model('blog', blogSchema);
module.exports = Blog;
22 changes: 22 additions & 0 deletions Full Stack Projects/Blog-Application/src/models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { Schema, model } = require('mongoose');

const commentSchema = new Schema(
{
content: {
type: String,
required: true,
},
blogId: {
type: Schema.Types.ObjectId,
ref: 'blog',
},
createdBy: {
type: Schema.Types.ObjectId,
ref: 'user',
},
},
{ timestamps: true }
);

const Comment = model('comment', commentSchema);
module.exports = Comment;
73 changes: 73 additions & 0 deletions Full Stack Projects/Blog-Application/src/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const { Schema, model } = require('mongoose');
const { createHmac, randomBytes } = require('crypto');
const { createTokenForUser } = require('../services/authentication');

const userSchema = new Schema(
{
fullName: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
salt: {
type: String,
},
password: {
type: String,
required: true,
},
profileImageUrl: {
type: String,
default: '/images/default.png',
},
role: {
type: String,
emun: ['ADMIN', 'USER'],
default: 'USER',
},
},
{ timestamps: true }
);

userSchema.pre('save', function (next) {
// Perform some logic or modifications before saving the document
const user = this; // this refers to the document being saved
if (!user.isModified('password')) return;

const salt = randomBytes(16).toString();
const hashedPassword = createHmac('sha256', salt)
.update(user.password) // .update takes the data to be hashed
.digest('hex');

this.salt = salt;
this.password = hashedPassword;

next();
});

userSchema.static('matchPasswordAndGenerateToken', async function (email, password) {
const user = await this.findOne({ email });
if (!user) throw new Error('User not found');

const salt = user.salt;
const hashedPassword = user.password;

const userProvidedHash = createHmac('sha256', salt)
.update(password)
.digest('hex');

if (hashedPassword !== userProvidedHash)
throw new Error('Password is incorrect');

const token = createTokenForUser(user); // passing user object to create token
return token;
// return user;
// return { ...user, password: undefined, salt: undefined };
});

const USER = model('user', userSchema);
module.exports = USER;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions Full Stack Projects/Blog-Application/src/routes/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { Router } = require('express');
const router = Router();
const addBlog = require('../controllers/blogController/blog');
const postBlog = require('../controllers/blogController/postBlog');
const multer = require('multer');
const path = require('path');
const storage = require('../controllers/authController/storage');
const cmt = require('../controllers/blogController/comment.js');
const blogId = require('../controllers/blogController/blogId.js');


router.get('/add-new', addBlog);
router.get('/:id', blogId);
const upload = multer({ storage: storage });
router.post('/', upload.single('coverImage'), postBlog);
router.post('/comment/:blogId', cmt);

module.exports = router;
16 changes: 16 additions & 0 deletions Full Stack Projects/Blog-Application/src/routes/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { Router } = require('express');
const signIn = require('../controllers/authController/signIn');
const signUp = require('../controllers/authController/signUp');
const register = require('../controllers/authController/register');
const login = require('../controllers/authController/login.js');
const logout = require('../controllers/authController/logout');

const router = Router();

router.get('/signin', signIn);
router.get('/signup', signUp);
router.post('/signup', register);
router.post('/signin', login);
router.get('/logout', logout);
router.get('/blog');
module.exports = router;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require('dotenv').config()
const JWT = require('jsonwebtoken');
const secret = process.env.TOKEN_SECRET;

function createTokenForUser(user) {
const payLoad = {
_id: user._id,
email: user.email,
profileImageUrl: user.profileImageUrl,
role: user.role,
};
const token = JWT.sign(payLoad,secret); // this is the token
return token;
}

function validateToken(token) {
const payLoad = JWT.verify(token, secret);
return payLoad;
}

module.exports = { createTokenForUser, validateToken };
Loading