Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check "isAuthenticated()" #47

Open
wisnetmark opened this issue Nov 17, 2022 · 2 comments
Open

How to check "isAuthenticated()" #47

wisnetmark opened this issue Nov 17, 2022 · 2 comments
Labels
question Further information is requested

Comments

@wisnetmark
Copy link

Not so much an issue but more of a question. I'm trying to use the Auth->isAuthenticated() function but can't for the life of me get the Auth model instantiated without jumping through a bunch of hoops trying to reverse engineer the middleware that sets the settings and everything. Has anyone done this before on this?

@breart
Copy link
Member

breart commented Nov 20, 2022

The actual user authentication is not handled by this library — that you have to do by yourself.

The middleware resolves a tenant, meaning it identifies an Identity Provider that makes a request and applies the configuration. Then you can use an event SignedIn and do your magic logging in the actual user based on the attributes you get.

@breart breart added the question Further information is requested label Nov 20, 2022
@saeed393
Copy link

Hi, @wisnetmark This is a sample SignedInListener code.

`<?php

namespace App\Listeners;

use App\Core\SsoUser;
use App\Helpers\Logger;
use App\Models\User;
use Slides\Saml2\Events\SignedIn;
use Illuminate\Support\Facades\Auth;

class SignedInListener
{

/**
 * Handle the event.
 *
 * @param SignedIn $event
 * @return void
 */
public function handle(SignedIn $event)
{
    try {
        $samlUser = $event->user;

        if ($event->auth->isAuthenticated()) {
            Logger::sso()->info('User is authenticated with attributes: ', $samlUser->getAttributes());

        } else {
            Logger::sso()->info('User is not authenticated.');
            Logger::sso()->error('Last error : ' . $event->auth->getLastErrorReason());
        }

        $user = new SsoUser();
        $user->setSsoAttributes($samlUser->getAttributes());

        if ($bsUser = $user->getUser()) {
            Auth::login($bsUser);

            if (Auth::check()) {
                Logger::sso()->info($bsUser->email . ' signed-in successfully via sso.');
             
            }
        } else {
            Logger::sso()->error('User Not Found.');
        }

    } catch (\Exception $exception) {
        Logger::sso()->error(__METHOD__ . ' Exception: ' . $exception->getMessage());
    }

}

public function failed(SignedIn $event, $exception)
{
    Logger::sso()->error('SignedIn listener failed. Error: ' . $exception->getMessage());
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants