In CakePHP 3, I found two ways to find if a user is logged in.
1st solution
if(!is_null($this->Auth->user('id'))){
// Logged in
}
2nd solution
if (!is_null($this->request->session()->read('Auth.User.id'))) {
// Logged in
}
I think the first one is better because it's short and concise.
Is there a better way to verify if a user is logged in?
I'm not looking for speed necessarily. I want a clean and expressive way to write it.
I think the best way is just:
if ($this->Auth->user()) {...}
You can do this using session()
helper.
$loggeduser = $this->request->session()->read('Auth.User');
if(!$loggeduser) {
$userID = $loggeduser['id'];
$firstName = $loggeduser['first_name'];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With