Home  >  Article  >  PHP Framework  >  How to use Laravel to verify user is logged in

How to use Laravel to verify user is logged in

PHPz
PHPzOriginal
2023-04-23 09:09:571142browse

Laravel is one of the most popular PHP frameworks currently. It provides many useful tools and functions that enable developers to quickly build powerful web applications. In many web applications, users need to be logged in in order to access protected pages, and we need to learn how to use Laravel to authenticate users if they are logged in.

In Laravel, the user authentication system is managed by the official Laravel Auth. This system provides many common authentication functions, such as user registration, login, and password reset. In this system, we use Middleware to verify that the user is logged in. Middleware is a request filter used to perform some operations before the request reaches the specified route. We can use Middleware to verify that the user is logged in so that we can restrict the user's access to protected pages.

Below, I will show you how to use Middleware to verify whether the user is logged in. We can use Laravel's own make:middleware command to create a new Middleware. For example, we can create a middleware named "AuthMiddleware".

We can run the following command to create AuthMiddleware:

php artisan make:middleware AuthMiddleware

After running the above command, Laravel will create an AuthMiddleware.php file in the app/Http/Middleware directory. At this point, we need to write our verification logic in the handle method of this file. Here is an example:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class AuthMiddleware
{
    public function handle($request, Closure $next)
    {
        if (!Auth::check()) {
            return redirect(&#39;/login&#39;);
        }

        return $next($request);
    }
}

In the above example, we first use the Auth::check() method to check if the user is logged in. If the user is not logged in, we will redirect to the /login page. If the user is already logged in, we will allow the request to proceed, this is what we call "through the Middleware".

Next, we need to use AuthMiddleware in our routes. There are two ways we can use Middleware in Laravel's route definition:

Method 1: Use Middleware in the route definition

Route::get(&#39;/dashboard&#39;, function () {
    return view(&#39;dashboard&#39;);
})->middleware('auth');

In the above code, we use auth middleware to verify Whether the user is already logged in. When a user accesses the /dashboard page, if the user is not logged in, it will be redirected to the /login page.

Method 2: Use Middleware in the controller class

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        return view('dashboard');
    }
}

In the above code, we use AuthMiddleware in the controller. When the user accesses/dashboard, if the user is not logged in, it will is redirected to the /login page.

Now that we have learned how to use Laravel to verify that the user is logged in, we can use Middleware in our controllers and routes to restrict users from accessing protected pages. Through Middleware, we can make our applications more secure and reliable.

The above is the detailed content of How to use Laravel to verify user is logged in. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is laravel userNext article:what is laravel user