whyounes/laravel-passwordless-auth

Laravel passwordless authentication for Laravel

v1.1 2016-11-24 18:51 UTC

This package is auto-updated.

Last update: 2024-03-29 03:26:28 UTC


README

Passwordless authentication for Laravel 5

Build status 68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f38633739363462662d353864352d343232392d393238622d6435373031306637313937372f6d696e692e706e67

Installation

Add the package to your project using Composer:

composer require whyounes/laravel-passwordless-auth

Publish package assets:

php artisan vandor:publish

Run the migration to create the tokens table:

php artisan migrate

Add it to you providers list:

// config/app.php

// ...
'providers' => [
    // ...
    Whyounes\Passwordless\Providers\PasswordlessProvider::class,
};

Add the Passwordless trait to your user model:

// app/User.php

class User extends Authenticatable
{
    use Whyounes\Passwordless\Traits\Passwordless;

    // ...
}

Configurations

If you don't want to use the user email along with the token, you can change it by overriding the following method:

// app/User.php

class User extends Authenticatable
{
    use Whyounes\Passwordless\Traits\Passwordless;

    // ...
    
    protected function getIdentifierKey()
    {
        return 'email';
    }
}

You can change the expiration time inside the config/passwordless.php file:

// config/passwordless.php

return [
    'expire_in' => 15, // Minutes
    'empty_tokens_after_login' => true // Empty user tokens after login
];

You can set the empty_tokens_after_login config to false if you don't want to delete unused tokens from DB.

Example

Display the login form for user to type the email:

// routes/web.php

Route::post('/login/direct', function() {
    return view('login.direct');
});

Catch the form submission:

// routes/web.php

Route::post('/login/direct', function(Request $request) {
    // send link to user mail
    $user = App\User::where('email', $request->get('email'))->first();
    if (!$user) {
        return redirect()->back(404)->with('error', 'User not found');
    }

    // generate token and save it
    $token = $user->generateToken(true);

    // send email to user
    \Mail::send("mails.login", ['token' => $token], function($message) use($token) {
        $message->to($token->user->email);
    });
});

Catch the login link request:

// routes/web.php

Route::get('/login/{token}', function(Request $request, $token) {
    $user = App\User::where('email', $request->get('email'))->first();

    if (!$user) {
        dd('User not found');
    }

    if($user->isValidToken($token))
    {
        // Login user
        Auth::login($user);
    } else {
        dd("Invalid token");
    }
});

Or, if you like working with exceptions:

// routes/web.php

Route::get('/login/{token}', function(Request $request, $token) {
    try {
        $user = App\User::where('email', $request->get('email'))->firstOrFail();
        $user->validateToken($token);

        Auth::login($user);
    } catch(Illuminate\Database\Eloquent\ModelNotFoundException $ex) {
        dd('User not found');
    } catch(Whyounes\Passwordless\Exceptions\InvalidTokenException $ex) {
        dd("Invalid token");
    }
});