PHPackages                             metarush/otp-auth - 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. metarush/otp-auth

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

metarush/otp-auth
=================

Authentication library using one-time passwords (via email token)

v4.1.0(1mo ago)343MITPHPPHP &gt;=8.0

Since Mar 16Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/metarush/otp-auth)[ Packagist](https://packagist.org/packages/metarush/otp-auth)[ RSS](/packages/metarush-otp-auth/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (14)Versions (17)Used By (0)

metarush/otp-auth
=================

[](#metarushotp-auth)

Authenticate and log in your users using one-time passwords (OTP) via email.

Install
-------

[](#install)

Install via composer as `metarush/otp-auth`

Database setup
--------------

[](#database-setup)

In addition to your usual `username` and/or `email` column, create the ff. columns in your users table.

- `otpHash` TEXT (255)
- `otpToken` TEXT(12)
- `otpExpire` INT (10)
- `rememberHash` TEXT (255)
- `rememberToken` TEXT (12)

Note: You can use other column names if you want, just set them in the config.

Sample usage
------------

[](#sample-usage)

The ff. example is meant to demonstrate the library's functionality in a simple way. It is not required to use this implementation as it is.

### \_init.php

[](#_initphp)

Create a `_init.php` file to be included on top of your script and put the ff.

**Initialize builder**

```
$builder = new MetaRush\OtpAuth\Builder;
```

**Define SMTP servers**

```
$smtpServers = [
    0 => $builder->SmtpServer()
        ->setHost('host')
        ->setUser('user')
        ->setPass('pass')
        ->setPort(465)
        ->setEncr('TLS'),
    1 => $builder->SmtpServer()
        ->setHost('host2')
        ->setUser('user')
        ->setPass('pass')
        ->setPort(465)
        ->setEncr('TLS'),
    ];
```

You can add as many as you like, the lib will failover to each automatically.

**Initialize the library with minimum config**

```
$auth = $builder->setDsn('mysql:host=localhost;dbname=foo')
    ->setServers($smtpServers)
    ->setAdminEmails(['admin@example.com'])
    ->setAppName('foo')
    ->setFromEmail('noreply@example.com')
    ->setUsernameColumn('email')
    ->setNotificationFromEmail('noreply@example.com')
    ->setTable('users')
    ->build();
```

Note: If you want to extend `Auth` class and still use the builder, you can pass your child class in the `build()` parameter as string. E.g. `->build('MyAuth');`

**Auto-login if username is remembered via cookie**

```
if (!$auth->authenticated()) {
    $rememberedUsername = $auth->rememberedUsername();
    if (null !== $rememberedUsername)
        $auth->login($rememberedUsername);
}
```

### login.php

[](#loginphp)

Create a `login.php` file and put the ff.

```
