A simple two factor authentication for laravel applications

Overview

Laravel 2fa

A simple two factor authentication for laravel applications.

Total Downloads Latest Stable Version License

Installation

Require this package via composer

To get started with Laravel 2FA, use Composer to add the package to your project's dependencies:

composer require rezkonline/laravel-2fa

Or add this line in your composer.json, inside of the require section:

{
    "require": {
        "rezkonline/laravel-2fa": "^1.1",
    }
}

then run composer install.

Update database with php artisan migrate

After installing the package, you must run php artisan migrate to add the two factor authentication fields to your users table.

It will add the following columns to your database table:

|-------- users --------|
|    two_factor_code    |
| two_factor_expires_at |
|-----------------------|

Replace AuthenticatesUsers trait on LoginController

After that, open your app\Http\Controllers\Auth\LoginController file and replace the AuthenticatesUsers trait with the AuthenticateUsersWithTwoFactor, provided by this package.

Basically, it overrides the authenticated method on AuthenticatesUsers:

trait AuthenticateUsersWithTwoFactor
{
    use AuthenticatesUsers;

    /**
     * The user has been successfully authenticated.
     * @param Request $request
     * @param $user
     */
    public function authenticated(Request $request, $user)
    {
        $user->generateTwoFactorCode();
        $user->notify(new TwoFactorCode());
    }
}

Then, just use the HasTwoFactorAuthentication trait in your User model:



namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    use HasTwoFactorAuthentication;
    ...
}

Publish package config

To publish the package configuration, you can use the following command:

php artisan vendor:publish --provider="Rezkonline\TwoFactorAuth\TwoFactorAuthServiceProvider" --tag="laravel-2fa-config"

After published, this is how config/laravel-2fa.php will looks like:

[ "users" => "users", ], /* |-------------------------------------------------------------------------- | Two factor code length |-------------------------------------------------------------------------- | Specify the length of your two factor code. | */ "code_length" => 8, /* |-------------------------------------------------------------------------- | Two factor code expiration time |-------------------------------------------------------------------------- | Specify the duration of your two factor code in minutes. | */ "code_expires_in" => 10, /* |-------------------------------------------------------------------------- | Redirect to route |-------------------------------------------------------------------------- | Specify the route which users should be redirected to after successfully confirming | the two factor auth code. | */ "redirect_to_route" => "home" ];">


return [
    /*
   |--------------------------------------------------------------------------
   | Tables
   |--------------------------------------------------------------------------
   | Specify the basics authentication tables that you are using.
   | Once you required this package, the following tables are
   | created/modified by default when you run the command
   |
   | php artisan migrate
   |
    */
    "tables" => [
        "users" => "users",
    ],
   
    /*
   |--------------------------------------------------------------------------
   | Two factor code length
   |--------------------------------------------------------------------------
   | Specify the length of your two factor code.
   |
    */
    "code_length" => 8,

     /*
    |--------------------------------------------------------------------------
    | Two factor code expiration time
    |--------------------------------------------------------------------------
    | Specify the duration of your two factor code in minutes.
    |
    */
    "code_expires_in" => 10,

     /*
     |--------------------------------------------------------------------------
     | Redirect to route
     |--------------------------------------------------------------------------
     | Specify the route which users should be redirected to after successfully confirming
     | the two factor auth code.
     |
      */
    "redirect_to_route" => "home"
];

Publish package assets

This package uses a custom view to confirm the two factor code. You need to publish the package assets to that view with the following command:

php artisan vendor:publish --provider="Rezkonline\TwoFactorAuth\TwoFactorAuthServiceProvider" --tag="laravel-2fa-assets" 

Usage

To start using this package, you need to configure your email settings in .env file. This is an example config:

MAIL_MAILER=your_mailer
MAIL_HOST=your_mailer_host
MAIL_PORT=2525
MAIL_USERNAME=your_mail_username
MAIL_PASSWORD=your_mail_password
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=your_mail@your_domain.com
MAIL_FROM_NAME="${APP_NAME}"

Now, you need to register the two_factor middleware in your app/Http/Kernel.php file. Add it to the routeMiddleware array:

protected $routeMiddleware = [
    ...
    'two_factor_auth' => TwoFactorAuthMiddleware::class
];

After that, you just need to protect your routes with the two_factor middleware:

Route::middleware('two_factor_auth')->group(function() {
    // Your routes here
});

Events

This package dispatches events for two factor code confirmed and two factor code resent actions.

You can listen to these events in your EventServiceProvider:

protected $listen = [
    \Rezkonline\TwoFactorAuth\Events\TwoFactorCodeConfirmed::class => [
        //Your listeners here
    ],
    \Rezkonline\TwoFactorAuth\Events\TwoFactorCodeResent::class => [
        // Your listeners here
    ]
];

With your routes protected, your users must confirm the two factor authentication code, which will be sent via email after they login with correct credentials.

Contributing

Thank you for considering contributing for the Laravel Invite Codes package! The contribution guide can be found here.

Tests

Run composer test to test this package.

Credits

License

The Laravel 2FA package is open-sourced software licenced under the MIT License. Please see the License File for more information.

You might also like...
This repository includes a sample project to illustrate the usage of the JobRouter® Authentication Factor API.

JR 2FA Example Plugin This repository includes a sample project to illustrate the usage of the JobRouter® Authentication Factor API. It can be used as

PHP class to generate and verify Google Authenticator 2-factor authentication

Google Authenticator PHP class Copyright (c) 2012-2016, http://www.phpgangsta.de Author: Michael Kliewe, @PHPGangsta and contributors Licensed under t

phpCAS is an authentication library that allows PHP applications to easily authenticate users via a Central Authentication Service (CAS) server.

phpCAS is an authentication library that allows PHP applications to easily authenticate users via a Central Authentication Service (CAS) server.

It's a Laravel 8 authentication markdown that will help you to understand and grasp all the underlying functionality for Session and API Authentication

About Auth Starter It's a Laravel 8 authentication markdown that will help you to understand and grasp all the underlying functionality for Session an

A Simple method to create laravel authentication for an existing laravel project.
A Simple method to create laravel authentication for an existing laravel project.

Laravel Simple Auth A Simple method to create laravel authentication for an existing laravel project. Indroduction Why I created this kind of package?

Simple readonly LDAP authentication with Laravel 5.2

ldap-auth Very basic READ ONLY LDAP authentication driver for Laravel 5.2+ Look HERE for the package for Laravel 5.1. However, only the 5.2 Version wi

Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.

Introduction Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs. Official Documentation Documentation for Sanctum

This is a simple laravel authentication built with livewire jetstream.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Owner
Rezkonline
Rezkonline
Laravel Two-Factor Authentication

This package allow you to enable two-factor authentication in your Laravel applications very easily, without the need to add middleware or any modification to your routes. It stores tokens in your database in a distinct table, so you don't need to alter your users table. Notify users about their token via mail, SMS or any custom channel.

null 7 Jun 24, 2022
Google Two-Factor Authentication Package for Laravel

Google2FA for Laravel Google Two-Factor Authentication Package for Laravel Google2FA is a PHP implementation of the Google Two-Factor Authentication M

Antonio Carlos Ribeiro 785 Dec 31, 2022
PHP library for Two Factor Authentication (TFA / 2FA)

PHP library for Two Factor Authentication PHP library for two-factor (or multi-factor) authentication using TOTP and QR-codes. Inspired by, based on b

Rob Janssen 896 Dec 30, 2022
Vendor-Agnostic Two-Factor Authentication

Multi-Factor Designed to be a vendor-agnostic implementation of various Two-Factor Authentication solutions. Developed by Paragon Initiative Enterpris

Paragon Initiative Enterprises 139 Dec 21, 2022
Redirects any user which hasn't setup two factor authentication yet to /2fa/

force-two-factor Redirects any user which hasn't setup two factor authentication yet to /2fa/. Use together with the forked two-factor plugin at https

Aiwos 0 Dec 24, 2021
Secure WordPress login with two factor authentication

This plugin allows you to secure your WordPress login with two factor authentication. The users will have to enter a one time password every time they log in.

Volodymyr Kolesnykov 6 Nov 2, 2022
Two-Factor Authentication for all your users out-of-the-box.

Two Factor On-premises Two-Factor Authentication for all your users out of the box. use Illuminate\Support\Facades\Auth; use Laragear\TwoFactor\TwoFac

Laragear 105 Dec 22, 2022
PHP library for Two Factor Authentication (TFA / 2FA)

PHP library for Two Factor Authentication PHP library for two-factor (or multi-factor) authentication using TOTP and QR-codes. Inspired by, based on b

Rob Janssen 896 Dec 30, 2022
Multi-factor Authentication using a Public PGP key for web based applications

PGPmfa() a PHP Class for PGP Multi-factor Authentication using a Public PGP key for web based applications Multi-factor Authentication with PGP Second

null 2 Nov 27, 2022
API stubs for developing a plugin that provides a 2FA authentication factor in JobRouter®.

Authentication Factor API JobRouter® is a scalable digitisation platform which links processes, data and documents. Starting with JobRouter® 5.2, a se

JobRouter 4 Nov 4, 2021