Skip to content

Commit

Permalink
Passport JS push
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-0602 committed Jul 26, 2023
1 parent 4e18adc commit b90f557
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
37 changes: 35 additions & 2 deletions BackEnd/concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,39 @@ Know more about mongoose: [OFFCIAL DOCUMENTATION](https://mongoosejs.com/docs/in

The `versionKey` is a property set on each document when first created by Mongoose. This keys value contains the internal [revision](http://aaronheckmann.blogspot.com/2012/06/mongoose-v3-part-1-versioning.html) of the document. The `versionKey` option is a string that represents the path to use for versioning. The default is `__v`.

## Some Backend Code Concepts in Express:
## Some Backend Code Concepts in Express:

How to use the req Object in Express: [Here](https://www.digitalocean.com/community/tutorials/nodejs-req-object-in-expressjs)
#### How to use the req Object in Express: [Here](https://www.digitalocean.com/community/tutorials/nodejs-req-object-in-expressjs)

#### Serialize and deserialize in passport js


To maintain a login session, Passport serializes and deserializes user information to and from the session. The information that is stored is determined by the application, which supplies a `serializeUser` and a `deserializeUser` function.

```
passport.serializeUser(function(user, cb) {
process.nextTick(function() {
return cb(null, {
id: user.id,
username: user.username,
picture: user.picture
});
});
});
passport.deserializeUser(function(user, cb) {
process.nextTick(function() {
return cb(null, user);
});
});
```

A login session is established upon a user successfully authenticating using a credential. The following route will authenticate a user using a username and password. If successfully verified, Passport will call the `serializeUser` function, which in the above example is storing the user's ID, username, and picture. Any other properties of the user, such as an address or birthday, are not stored.

```
app.post('/login/password',
passport.authenticate('local', { failureRedirect: '/login', failureMessage: true }),
function(req, res) {
res.redirect('/~' + req.user.username);
});
```
51 changes: 51 additions & 0 deletions ConnectIO/config/passport-local-strategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const passport = require("passport");

const LocalStrategy = require("passport-local").Strategy;
const User = require('../models/user');

//authentication using passport:
passport.use(new LocalStrategy({
usernameField: 'email'
},


//here done is a callback function.
function(email,password,done){
//find user and establish identity:
User.findOne({email:email}, function(err,user){
if(err){
console.log("Error in finding user --> Passport");
return done(err);
}

//if user does not exist or password incorrect:
if(!user || user.password!= password){
console.log("Invalid Credentials!!");
return done(err,false); //done takes 2 arguments.
}

return done(null,user); //if user is found then return done().
});
}


));

//serializing user to decide which key is to be kept in the cookies

passport.serializeUser(function(user,done){
done(null,user.id); //this encrypts the id.
});

//deserializing user from thr key in the cookies

passport.deserializeUser(function(id,done){
User.findById(id, function(err,user){
if(err){
console.log("Error in finding user --> Passport");
return done(err);
}

return done(null,user);
})
})

0 comments on commit b90f557

Please sign in to comment.