Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/node_modules

.env
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const app=express();
const complains=require('./routes/Complains');
const loged=require('./routes/login');
const Complain=require('./models/complain');
const checkAuth=require('./middleware/auth');

const { checkAuth, checkUser } =require('./middleware/auth');
//built in middleware for serving static files
app.use(cookieParser());
app.use(express.static('public'));
Expand All @@ -20,6 +20,8 @@ const { use } = require('./routes/Complains');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());

app.get('*', checkUser);
app.use('/complains',complains);


Expand Down Expand Up @@ -96,4 +98,4 @@ app.get('/signup', (req, res) => {

const port =process.env.PORT || 3000;
//asynchronous function handles wih callback
app.listen(port,()=>console.log(`Listening to port ${port}...`));
app.listen(port,()=>console.log(`Listening to port ${port}...`));
38 changes: 34 additions & 4 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const jwt=require('jsonwebtoken');
var cookieParser = require('cookie-parser')
require('dotenv').config()
const User = require('../models/Users');

module.exports=(req,res,next)=>{
const checkAuth=(req,res,next)=>{
try{
// console.log('hello');
console.log(req.cookies.token);
const token = req.cookies.token;
console.log(token);
// console.log(process.env.JWT_KEY);
jwt.verify(req.cookies.token, process.env.JWT_KEY,(err,decoded)=>{
jwt.verify(token, process.env.JWT_KEY,(err,decoded)=>{
if(err)
{ console.log("Not Authorized");
// res.status(500).json({err:"Not Authorized"})
Expand All @@ -31,4 +33,32 @@ module.exports=(req,res,next)=>{

}

}
}

const checkUser = (req, res, next) => {
const token = req.cookies.token;

// verification of token
if(token) {
jwt.verify(token, JWT_KEY, async (err, decodedToken) => {
if(err) {
console.log(err.message);
res.locals.user = null;
next();
} else {
//console.log(decodedToken);
let user = await User.findById(decodedToken.id);
res.locals.user = user;
next();
}
});
} else {
res.locals.user = null;
next();
}
}

module.exports = {
checkAuth,
checkUser
}
1 change: 0 additions & 1 deletion models/complain.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ const Complain=mongoose.model('complain',new mongoose.Schema({

}));

// exports.Complain=Complain;
module.exports=Complain;
5 changes: 5 additions & 0 deletions routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ router.delete('/:userId',(req,res,next)=>{
});
});

router.get((req, res, next) => {
// replace existing cookie with fast expiring cookie
res.cookie('token', '', { maxAge: 1 });
res.redirect('/');
});

// router.post('/login', (req, res) => {
// const email = req.body.email;
Expand Down
6 changes: 6 additions & 0 deletions views/complaintForm.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
</head>

<body>
<header class="header">
<%- include('partials/header.ejs') %>
</header>

<section class="form">
<div class="container">
<div style="justify-content: center;" class="row">
Expand Down Expand Up @@ -71,6 +75,8 @@
</div>
</section>

<%- include('partials/footer.ejs') %>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous">
</script>
Expand Down
24 changes: 17 additions & 7 deletions views/partials/header.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@
<li class="nav-item active"><a class="nav-link" href="#footer"><span class="fa fa-address-card fa-lg"></span><b> Contact Us</b></a></li>
</ul>
<span>
<a href="/login">
<span class="fa fa-sign-in"></span> <b>Log In</b>
</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="/signup">
<span class="fa fa-user-plus"></span> <b>Sign Up</b>
</a>
<% if(username) { %>
<a href="/login">
<span class="fa fa-sign-in"></span> Log In
</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="/signup">
<span class="fa fa-user-plus"></span> Sign Up
</a>
<% } else { %>
<a href="/profile">
<span class="fa fa-sign-in"></span> <% username %>
</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="/logout">
<span class="fa fa-user-plus"></span> Log Out
</a>
<% } %>
</span>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions views/sign.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
</head>

<body>


<section class="form">
<div class="container">
<div class="row">
Expand Down Expand Up @@ -72,6 +74,7 @@
</div>
</section>


<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW"
Expand Down