How to use passwordless login in Laravel

Overview

Passwordless login temporarily allows the user to access an account without a password.

In this shot, we will use a package called laravel-passwordless-login.

Usage of passwordless login

You can use passwordless login during a security breach where you can’t access your account with your known password. You are then given passwordless access to make necessary changes, which can be a reset password, etc.

Package installation

Run the composer command below to install the package:

composer require grosv/laravel-passwordless-login

Example

In this example, let’s assume there’s an existing method to send emails or text messages to users.

use App\User;
use Grosv\LaravelPasswordlessLogin\LoginUrl;

function sendLoginLink()
{
    $user = User::find(1);// 1 is representing the user_id it can be passed with a variable

    $generator = new LoginUrl($user);
    $generator->setRedirectUrl('/somewhere/else'); // Redirect the user to the page you want him to see
    $url = $generator->generate();

    //You can also use facade
    $url = PasswordlessLogin::forUser($user)->generate();

    // Send $url as  an email or text  message
}

The code above gets the specified user. We use the LoginUrl() method to generate a specific link for the user account, and use setRedirectUrl() to redirect the user to the route we want them to access.

Please visit laravel-passwordless-login for complete documentation and usage of laravel-passwordless-login.

Free Resources