In node.js authentication and login could be a difficult task but there are some modules available in node that makes it easier for a node developer. One of them is Passport.
Passport is a middleware that allows us to authenticate using username and password called as local strategy. Apart from local strategy, passport has a set of strategies for authentication like facebook, twitter and so on, based on our requirement we can use any of them.
In this particular blog i am going to use local authentication.
First of all i am going to create an application using express generator:
express passportAuthApp
cd passportAuthApp
npm install
Now we will install passport and passport-local Node modules using the below commands:
npm install passport
npm install passport-local
Once installation is done we create a login form, so create a file view/login.html:
<html>
<body>
<form action="/login" method="post">
<div>
<label>Username:</label>
<input type="text" name="username" />
<br/>
</div>
<div>
<label>Password:</label>
<input type="password" name="password" />
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
</body>
</html>
Inside app.js, add the following route:
app.get('/', function(req, res) {
res.render('login');
});
Now open the browser and type http://localhost:3000, you will see a login page.
To handle authentication we need to include passport in our app.js:
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
app.use(passport.initialize());
app.use(passport.session());
Now we need to add routes for login page that we have created.
app.post('/login',
passport.authenticate('local', {
successRedirect: '/home',
failureRedirect: '/'
})
);
app.get('/home', function(req, res, next) {
res.send('Successfull authentication');
});
We also needs to serialize and deserialize the user instance:
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
As we are going to use local database for authentication, we have to connect to Mongo:
for that first we install mongoose with the command below:
npm install mongoose
Now add Mongoose to app.js using the following code.
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/authDatabase');
In order to access data in mongo, we need to have schema and model.
var schema = mongoose.Schema;
var userSchema = new schema({
username: String,
password: String
});
var User = mongoose.model('User', userSchema);
Finally we include our logic to authenticate the user using User model.
passport.use(new localStrategy(function(username, password, done) {
process.nextTick(function() {
User.findOne({
'username': username,
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
if (user.password != password) {
return done(null, false);
}
return done(null, user);
});
});
}));
Now fill the username and password and submit the login form.User will be authenticated and redirected to the corresponding page.
0 Comment(s)