Cakephp手动登录不启动会话

时间:2012-08-29 16:02:40

标签: php cakephp

我正在使用他们的fb用户ID登录用户。因此,登录用户时无需用户名或密码。这很好用。但是,当我尝试访问$this->Auth->user时,没有任何内容被保存。请参阅下面的代码。

$this->authUser = $this->User->findById($user_profile['id']);
        if($this->authUser){
            if($this->Auth->login($this->authUser)){
            $this->redirect('/Users/dash');
            }

有什么想法吗?

如果我调试$this->Auth而不是重定向,我会得到以下内容:

object(AuthComponent) {
components => array(
    (int) 0 => 'Session',
    (int) 1 => 'RequestHandler'
)
authenticate => array(
    (int) 0 => 'Form'
)
authorize => array(
    (int) 0 => 'Controller'
)
ajaxLogin => null
flash => array(
    'element' => 'default',
    'key' => 'auth',
    'params' => array()
)
loginAction => array(
    'controller' => 'users',
    'action' => 'login',
    'plugin' => null
)
loginRedirect => null
logoutRedirect => array(
    'controller' => 'users',
    'action' => 'login',
    'plugin' => null
)
authError => 'You can't access that page'
allowedActions => array(
    (int) 0 => 'index',
    (int) 1 => 'view'
)
request => object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'Users',
        'action' => 'index',
        'named' => array(),
        'pass' => array()
    )
    data => array(
        'signed_request' => 'Xyx9uGB4OZT_S59w-sMmJe5ofuzCwib2F1dGhfdG9rZW4iOiJBQUFEcDlCdlpDMzRjQkFJSlBaQ3VwVGtoTlJRRGdIWkJEU0IxeWlTZDFhRTNLcXg1TzJma2Ewek1yOUlrTkVUZTBWbGNpaUxhMFpDUWFQd0ZOWVF3MnZyU1B4NXFrV2VqMjlOMzY4NXJ2d1pEWkQiLCJwYWdlIjp7ImlkIjoiMjQ3ODg2NjY1MzMxOTgwIiwibGlrZWQiOnRydW8BPoTx2YAtQtdin0.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEzNDYyNjMyMDAsImlzc3VlZF9hdCI6MTM0NjI1NzU3NUsImFkbWluIjp0cnVlfSwidXNlciI6eyJjb3VudHJ5IjoiZ2IiLCJsb2NhbGUiOiJlbl9HQiIsImFnZSI6eyJtaW4iOjIxfX0sInVzZXJfaWQiOiI2NjEyNjMzODgifQ'
    )
    query => array()
    url => 'Users/index'
    base => ''
    webroot => '/'
    here => '/Users/index'
}
response => object(CakeResponse) {

}
settings => array(
    'authError' => 'You can't access that page',
    'authorize' => array(
        (int) 0 => 'Controller'
    )
)
Session => object(SessionComponent) {
    settings => array()
    components => array()
}

}

谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个(适用于我,我使用它在帐户激活后自动登录用户):

$user = $this->User->findById($id);
$user = $user['User'];
$this->Auth->login($user);

此外,$ this-> Auth->用户不是属性。如果要访问它,则必须编写$this->Auth->user();

答案 1 :(得分:0)

您无法登录,因为查找结果包含哈希密码。并且$this->Auth->login()方法将再次将其转换为哈希密码。 You can't decrypt the hashed password

您可以使用以下代码执行此操作:

        if($this->Auth->login($user_profile['id'])){
        $this->redirect('/users/dash');
        }

Auth :: login接受用户名/密码组合或仅接受用户ID(数据库中用户模型的主键)。