简体   繁体   中英

Why use Auth::check() if Auth::user() retrieves already authenticated user or null?

Just to clarify the question, are the following statements equivalent, or am I missing something and introducing a security hole?

// 1
if(Auth::user()){
    // do something
}

// 2
if(Auth::check() && Auth::user()){
    // do something
}

Let's take a quick peek under the hood

public function check()
{
    return ! is_null($this->user());
}

So as you can see, all that check does, is check for null. Using Auth::check() is much easier when all you need to know if whether they are auth'd. It would make no sense to return an object if you're not going to use it.

the statement Auth::Check() just returns if user is logged in or not.

Auth::user() returns the user that's logged in, if have one.

$user_name = Auth::user()->name;

and you can retrieve user's data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM