Search code examples
phplaravelhybridauth

Hybrid Auth: Logout


I'm using Laravel 4 to implement Hybrid Auth (Steam Community). I have made two methods in my Controller, login and logout.

Login is working, and displays the information from Steam:

public function login()
{

    $config = array(    
        "base_url"   => "http://site.com/login/auth",
        "providers" => array ( 
            "OpenID" => array (
                    "enabled" => true
            ),
            "Steam"  => array ( 
                "enabled" => true 
            )
        )
    );

    try {
        $socialAuth = new Hybrid_Auth($config);
        $provider = $socialAuth->authenticate("Steam");
        $userProfile = $provider->getUserProfile();
    }
    catch(Exception $e) {
        return "Error: " . $e;
    }

    echo "Connected with: <b>{$provider->id}</b><br />";
    echo "As: <b>{$userProfile->displayName}</b><br />";
    echo "<pre>" . print_r( $userProfile, true ) . "</pre><br />";
    echo "<img src=". $userProfile->photoURL . ">";

}

Now to logout, I would call $provider->logout(); However I want to logout using another method.

However, I can't seem to understand how this would work... I have tried things such as:

public function logout() 
{

    Hybrid_Auth()->authenticate('Steam')->logout();

}

There is documentation on http://hybridauth.sourceforge.net/apidoc.html delaring methods(?) such as Hybrid_Auth::logoutAllProviders() But I can't seem to work out how to use it!

Any help would be swell!

Thanks.


Solution

  • You can instantiate a Hybrid_Auth class in your logout function and then use the logoutAllProviders method:

    (new Hybrid_Auth($config))->logoutAllProviders();
    

    However, I suggest that you pass HybriadAuth's instance to the constructor:

    # YOUR CONTROLLER 
    public function __construct(Hybrid_Auth $hybridAuth)
    {
        $this->hybridAuth = $hybridAuth;
    }
    
    public function logout()
    {
        $this->HybridAuth->logoutAllProviders();
    }
    
    # ELSEWHERE IN THE APP (ROUTES FILE, FOR INSTANCE)
    App::bind('Hybrid_Auth', function() {
        return new Hybrid_Auth(array(
                "base_url"   => "http://site.com/login/auth",
                "providers" => array (
                    "OpenID" => array (
                            "enabled" => true
                    ),
                    "Steam"  => array (
                        "enabled" => true
                    )
                )
            ));
    });
    

    With dependency injection, your controller should also be testable.