cesargb / laravel-magiclink by cesargb

Create secure link for access to private data or login in Laravel without password
662,807
383
8
Package Data
Maintainer Username: cesargb
Maintainer Contact: cesargb@gmail.com (Cesar Garcia)
Package Create Date: 2017-07-25
Package Last Update: 2024-11-13
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:06:05
Package Statistics
Total Downloads: 662,807
Monthly Downloads: 28,773
Daily Downloads: 1,194
Total Stars: 383
Total Watchers: 8
Total Forks: 44
Total Open Issues: 7

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.

Instalation

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.