dqsh30374 2018-05-07 16:18
浏览 29
已采纳

CakePHP 3:会议已经开始使用Hybridauth 3

I have a LoginController where I do my usual login operation with combination of an email address and a password associated with the account.

I have separated my Hybridauth related code into a separate controller named OauthController where I have all my Hybridauth magic and where my callback / endpoint resides.

In the OauthController I check if user's email from the specified provider is already registered, and in either case I try to login that user with $this->Auth->setUser(object).

Whenever, or whatever from the $this->Auth is called, I get a response stating:

Session was already started

I have browser through CakePHP 3 code and found the following statement in:

vendor/cakephp/cakephp/src/Network/Session.php (335)

public function start()
{
    if ($this->_started) {
        return true;
    }

    if ($this->_isCLI) {
        $_SESSION = [];
        $this->id('cli');

        return $this->_started = true;
    }

    if (session_status() === \PHP_SESSION_ACTIVE) {
        throw new RuntimeException('Session was already started');
    }

    ...

And that's the point in code where that message is thrown at me.

Now, as I browsed through the Hybridauth code itself, I have found following in:

vendor/hybridauth/hybridauth/src/Storage/Session.php (46)

public function __construct()
{
    if (session_id()) {
        return;
    }

    if (headers_sent()) {
        throw new RuntimeException('HTTP headers already sent to browser and Hybridauth won\'t be able to start/resume PHP session. To resolve this, session_start() must be called before outputing any data.');
    }

    if (! session_start()) {
        throw new RuntimeException('PHP session failed to start.');
    }
}

And both of them call session_start, one before the other, although CakePHP's part is blocking me.

I have tried removing !session_start() check from Hybridauth, but then Hybridauth doesn't know where to read out it's thingies it needs to read.

So, as a demonstrator, I am trying to achieve this in OauthController:

<?php

namespace App\Controller;

use Hybridauth\Hybridauth;

class OauthController extends AppController
{

    public function callback($provider)
    {
        try {
            $hybridauth = new Hybridauth($config);

            // additional mystery code

            $hybridauth->authenticate();

            if($everything_okay) {
                $this->Auth->setUser($userObject); // and this is the point of failure
                return $this->redirect('/account'); // and this never happends... :(
            }
        }
    }

}

Any help, ideas, insights on how to deal with this are all welcome!

  • 写回答

1条回答 默认 最新

  • dongshang3309 2018-05-07 18:19
    关注

    Simply start the CakePHP session manually before using the Hybridauth library, so that it bails out at the session_id() check and picks up the existing session.

    For example in your controller:

    $this->getRequest()->getSession()->start();
    
    // in CakePHP versions before 3.6/3.5
    // $this->request->session()->start();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化