How to setup passwordless Login In Laravel

Laravel
April 02, 20212 minutesuserShailesh Ladumor
How to setup passwordless Login In Laravel
Parsed in 0.31 ms or 2 times faster

Basically, we set up email/username and password login in all our projects. but, sometimes we need to implement s passwordless login in the laravel application,

First of all, what is passwordless login? passwordless login is an authentication method that allows the user to log in without entering a password.

In this article, I show you how to set up passwordless login laravel step by step.

Step 1:

one great laravel package Laravel Passwordless Login provides the ability to log in without a password.

This package provides a temporary signed URL link that logs in a user, What it does not provide is a way of actually sending the link to the route to the user. This is because I don't want to make any assumptions about how you communicate with your users.

Step 2:

Open the terminal and go to the project directory and fire the following command to install

composer require grosv/laravel-passwordless-login

Step 3:

Configure the following variables in your env file

  LPL_USER_MODEL=App\User
  LPL_REMEMBER_LOGIN=false
  LPL_LOGIN_ROUTE=/magic-login
  LPL_LOGIN_ROUTE_NAME=magic-login
  LPL_LOGIN_ROUTE_EXPIRES=30
  LPL_REDIRECT_ON_LOGIN=/
  LPL_USER_GUARD=web
  LPL_USE_ONCE=false
  LPL_INVALID_SIGNATURE_MESSAGE="Expired or Invalid Link"

Step 4:

Create one function in your login controller. it looks like

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

function sendLoginLink(\Request $request)
{
    $user = User::where('email','=', $request->get('email))->first();

    $generator = new LoginUrl($user);
    $url = $generator->generate();

    //OR Use a Facade
    $url = PasswordlessLogin::forUser($user)->generate();

    $data['url'] = $generator->generate();
    $data['user'] = $user;

    Mail::to($user->email)->send(new UserLoginMail($data));

    return back();
}

Step 5:

Set following route in your web.php

Route::post('/user/login', [LoginController::class, 'sendLoginLink'])->name('userLogin');

Step 6:

Create one mailable. you can refer to a doc if not familiar. Also, fire the following command to create a mailable

php artisan make:mail UserLoginMail

Step 7: Create an Email UI as per your requirement.