如果重定向,则在CakePHP中手动登录后未登录,如果没有重定向则登录

时间:2010-09-21 17:14:08

标签: cakephp janrain

我正在使用Janrain参与登录我的CakePHP网站,在处理用户数据时,我想使用$ this-> Auth-> login() - 函数自动登录。

如果我在通话结束后没有重定向,我设法登录正常,但是如果我重定向,我就没有登录。现在有人为什么或者我可以做些什么来扼杀这个?

    function janrain(){
    $rpxApiKey = 'kassdkfkafkkadskfkkdfkksdk';  

    if(isset($_POST['token'])) { 

      /* STEP 1: Extract token POST parameter */
      $token = $_POST['token'];

      /* STEP 2: Use the token to make the auth_info API call */
      $post_data = array('token' => $_POST['token'],
                         'apiKey' => $rpxApiKey,
                         'format' => 'json'); 

      $curl = curl_init();
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_URL, 'https://rpxnow.com/api/v2/auth_info');
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
      curl_setopt($curl, CURLOPT_HEADER, false);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      $raw_json = curl_exec($curl);
      curl_close($curl);


      /* STEP 3: Parse the JSON auth_info response */
      $auth_info = json_decode($raw_json, true);

      if ($auth_info['stat'] == 'ok') {

        /* STEP 3 Continued: Extract the 'identifier' from the response */
        $profile = $auth_info['profile'];
        $identifier = $profile['identifier'];

        if (isset($profile['photo']))  {
          $photo_url = $profile['photo'];
        }

        if (isset($profile['displayName']))  {
          $name = $profile['displayName'];
        }

        if (isset($profile['email']))  {
          $email = $profile['email'];
        }

        $user = $this->User->findByUsername($identifier);

        if($user){

            $this->Auth->login($user['User']);
            if ($this->Session->read('Auth.User')) {
                    $this->Session->setFlash('You are logged in!');
                    $this->redirect('/', null, false);
                }
        }
        else{
          $this->User->create();
          $this->User->set('username',$identifier);
          $this->User->set('displayname',$name);
          if(isset($photo_url)){

            $this->User->set('photo_url', $photo_url);
          }
          $this->User->set('password', $this->Auth->password($identifier));
          $this->User->save();
          //$this->User->set('password', $identifier);
          $this->Auth->login($this->User);          
        }

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。但是我无法解决这个问题。尝试覆盖PagesController上的beforeFilter(如果你正在使用它)并在其中添加parent :: beforeFilter。

public function beforeFilter() {
    parent::beforeFilter();
}

然而,这并没有解决我的问题。最终我放弃了尝试。安装OPAuth,遇到了几个问题,但解决了它们。 Facebook,Twitter,谷歌等,现在可以正常工作,并与我网站的内置身份验证系统集成。

链接:OPAuth WebsiteOPAuth DownloadOPAuth CakePHP Plugin

答案 1 :(得分:0)

我遇到了同样的麻烦。如果我重定向,则不验证用户。

我到目前为止找到的唯一解决方案是在验证用户后使用JavaScript重定向。我将url作为参数重定向到嵌入小部件中定义的令牌URL:

$url = urlencode($baseUrl.'users/rpx?redirect=' . 'lala');

<iframe src="http://lolo.rpxnow.com/openid/embed?token_url=<?php echo $url; ?>" scrolling="no" frameBorder="no" allowtransparency="true" style="width:350px;height:216px"></iframe>

<iframe src="http://lolo.rpxnow.com/openid/embed?token_url=<?php echo $url; ?>" scrolling="no" frameBorder="no" allowtransparency="true" style="width:350px;height:216px"></iframe>

答案 2 :(得分:0)

我真的不喜欢CakePHP的Auth组件。我遇到了与CakePHP 1.2完全相同的问题,但是通过将core.php文件中的安全级别更改为“低”来设法使其工作正常。

Configure::write('Security.level', 'low');
相关问题