In this article, we are going to learn how to create a passwordless login system in Laravel. In here, we will use only the user's email address to register and login. In the registration, we will send a link to given email for email verification, while we will send login link everytime when user input email.
Setting up the Project
First you need to create a fresh laravel project by running below command in your terminal
Then navigate to your project directory by using below command in your terminal
1
cd laravel-passwordless-login
Setting up Database
To setup up Database for our project, open the application using your favourite text editor and and then navigate to .env file in it and then change below section acording to your Database settings:
In here, for testing our emails we use one of the most popular mail testing platform Mailtrap. To configure mail settings, navigate to .env file in it and then change below section acording to your Mailtrap settings:
By default Laravel comes with a migration file named create_users_table which is used to create a table named users in your database. Then navigate to your users migration file and add below code on it:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->boolean('email_verified')->default(0)->comment('1 for verified and 0 for not verified'); $table->string('token'); $table->rememberToken(); $table->timestamps(); }); } public function down() { Schema::dropIfExists('products'); } }
Then run below command in your terminal to migrate migration file:
1
php artisan migrate
Create Product Controller
Now we need to create contoller to handle our core logic for handling our login/ register links and views. To create new controller:
1
php artisan make:controller AuthController
After that open your app/Http/Controllers/AuthController.php and paste below code in it:
use Auth; use Mail; use App\Mail\LoginMail; use App\Mail\RegisterMail; use Illuminate\Http\Request; use Illuminate\Support\Str; use App\Models\User;
class AuthController extends Controller {
public function __construct() { $this->middleware('guest')->except(['logout', 'dashboard']); $this->middleware('auth')->only(['logout', 'dashboard']); }
/* function show register form */ public function showRegisterForm() { return view('auth.register'); }
/* function to register user */ public function register(Request $request) { $input = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email|string|max:255|unique:users', ]);
return redirect()->back()->with( 'success', 'Verification mail sent, please check your inbox.' ); } /* function to verify token after user registration */ public function verifyToken(Request $request) { $input = $request->validate([ 'token' => 'required|string', ]);
return redirect()->back()->with( 'error', 'Login link is not valid.' ); }
/* function to load dashboard */ public function dashboard() { return view('dashboard'); }
/* function to logout user */ public function logout(Request $request) { auth()->guard('web')->logout(); \Session::flush(); return redirect()->route('loginForm')->with( 'success', 'You are successfully logged out.' ); }
}
Create mailable classes
To send the email, we need to create mailable class. In this step, we will create mailable classes RegisterMail and LoginMail. Run the following Artisan commands to generate mailable class.
1
php artisan make:mail RegisterMail
It will create a file called RegisterMail.php in app/Mail/RegisterMail.php. Then paste below code in it:
use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels;
class LoginMail extends Mailable { use Queueable, SerializesModels;
public $token;
public function __construct($token) { $this->token = $token; }
public function build() { return $this->view('mail.login') ->subject('Click on the link to login.') ->with('token', $this->token); } }
Create mail templates
We have configured mailable class. Now we need to create mail template blade file with link. We will send this template with email. Laravel blade files are located at resources/views directory.
For verification mail, create register.blade.php file at resources/views/mail directory and input below HTML code into it.
In this tutorial, we passwordless login in Laravel. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail. You can obtain complete source code and asset files for this tutorial from this GitHub repository.