简体   繁体   中英

YII2 AuthClient onAuthSuccess session Not Saving

Hope someone could help me with this issue.

We are using AuthClient for Social Signup and Login. Feature is working fine locally on Windows XAMPP (PHP 5.6) but on the server (Amazon Ec2 AMI, PHP 5.6), code is not saving session data.

public function onAuthSuccess($client)
{   
    //.... COMMENTED CODE
    $session = Yii::$app->session;

    //Store social info in session
    $session['socialClient'] = $_GET['authclient'];

    return;
}

When we are trying to access the session data set in the above function in the following Controller Action, the session data is never saved.

Weirdly after hours of research, if we use the PHP standard header() and exit() then it all works, fine.

header("Location: " . $authObj->getSuccessUrl());
exit();

I have tried all the combination, but still no luck.

eg return $this->redirect()

I am not sure if its a PHP issue or something to do with YII2 session handling. There are few other screens, where I believe the session is not saving as client has logged few bugs recently related to data loss, while the code logic is fine and works locally.

Any help would be of great help.

Thanks

Try using $session->open(); to open a session first before assigning some values.

Therefore, your code should be like:

    `public function onAuthSuccess($client)
{   

    $session = Yii::$app->session;
    $session->open();//Open session.
    //Store social info in session
    $session['socialClient'] = $_GET['authclient'];

    return;
}

You said onAuthSuccess() is an action right? If yes then please use actionOnAuthSuccess(). This might be the reason you are not getting the values in the session because this action was never found.

In their example:

 public function successCallback($client)
 {
      $attributes = $client->getUserAttributes();
      // user login or signup comes here
 }

Your code:

//Store social info in session
$session['socialClient'] = $_GET['authclient'];

It's a callback function, you wouldn't get any parametes from $_GET. You may use the variable '$client' to get some data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM