0

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.

Alias
  • 2,983
  • 7
  • 39
  • 62
  • Sorry to comment on your question, but since you seem to have got it working how have you dealt with hybridauth in Laravel not loading Steam from the right path as described here? http://stackoverflow.com/questions/18592642/laravel-4-composer-and-hybridauth-how-to-load-additional-providers – John Mellor Sep 03 '13 at 14:59
  • Hey, commented on that question for you. – Alias Sep 06 '13 at 14:08

1 Answers1

3

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.

MrCasual
  • 153
  • 1
  • 1
  • 3
  • Thanks, although how do I initiate the Steam authentication? $provider = $this->HybridAuth->authenticate("Steam"); or something? (Doesn't work but I think I'm close...) – Alias Jun 17 '13 at 08:46
  • Ah, it's "hybridAuth" rather than "HybridAuth". So I've managed to login, however when calling my logout function, I get the error "Route pattern "/login/{action}/{{action}}" cannot reference variable name "action" more than once." Seen that before? – Alias Jun 17 '13 at 09:05
  • SOLVED: The routes need to use the correct resource controllers: http://laravel.com/docs/controllers#resource-controllers – Alias Jun 17 '13 at 14:33