PHPackages                             tpaksu/laravel-otp-login - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. tpaksu/laravel-otp-login

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

tpaksu/laravel-otp-login
========================

Adds OTP login step to Laravel's built-in authentication system

2.0(2y ago)221.2k14MITPHP

Since Oct 23Pushed 2y agoCompare

[ Source](https://github.com/tpaksu/laravel-otp-login)[ Packagist](https://packagist.org/packages/tpaksu/laravel-otp-login)[ RSS](/packages/tpaksu-laravel-otp-login/feed)WikiDiscussions develop Synced today

READMEChangelog (5)Dependencies (5)Versions (11)Used By (0)

Laravel OTP Login Package
=========================

[](#laravel-otp-login-package)

[![Total Downloads](https://camo.githubusercontent.com/3112db6c0f35a15bf3b17b13ac088847c95acbf149b7d7168f9f9a14c10f1b39/68747470733a2f2f706f7365722e707567782e6f72672f7470616b73752f6c61726176656c2d6f74702d6c6f67696e2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/tpaksu/laravel-otp-login) [![Latest Stable Version](https://camo.githubusercontent.com/b1eda2babc095d9a0faf46441731bef94edb363c97276ee5b31ca4b47fc72712/68747470733a2f2f706f7365722e707567782e6f72672f7470616b73752f6c61726176656c2d6f74702d6c6f67696e2f762f737461626c652e706e67)](https://packagist.org/packages/tpaksu/laravel-otp-login) [![HitCount](https://camo.githubusercontent.com/f447b90279a0f5f2f82b7085ef2136c6bd2e347bed1333746615b36887d5db60/687474703a2f2f686974732e6477796c2e636f6d2f7470616b73752f6c61726176656c2d6f74702d6c6f67696e2e737667)](http://hits.dwyl.com/tpaksu/laravel-otp-login) [![](https://camo.githubusercontent.com/4563e4107ef3de602699e26eb4a550b64cfeab9ec77d5f24f98b9747cf0c4dd5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f6261646765732f736869656c64732e737667)](https://github.com/tpaksu/laravel-otp-login) [![](https://camo.githubusercontent.com/cae863279e628d012c849a6ed24df79c8cd7b7db1632f251d283a6d909279b48/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7470616b73752f6c61726176656c2d6f74702d6c6f67696e2e737667)](https://github.com/tpaksu/laravel-otp-login)[![Run PHPUnit tests](https://github.com/tpaksu/laravel-otp-login/actions/workflows/testing.yml/badge.svg?branch=develop)](https://github.com/tpaksu/laravel-otp-login/actions/workflows/testing.yml)

This package provides an One Time Password check step after successful login using the default authentication mechanism. The package stores all requested OTP and it's validation statuses in `one_time_passwords` and `one_time_password_logs` tables.

It uses the middleware included in this package to check if the user has passed the OTP check or not regarding the current authentication status.

Credits
-------

[](#credits)

This package is based on the idea of [nattaponra/laravel-one-time-password](https://github.com/nattaponra/laravel-one-time-password) package. The middleware, translations, views, the controller, routes and extending the services strategy is developed by me.

Requirements
------------

[](#requirements)

- [Composer](https://getcomposer.org/)
- [Laravel](http://laravel.com/)

Installation
------------

[](#installation)

You can install this package on an existing Laravel project with using composer:

```
 $ composer require tpaksu/laravel-otp-login
```

Then, register the OTP Login ServiceProvider editing **config/app.php** file and adding to providers array:

```
  tpaksu\LaravelOTPLogin\OTPServiceProvider::class,
```

Note: use the following for Laravel &lt;5.1 versions:

```
 'tpaksu\LaravelOTPLogin\OTPServiceProvider',
```

Publish files with:

```
 $ php artisan vendor:publish --provider="tpaksu\LaravelOTPLogin\OTPServiceProvider"
```

> **Warning**: After publishing files, the package will continue to use the vendor copies of services. To change this to use the published ones in `app/Http/OtpServices` folder;
>
> - Change the namespaces of the published services to `App\OtpServices`,
> - Change the references in `config/otp.php` in the `services` array to point the published services,
> - Run `composer dumpautoload`.
> - Run `php artisan config:cache`.

or by using only `php artisan vendor:publish` and select the `tpaksu\LaravelOTPLogin\OTPServiceProvider` from the outputted list.

Apply the migrations for the `OneTimePassword` and `OneTimePasswordLogs` tables:

```
 $ php artisan migrate
```

Configuration
-------------

[](#configuration)

This package publishes an `otp.php` file inside your applications's `config` folder which contains the settings for this package. Most of the variables are bound to environment variables, but you are free to directly edit this file, or add the configuration keys to the `.env` file.

This line shows if the service is enabled or not:

```
'otp_service_enabled' => true,
```

On this line, you can configure the default SMS service the system will use to send the OTP SMS'es:

```
'otp_default_service' => env("OTP_SERVICE", "nexmo"),
```

The services predefined in this package are `Nexmo`, `Twilio` and `BioTekno` for now, but it's very easy to add your custom service to this package. It'll be explained in detail in the [Services](#services) section of this documentation.

```
'services' => [
    'biotekno' => [
        "class" => \tpaksu\LaravelOTPLogin\Services\BioTekno::class,
        "username" => env('OTP_USERNAME', null),
        "password" => env('OTP_PASSWORD', null),
        "transmission_id" => env('OTP_TRANSMISSION_ID', null)
    ],
    'nexmo' => [
        'class' => \tpaksu\LaravelOTPLogin\Services\Nexmo::class,
        'api_key' => env("OTP_API_KEY", null),
        'api_secret' => env('OTP_API_SECRET', null),
        'from' => env('OTP_FROM', null)
    ],
    'twilio' => [
        'class' => \tpaksu\LaravelOTPLogin\Services\Twilio::class,
        'account_sid' => env("OTP_ACCOUNT_SID", null),
        'auth_token' => env("OTP_AUTH_TOKEN", null),
        'from' => env("OTP_FROM", null)
    ]
],
```

This is very important. The service expects you to have a phone field in your `users` table or a related table to send the SMS to the user. If your user's phone number is in the `users` table, you can write the field name directly to this setting.

```
'user_phone_field' => 'phone',
```

Otherwise, if it's in a table like `user_phones` linked to your `users` table, you can use the linked setting like this:

```
'user_phone_field' => 'user_phone.phone',
```

This line lets you define your user table's name. If you're using a different table than `users`, you can edit this line to point that. Needed for the migrations to reference the correct primary key. Note that you can change the model you're using for authentication by modifying the `auth.providers.users.model`. That's already a Laravel feature.

```
'users_table' => 'users',
```

This line lets you define your user table's primary key. If your primary key is not id, you can edit this line to your own customized primary key.

```
'user_id_field' => 'id',
```

This line shows the length of the generated one time password's reference number. See `otp_digit_length` setting description for limitations. It's not currently used in SMS but it's generated and saved to database. In later versions, I plan to implement it to the services.

```
'otp_reference_number_length' => 6,
```

This defines the OTP validity timeout in seconds after creating it, currently set as 5 minutes.

```
'otp_timeout' => 300,
```

This line shows the length of the generated one time password. It should be below 10 because of PHP's integer limit which is 232 (2,147,483,647) on 32-bit machines. It'll be more configurable in the later versions, but I don't think it'll be needed more than 10 digits for UX reasons.

```
'otp_digit_length' => 6,
```

Views
-----

[](#views)

This package publishes one view named `otpvalidate.blade.php` to `resources/views/vendor/laravel-otp-login` folder, which contains the OTP validation screen. The strings used in this view are fetched from the language files also published in this package, so if you are trying to change the strings inside this view, you can change it inside the language file.

Translations
------------

[](#translations)

This package publishes the translations to `resources/lang/vendor/laravel-otp-login` folder, Turkish and English languages are published by this package as `php` files, and you can translate it into more languages by using the English language file as a template. Here's the content of the English file as an example:

```
