Skip to content

benbjurstrom/plink

Repository files navigation

Plink Screenshot

Passwordless Log-In Links for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides full-featured passwordless log-in links for Laravel applications.

  • βœ… Rate limited
  • βœ… Invalidated after first use
  • βœ… Locked to the user's session
  • βœ… Configurable expiration
  • βœ… Detailed error messages
  • βœ… Customizable mail template
  • βœ… Auditable logs

Installation

1. Install the package via composer

composer require benbjurstrom/plink

2. Add the package's interface and trait to your Authenticatable model

// app/Models/User.php
namespace App\Models;

//...
use BenBjurstrom\Plink\Models\Concerns\HasPlinks;
use BenBjurstrom\Plink\Models\Concerns\Plinkable;

class User extends Authenticatable implements Plinkable
{
    use HasFactory, Notifiable, HasPlinks;
    
    // ...
}

3. Publish and run the migrations

php artisan vendor:publish --tag="plink-migrations"
php artisan migrate

4. Add the package provided routes

// routes/web.php
Route::plinkRoutes();

5. (Optional) Publish the views for custom styling

php artisan vendor:publish --tag="plink-views"

This package publishes the following views:

resources/
└── views/
    └── vendor/
        └── plink/
            β”œβ”€β”€ error.blade.php
            β”œβ”€β”€ components/
                └── template.blade.php
            └── mail/
                β”œβ”€β”€ notification.blade.php
                └── plink.blade.php

6. (Optional) Publish the config file

php artisan vendor:publish --tag="plink-config"

This is the contents of the published config file:

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | Model Configuration
    |--------------------------------------------------------------------------
    |
    | This setting determines the model used by Plink to store and retrieve
    | one-time passwords. By default, it uses the 'App\Models\User' model.
    |
    */

    'models' => [
        'authenticatable' => env('AUTH_MODEL', App\Models\User::class),
    ],

    /*
    |--------------------------------------------------------------------------
    | Mailable Configuration
    |--------------------------------------------------------------------------
    |
    | This setting determines the Mailable class used by Plink to send emails.
    | Change this to your own Mailable class if you want to customize the email
    | sending behavior.
    |
    */

    'mailable' => BenBjurstrom\Plink\Mail\PlinkMail::class,

    /*
    |--------------------------------------------------------------------------
    | Template Configuration
    |--------------------------------------------------------------------------
    |
    | This setting determines the email template used by Plink to send emails.
    | Switch to 'plink::mail.notification' if you prefer to use the default 
    | Laravel notification template.
    |
    */

    'template' => 'plink::mail.plink',
    // 'template' => 'plink::mail.notification',
];

Usage

  1. Replace the Laravel Breeze LoginForm authenticate method with a sendEmail method that runs the SendPlink action. For example:
    public function sendEmail(): void
    {
        $this->validate();

        $this->ensureIsNotRateLimited();
        RateLimiter::hit($this->throttleKey(), 300);

        try {
            (new SendPlink)->handle($this->email);
        } catch (PlinkThrottleException $e) {
            throw ValidationException::withMessages([
                'form.email' => $e->getMessage(),
            ]);
        }

        RateLimiter::clear($this->throttleKey());
    }

Everything else is handled by the package components.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

About

πŸ”— Passwordless Log-In Links for Laravel

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published