Sometimes we need a passwordless authentication system in our web application. Assume an OTP authentication system or login with just a username. In this example, we will see how we can implement a passwordless auth system in laravel. Laravel by default provides a login system without a password using a user instance. 

If we need to set a user instance as the currently authenticated user, we may pass the user instance to the Auth facade's login method. 

laravel-login-system-without-password

Let's see the example code of laravel auth login without password:

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class TestController extends Controller
{
    public function login(Request $request)
    {
        $user = User::find(1);
        if(isset($user)){
            Auth::login($user);
            $request->session()->regenerate();
            //user is logged in.
        }

        return redirect('/');
    }
}

 

Using this method, we can login user using user id, mobile number or username whatever you want.

 

Read also: Laravel 9 Auth With Inertia JS Jetstream Tutorial

 

Conclusion

Now we know laravel 9 login without password. Hope this laravel login with username tutorial will help you to custom login system with username or without password in laravel application.

Category : #laravel

Tags : #laravel , #laravel auth