简体   繁体   中英

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.

As far as I understand facebook doesn't retrieve password and so I created a password with a space and hash them before save. 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).

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. 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.

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:-

    $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:-

$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 :-

$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?

If the username(email) already exists, you can find it using Facebook ID and login it in directly using $this->Auth->login($user). Check out this plugin code, it contains the full logic of login using Facebook:

function __syncFacebookUser

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

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