duanshang9426 2017-10-20 14:57
浏览 58
已采纳

如何使用Hybridauth 3登录/退出?

I can login/auth the user with Hybridauth3 lib. Here is my code:

session_name("authSession");
session_start();
include 'hybridauth/src/autoload.php';
$config = [
    'callback' => "https://***/loginRead.php",
    'providers' => [
        'Twitter' => [],"includeEmail" => true],
        'Google'   => [], 
        'Facebook' => []   
    ],
];

$authName = "Facebook";
if(isset($_REQUEST['provider'])){

    $provider = strtolower($_REQUEST['provider']);
    switch ($provider) {
        case 'google':
            $authName = "Google";
            break;
        case 'facebook':
            $authName = "Facebook";
            break;
        case 'twitter':
            $authName = "Twitter";
            break;
    }
    $_SESSION['provider'] = $authName;

    $hybridauth = new Hybridauth\Hybridauth( $config ); 
    $adapter = $hybridauth->getAdapter($authName);
    $user = $adapter->authenticate();
    $userProfile = $adapter->getUserProfile();

}
    // this part is for this callback https://***/loginRead.php from config
    if(isset($_SESSION['HYBRIDAUTH::STORAGE']) && count($_SESSION['HYBRIDAUTH::STORAGE'])>=1){

        $provider = $_SESSION['provider'];
        $hybridauth = new Hybridauth\Hybridauth( $config ); 
        $adapter = $hybridauth->getAdapter($provider);
        $user = $adapter->authenticate();
        $userProfile = $adapter->getUserProfile();
    }

    if(isset($_GET['loginExit'])){
        $adapter->disconnect();
        $hybridauth->disconnectAllAdapters();
        session_destroy();
        header("Location:test.php");
        exit();
    }

    echo '<img src="'.$userProfile->photoURL.'"><br>';
    echo $userProfile->identifier.'<br>';
    echo '<a href="?loginExit">Exit</a>';

But I am pretty sure that I am not using it right. As I am saving the provider in session for the second redirect, but there must be way to get the provider name with Hybridauth api or maybe there is no need to do that? How to auth the user with [HYBRIDAUTH::STORAGE] without knowing the privider name? Is there any good 100% working example for the 3 version or etc? Thanks

  • 写回答

1条回答 默认 最新

  • dtcwehta624485 2017-10-26 23:51
    关注

    As @Anar Bayramov points out, there are some examples, although you probably want to look at the official repository examples instead. They also have some prettier online documentation for Hybridauth if you prefer that over just the code examples.

    If you want to use session storage via the Hybrid 'API', then you want to access via the Storage\Session class instead of trying to manipulate the session directly. Broadly speaking, having a library have their own access method and you trying to skip it to talk to the session directly is going to result in some pretty funky, hard to debug problems.

    If you use the Hybridauth Session accessor to store the provider type, then you'll be able to load the correct one from their session storage instead of your own.

    Final note, as of writing this answer Hybridauth v3 is in active development but is marked as beta and potentially unsuitable for production use. v2.9.6 is the current stable release.

    Sidebar

    There's a baisc PHP syntax error in the definition of $config in the OP, so that code is is never going to work. You should probably be loading the library via Composer as well - it will make your life easier for any PHP application. The "which provider do I use" code seems a bit contrived/roundabout when there are simpler ways of writing the same code, but it should do the trick.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog