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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题