简体   繁体   English

CakePHP验证登录(错误的方法?)

[英]CakePHP Auth Login (Wrong Approach?)

I was using CakePHP 2.4.4, facebook_php_sdk and trying to build my own function for creating a user via facebook /linkedin login. 我正在使用CakePHP 2.4.4,facebook_php_sdk并尝试构建我自己的功能,通过facebook / linkedin登录创建用户。

As far as I understand facebook doesn't retrieve password and so I created a password with a space and hash them before save. 据我所知,facebook不会检索密码,因此我创建了一个带空格的密码,并在保存之前将其哈希。 The reason I did this because the "Auth Login" require username (which I put as email) and password(which I'm not so clear about the provided CakePHP Auth, and here might be the wrong approach for doing so). 我这样做的原因是因为“Auth Login”需要用户名(我把它作为电子邮件)和密码(我对提供的CakePHP Auth不太清楚,这可能是错误的方法)。

The problem that I facing right now is if the user go through manual creation via "abc@gmail.com" and putting in simple password for example. 我现在面临的问题是,如果用户通过“abc@gmail.com”进行手动创建并输入简单的密码,例如。 Later on if this User login via facebook that created with with the same email "abc@gmail.com", he/she will fail to login as in the database already have this email. 如果此用户通过使用相同电子邮件“abc@gmail.com”创建的Facebook登录,他/她将无法登录,因为在数据库中已经有此电子邮件。

I'm pretty sure something gone wrong down there but don't know where to start from, any guide and pointing will be greatly appreciated. 我很确定那里出了问题,但不知道从哪里开始,任何指南和指点都会非常感激。

This is how I login via facebook:- 这是我通过Facebook登录的方式: -

    $user_profile = $facebook->api('/me','GET');
    $this->request->data['User']['username'] = $user_profile['email'];
    $this->request->data['User']['password'] = ' ';
    $this->Auth->login()

User created via facebook:- 用户通过Facebook创建: -

$data = array(
  'name'     => $user_profile['name'],
  'username'    => $user_profile['email'],
  'email'    => $user_profile['email'],
  'password' => ' ',
  'create_source' => 1 //Used this to differentiate from fb or linkedin
);

$this->User->save($data);

Thanks for XuDing answers which I should do this :- 感谢XuDing的答案,我应该这样做: -

$this->Auth->login($user)

One more Scenario: What if a user wanted to do manual login where the account was initially created by Facebook Login? 还有一个场景:如果用户想要手动登录该帐户最初是由Facebook登录创建的,该怎么办?

If the username(email) already exists, you can find it using Facebook ID and login it in directly using $this->Auth->login($user). 如果用户名(电子邮件)已经存在,您可以使用Facebook ID找到它并使用$ this-> Auth-> login($ user)直接登录。 Check out this plugin code, it contains the full logic of login using Facebook: 看看这个插件代码,它包含使用Facebook登录的完整逻辑:

function __syncFacebookUser

https://github.com/webtechnick/CakePHP-Facebook-Plugin/blob/master/Controller/Component/ConnectComponent.php#LC129 https://github.com/webtechnick/CakePHP-Facebook-Plugin/blob/master/Controller/Component/ConnectComponent.php#LC129

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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