Skip to content

Commit

Permalink
Passport JS and express update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-0602 committed Jul 26, 2023
1 parent b0ef2ff commit 62d892f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
32 changes: 2 additions & 30 deletions ConnectIO/controllers/users_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,8 @@ module.exports.create = function(req,res){

module.exports.createSession = function(req,res){

//find user
User.findOne({ email: req.body.email}, function(err,user){
if(err){
console.log("Some error occurred! Specific: ",err);
}

if(user){
//handle the user found
//if the password does not match
if(user.password!=req.body.password){
return res.redirect('back');
}
//create session for user:

//to create a session, set the cookie with user_id.
res.cookie('user_id',user.id);
return res.redirect('/users/profile');

}else{
//handle if user not found
return res.redirect('back');
}
});
//redirect to home page as session is created in passport Js itself.
return res.redirect('/users/profile');

}


module.exports.signOut = function(req,res){
//delete the cookie and send the response status as 200:
res.clearCookie('user_id'); //this clears the cookie.
console.log("Logged out successfully!");
res.redirect('/users/sign-in');
}
28 changes: 22 additions & 6 deletions ConnectIO/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const port = 8000; //when deployed on live server, it is deployed on port 80.
const expressLayouts = require('express-ejs-layouts');
const db = require('./config/mongoose');

//used for session cookie and auth
const session = require('express-session');
const passport = require('passport');
const passportLocal = require('./config/passport-local-strategy.js');



app.use(express.urlencoded());
Expand All @@ -19,17 +24,28 @@ app.set("layout extractScripts", true)



//use express router:

app.use('/',require('./routes'));

//set up the view engine.
app.set('view engine','ejs');
app.set('views','./views'); //add the views path.

app.get("/", (req, res) => {
res.send("Hello World!");
});
app.use(session({
name: 'ConnectIO',
secret: 'something',
saveUninitialized: false,
resave: false,
cookie: {
maxAge: (1000 * 60 * 100)
}
}));

app.use(passport.initialize());
app.use(passport.session())

//use express router:

app.use('/',require('./routes'));



app.listen(port,function(err){
Expand Down
6 changes: 4 additions & 2 deletions ConnectIO/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');
const router = express.Router();
const usersController = require('../controllers/users_controller');
const passport = require('passport');

//this router is used for user profile:
router.get('/profile',usersController.profile);
Expand All @@ -11,9 +12,10 @@ router.get('/sign-in',usersController.signIn);

//these routes will create a user and create a session after login.
router.post('/create',usersController.create);
router.post('/create-session',usersController.createSession);

router.post('/sign-out',usersController.signOut);
//to auth, use passport as middleware:
router.post('/create-session', passport.authenticate('local',{failureRedirect : '/users/sign-in'},), usersController.createSession);



module.exports = router;

0 comments on commit 62d892f

Please sign in to comment.