Laravel Passwordless Authentication Part 1

1 year ago admin Laravel

In this tutorial, we will see how to create a Laravel passwordless authentication system, the user will create an account and once created he will receive an email with a unique signature to log in, the same process will happen when he logs in.


Update the user's migration

First, let's update the user's migration we set the password default value to null because we will not use it for authenticating users.

Next, run php artisan migrate to create the database and migrate the tables.

                                                    
                                                                                                                
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
    }
};

Generating the mailable with the markdown template

Next, let's generate the mailable with the markdown template we name it 'LoginLink' with a template named 'login_link' inside the folder 'emails'.

                                                        
                                                                                                                        
php artisan make:mail LoginLink --markdown=emails.login_link

Update the mailable content

Next, let's update the 'LoginLink' content we create a temporary signed URL, it takes the route as the first param, and the time of expiration here it's 10 minutes as the second param, and finally the user email as the third param. 

                                                        
                                                                                                                        
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\URL;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Contracts\Queue\ShouldQueue;

class LoginLink extends Mailable
{
    use Queueable, SerializesModels;

    public string $url;

    /**
     * Create a new message instance.
     */
    public function __construct(string $email)
    {
        //
        $this->url = URL::temporarySignedRoute('user.session', now()->addMinutes(10), ['email' => $email]);
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Your login Link',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            markdown: 'emails.login_link',
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}

Update the markdown template

Next, let's update the 'login_link.blade.php' content inside we add the button that contains the link for authenticating the user.

                                                        
                                                                                                                        
<x-mail::message>
# Your login link

<x-mail::button :url="$url">
Login
</x-mail::button>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>


Update the env file to send emails

To send emails we will use the Mailtrap service, you need to create an account choose Laravel from the integrations dropdown menu, and grab your credentials.

Inside the .env file add the credentials.

                                                        
                                                                                                                        
MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME='Your username'
MAIL_PASSWORD='Your password'
MAIL_FROM_NAME="${APP_NAME}"

Related Tuorials

How to Show the Old Values in Multiple Select Options in Laravel

In this lesson, we will see how to show the old values in multiple select options when editing in La...


How to Get the Old Value of the Select in Laravel

In this lesson, we will see how to get the old value of the select when editing in Laravel, this app...


How to Show the Old Value of the Input Field When Editing in Laravel

in this lesson, we will see how to show the old value of the input field when editing in Laravel, th...


How to Prevent the Loop Incrementing Operator from Resetting Back to 1 in the Next Pagination Pages in Laravel

In this lesson, we will see how to prevent the loop incrementing operator from resetting back to 1 i...


How to Logout a User from the Other Devices in Laravel 11

In this lesson, we will see how to logout a user from the other devices in Laravel 11, sometimes you...


How to Logout a User from the Current Device in Laravel 11

In this lesson, we will see how to logout a user from the current device in Laravel 11, sometimes yo...


How to Import Multiple Classes from a Single Namespace in Laravel

In this lesson, we will see how to import multiple classes from a single namespace in Laravel by add...


Laravel 11 Livewire CRUD Application Tutorial Part 2

In the second part of this tutorial, we will display all the tasks on the home page and later we wil...


Laravel 11 Livewire CRUD Application Tutorial Part 1

This tutorial will show us how to create a Laravel 11 Livewire CRUD application. The user can c...


How to Conditionally Include a Blade Template in Laravel

In this lesson, we will see how to conditionally include a blade template in Laravel.Sometimes,...