Home > PHP Framework > Laravel > body text

How to determine whether to log in in laravel

青灯夜游
Release: 2023-01-13 00:40:24
Original
4208 people have browsed it

In laravel, you can use the check method of Auth to check whether the user is logged in. If logged in, true will be returned; the syntax "use Illuminate\Support\Facades\Auth;if (Auth::check() ){//Login operation}".

How to determine whether to log in in laravel

The operating environment of this tutorial: Windows 7 system, Laravel 6 version, Dell G3 computer.

Determine whether the current user is logged in (authenticated)

You can use the check method of the Auth facade to check the user Whether it has been certified. If authenticated, will return true:

use Illuminate\Support\Facades\Auth;

if (Auth::check()) {
    // 用户已经登录了...
}
Copy after login

Although you can use the check method to confirm whether the user is authenticated, before allowing the user to access certain routes/controllers, Often middleware is still used to verify that the user is authenticated.

Extended knowledge:

Many applications require logging in before they can be operated. Laravel provides an auth tool to implement user authentication. And there is a config/auth.php to configure the auth tool.

Let’s take a look at the common methods of auth tools:

Auth::check();// 判断当前用户是否未登录

Auth::guest();// 判断当前用户是否未登录,与 check() 相反

Auth::guard();// 自定义看守器 默认为 `web`

Auth::user();// 获取当前的认证用户,一个提供者的模型

Auth::id();// 获取当前的认证用户的 ID(未登录情况下会报错)

Auth::attempt(['email' => $email, 'password' => $password],true);// 通过给定的信息来尝试对用户进行认证(成功后会自动启动会话),第一个数组就是认证的参数,第二个参数true就是'记住我'功能

Auth::login(User::find(1), $remember = false);// 登录一个指定用户到应用上,一般是登陆的参数通过后,执行login方法,保存session等登陆成功的操作

Auth::logout();// 使用户退出登录(清除会话)
Copy after login

Related recommendations: The latest five Laravel video tutorials

The above is the detailed content of How to determine whether to log in in laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template