Skip to content

Commit

Permalink
Passport Auth update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-0602 committed Jul 27, 2023
1 parent 62d892f commit 5da43c2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
22 changes: 22 additions & 0 deletions ConnectIO/config/passport-local-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,27 @@ passport.deserializeUser(function(id,done){
})
});

//check if user is authenticated:
passport.checkAuthentication = function(req,res,next){
if(req.isAuthenticated()){
return next(); //if user is signed-in pass user to next page.
}else{
//if user is not signed in:
return res.redirect('/users/sign-in');
}


}

passport.setAuthenticatedUser = function(req,res,next){
if(req.isAuthenticated()){
/*req.user contains the current signed in user form the session
cookie and we are just sending this to the locals for the views.
*/
res.locals.user = req.user;
}

next();
}

module.exports = passport;
24 changes: 4 additions & 20 deletions ConnectIO/controllers/users_controller.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
const User = require('../models/user.js')

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

//check if user id is present in cookies:
if(req.cookies.user_id){
console.log("Session cookie created!!");
User.findById(req.cookies.user_id, function(err,user){
if(user){
return res.render('user_profile',{
title: "User Profile",
user: user
})
}else{
return res.redirect('/users/sign-in');
}
});
}else{
return res.redirect('/users/sign-in');
}


//no need to check for session cookie, as ,it will be authenticated by passport JS.
return res.render('user_profile',{
title: 'User Profile'
});

}


//render the sign up page.
module.exports.signUp= function(req,res){
return res.render('../views/user_sign_up.ejs',{
Expand Down
6 changes: 4 additions & 2 deletions ConnectIO/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ app.set('view engine','ejs');
app.set('views','./views'); //add the views path.

app.use(session({
name: 'ConnectIO',
name: 'user_id',
secret: 'something',
saveUninitialized: false,
resave: false,
Expand All @@ -40,7 +40,9 @@ app.use(session({
}));

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

app.use(passport.setAuthenticatedUser);

//use express router:

Expand Down
2 changes: 1 addition & 1 deletion ConnectIO/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const usersController = require('../controllers/users_controller');
const passport = require('passport');

//this router is used for user profile:
router.get('/profile',usersController.profile);
router.get('/profile',passport.checkAuthentication,usersController.profile);

//these routers is used for user sign-in, sign-up:
router.get('/sign-up',usersController.signUp);
Expand Down

0 comments on commit 5da43c2

Please sign in to comment.