XavRsl / Cas

CAS server SSO authentication in Laravel 4.x & 5.x
MIT License
77 stars 36 forks source link

How to return null when not auth #45

Closed adispartadev closed 6 years ago

adispartadev commented 6 years ago

I use \Cas::authenticate(); and then $user = \Cas::getCurrentUser(); to get current login user as well. But, I want to get null when user not auth yet without redirect to CAS_LOGIN_REDIRECT.

How can I do that?

XavRsl commented 6 years ago

Hi,

GetCurrentUser is just a wrapper for Cas::getUser(). I've checked the phpCas rapidly and I don't think it's possible to just catch an exception and return null, but maybe you should still try it. Something like this:

try {
    $user = \Cas::getCurrentUser();
} catch(\Exception $e) {
    $user = null;
}

It may not work as I'm not sure the phpCas library is actually throwing an error or not, but it's worth trying.

Xavier

adispartadev commented 6 years ago

Hi, thanks for your fast respon btw. I had try that and that not work.

try {
        $user = \Cas::getCurrentUser();
} catch(\Exception $e) {
        $user = null;
}

But it give an error Error: Internal script failure. I try to add \Cas::authenticate(); before $user = \Cas::getCurrentUser(); but it redirect to our login page instead of return null.

adispartadev commented 6 years ago

I solved this problem by using this function to return true or false when user is login or not without redirect to sso page when user not login yet.

$isLogin = Cas::isAuthenticated();