Laravel two-factor authentication

Two-factor authentication in Laravel

Two-factor authentication offers a way to add extra protection layer for your website’s user accounts. This allows your users to protect their accounts from unauthorized access. There are many providers that offers such services. In this post, we will use Authy. Authy provides a very robust API for implementing two-factor authentication. You can find API documentation for Authy here.

First you must install composer & create a new laravel application.

Installation

  • Use following command to install:
composer require srmklive/authy
  • Add following to $providers array in config/app.php:
'Srmklive\Authy\Providers\AuthyServiceProvider' // Laravel 5
Srmklive\Authy\Providers\AuthyServiceProvider::class // Laravel 5.1 or greater
  • Add following to $aliases array in config/app.php:
'Authy' => 'Srmklive\Authy\Facades\Authy' // Laravel 5
'Authy' => Srmklive\Authy\Facades\Authy::class // Laravel 5.1 or greater
  • Publish configuration:
php artisan vendor:publish
  • Migrate user table changes to database:
php artisan migrate
  • Add the following lines to User model (e.g App\User.php)
    • Before the class declaration, add these lines:
use Srmklive\Authy\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatable;
use Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatableContract;
  • Now the change the class declaration. For example, if your class declaration is
class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract

to this:

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract,
                                    TwoFactorAuthenticatableContract
  • Now change the import traits line accordingly in user model file. For example if the line is:
use Authenticatable, Authorizable, CanResetPassword;

to

use Authorizable, CanResetPassword, TwoFactorAuthenticatable;
  • Lastly, add/update $hidden variable to hide ‘two_factor_options’ field from any DB call for user detail:
protected $hidden = [
    'two_factor_options'
];

Implementation

You can view further documentation for this plugin here.

I have also implemented it in a sample laravel application as well. You can view installation instructions here. Through this application, you can do:

  • User login & registration.
  • Enable/Disable two-factor authentication for a user.

Following are the download links for the laravel demo application with two-factor authentication:

Install Nodejs from standard binaries in Linux

Install Nodejs from standard binaries in Linux

In this post, i will show you how to install Node.js from standard binaries(https://nodejs.org/) in Linux.

First check your system architecture with following commands:

getconf LONG_BIT
uname -p

On my system, the above two commands gave output 64 and x86_64 respectively. This means i need to install 64-bit version of Node.js.

Now lets download the latest version of Node.js from https://nodejs.org/. The current LTS release is 4.2.2.

wget https://nodejs.org/dist/v4.2.2/node-v4.2.2-linux-x64.tar.gz

After file is downloaded, run the following command to install Node.js

tar -C /usr/local --strip-components 1 -xzf node-v4.2.2-linux-x64.tar.gz

The above command will install node & npm in “/usr/local/bin“. You can check whether its installed by typing following commands:

node -v
npm -v