cesargb/laravel-magiclink

Create link for authenticate in Laravel without password

v1.5.1 2019-10-08 17:58 UTC

README

You don't need this package if use Laravel 5.6.12 or higher, if you use this version of Laravel you can use Signed URLs, this is an example:

Add a new route in file route/web.php to Auth:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

Route::get('/autologin', function (Request $request) {
    if (! $request->hasValidSignature()) {
        abort(403);
    }

    Auth::loginUsingId($request->input('user_id'));

    return response()->redirect($request->input('url_redirect', '/'));
})

Create a link to authenticate

use Illuminate\Support\Facades\URL;

$url_to_auth = URL::temporarySignedRoute(
    'autologin', now()->addDay(), [
        'user_id'       => 1,
        'url_redirect'  => '/dashboard',
    ],
);

If you use Laravel 5.6.11 or minor continue.

Latest Version on Packagist Build Status StyleCI Total Downloads

Create link for authenticate in Laravel without password

This package permit create a link for authenticate without user or password.

Installation

This package can be used in Laravel 5.4 or higher.

You can install the package via composer:

composer require cesargb/laravel-magiclink

If you have Laravel 5.4, you must add the service provider in config/app.php file:

'providers' => [
    // ...
    Cesargb\MagicLink\MagicLinkServiceProvider::class,
];

You can publish config file with:

php artisan vendor:publish --provider="Cesargb\MagicLink\MagicLinkServiceProvider" --tag=config

This is the contents of the published config/magiclink.php config file:

return [
    /*
    |--------------------------------------------------------------------------
    | Application magiclink Table
    |--------------------------------------------------------------------------
    |
    | This is the magiklink table used by the application to save links to the
    | database.
    |
    */
    'magiclink_table' => 'magic_links',
    /*
    |--------------------------------------------------------------------------
    | Application Users Table
    |--------------------------------------------------------------------------
    |
    | This is the users table used by the application to save users to the
    | database.
    |
    */
    'user_table' => 'users',
    /*
    |--------------------------------------------------------------------------
    | Application Primary Key of Users Table
    |--------------------------------------------------------------------------
    |
    | This is the primary key of users table used by the application to save
    | users to the database.
    |
    */
    'user_primarykey' => 'id',
    'token' => [
        /*
        |--------------------------------------------------------------------------
        | Token lifetime default
        |--------------------------------------------------------------------------
        |
        | Here you may specifiy the number of minutes you wish the default token
        | to remain active.
        |
        */
        'lifetime' => 4320,
    ],
    'url' => [
        /*
        |--------------------------------------------------------------------------
        | Path to Validate Token and Auto Auth
        |--------------------------------------------------------------------------
        |
        | Here you may specify the name of the path you'd like to use so that
        | the verify token and auth in system.
        |
        */
        'validate_path' => 'magiclink',
        /*
        |--------------------------------------------------------------------------
        | Path default to redirect
        |--------------------------------------------------------------------------
        |
        | Here you may specify the name of the path you'd like to use so that
        | the redirect when verify correct token.
        |
        */
        'redirect_default' => '/',
        /*
        |--------------------------------------------------------------------------
        | Path default to redirect when token is invalid
        |--------------------------------------------------------------------------
        |
        | Here you may specify the name of the path you'd like to use so that
        | the redirect when token is invalid.
        |
        */
        'redirect_error' => 'magiclink/error'
    ],
];

You can publish migration with command:

php artisan vendor:publish --provider="Cesargb\MagicLink\MagicLinkServiceProvider" --tag=migrations

After the migration has been published you can create the table by running the migrations:

php artisan migrate

Usage

First add the use Cesargb\MagicLink\Traits\HasMagicLink trait to your User model(s):

use Illuminate\Foundation\Auth\User as Authenticatable;

use Cesargb\MagicLink\Traits\HasMagicLink;

class User extends Authenticatable
{
    use HasMagicLink;

    // ...

This package allows for users to be associated with magiklick.

After add this trait, you can create a magiclink for a user, with this command:

$redirect_to = '/dashboard';
$minutes_forexpire_link = 4320;
$full_url_to_access_without_password = $user->create_magiclink($redirect_to, $minutes_forexpire_link);

Now, you can send notify at user to login with this magic link ($full_url_to_access_without_password).

License

The MIT License (MIT). Please see License File for more information.