How to use Google Authentication App for authentication in Node Js

Posted By : Rajat khurana | 25-Jan-2018

The most important factor of each and every project is security. How much project is secured and what security system will be applied to it.
For security purpose, there is 2factor authentication which is followed by each and every cryptocurrency project to make it secure so that no one hack data. For general purpose, we use a robot, for checking existing of a user so that no one can try with the automated script to login into a user account. But now a days 2factor authentication is most popular. What basically 2factor authentication is?

 

It is authentication via phone no or code generated by google Authenticator app. For phone no like paytm which use authentication system. For Google Authentication, user needs to scan a barcode from Google Authenticator app and its user specific. On scanning barcode, a code is generated d on every 30 sec. Based on this generated code, a user will login to any website. Now how we will apply it?


In Node js, there are module "speakeasy" and "QRCode", by using this module you will apply authentication on any website.
From QRcode module you will generate QR code which is based on the unique id of the user, that unique id may be a username or email anything depending on your project.

   var speakeasy = require('speakeasy');
    var secret = speakeasy.generateSecret({name:"XXXXXX",issuer:"XXXXXXX"});
var url = secret.base32+"&issuer="+"User UniqueId";
 QRCode.toDataURL(url, function(err, data_url) {
                              if(err) {
                                 console.log("\nError:-",err);
                                 callback(err,null)
                              }
var verified = speakeasy.totp.verify({ secret: secret.base32,
                  encoding: 'base32',
                  token: googleToken,
                  window: 10,

               });

Here from the scret key, you will get three different parameters, you will use base32 parameter and pass that parameter by combining with unique user identity in QRCode method, this will generate a QR code which will be scan by Google Authenticator app.

 

When you will scan it then code generation step will take place on this app and it regenerates token or code in every 30 sec. NOw by taking code input from a user, you can verify it using speakeasy verify method. Once verified user will be successful for login.

About Author

Author Image
Rajat khurana

Rajat is a bright Javascript Developer, he has good knowledge of HTML, WordPress, NodeJs, Core Java, Javascript, Jquery. Apart from this he loves to play Badminton and PC Gaming.

Request for Proposal

Name is required

Comment is required

Sending message..