Skip to content

Instantly share code, notes, and snippets.

@ioness
Created October 10, 2018 19:30
Show Gist options
  • Save ioness/8553daf63792b7b8d531d03fc02d6c6e to your computer and use it in GitHub Desktop.
Save ioness/8553daf63792b7b8d531d03fc02d6c6e to your computer and use it in GitHub Desktop.
private function _doSocialLogin($profile, $returning = false) {
$user_exist = !empty($profile['SocialProfile']['user_id']) ? $this->User->findById($profile['SocialProfile']['user_id']) : null;
if ($user_exist && $user_exist['User']['is_registered'] == 1) {
if ($user_exist['User']['is_active'] == 1) {
if ($this->Auth->login($user_exist['User'])) {
if ($this->Auth->login()) {
// is_logged mantiene constancia de si un usuario tiene sesion abierta o no
// Update islogged
$this->loadModel('User');
$this->User->id = $this->Auth->user('id');
$this->User->saveField('is_logged', true);
if ($this->Auth->user('logged') === null) {
//Chipirones por Registrarse
$this->Chipirones->addCredit('signin', $this->Auth->user('id'));
$this->Notifications->notify(1, 'Welcome', 'Welcome To Studentfy!');
$this->Chipirones->addAchievenment('signin', $this->Auth->user('id'));
$this->Notifications->notify(2, 'Add Chipirones', 'We give you +100 Chipirones for signing up on our platform!');
// anyadir carrito de compra
$this->ShoppingCart->createShoppingCart($this->Auth->user('id'));
$this->loadModel('SharesUser');
$inv_reg = $this->SharesUser->find('first', [
'conditions' => [
'email_of_registration' => $this->Auth->user('email'),
'registered' => 1,
'paid' => 0
]
]);
$this->loadModel('User');
$this->User->isLogged($this->Auth->user('id'));
$this->Session->write('NewUser', true); // To know the new user
}
return $this->redirect('/');
}
} else {
$this->Flash->set(__('Unknown Error could not verify the user: '. $this->Auth->user('username')));
$this->redirect('/');
}
} else {
if ($user_exist['User']['is_active'] == 2) {
$this->Flash->set(__('Your account has been canceled. Please contact the site administrator.'));
return $this->redirect('/');
} else {
$this->Flash->set(__('Your account has not been verified, '. $user_exist['User']['fullname'] .'. Please contact the site administrator.'));
return $this->redirect('/');
}
}
} else {
$this->Session->write(
'userProfile',
$profile
);
$this->redirect(array('controller' => 'accounts', 'action' => 'register'));
}
}
public function social_endpoint($provider = null) {
$this->Hybridauth->processEndpoint();
}
// Aqui viene la peticion cuando el usuario le da click a "Logerse con facebook"
public function social_login($provider, $count = 0) {
$this->isLoggin();
$this->set([
'provider' => $provider
]);
try {
if( $this->Hybridauth->connect($provider) ){
$exist_native = $this->User->find('first',[
'conditions' => [
'email' => $this->Hybridauth->user_profile['SocialProfile']['email'],
'login_type_id' => 1
]
]);
// valida si ya posee registro
if (empty($exist_native)) {
$this->_successfulHybridauth($provider,$this->Hybridauth->user_profile);
}
else {
/*$this->Flash->set('The email \''.$this->Hybridauth->user_profile['SocialProfile']['email'].'\' has been registered by native login. Please enter email and password to log in.
If you forget your password, click on the \'forgot password\' link.',['element' => 'error']);*/
$this->Flash->set('The email \''.$this->Hybridauth->user_profile['SocialProfile']['email'].'\' has been registered by native login. Please enter email and password to log in.
If you forget your password, click on the \'forgot password\' link.',['element' => 'error']);
$this->redirect($this->Auth->loginAction);
}
}
else{
// error
$this->Flash->set($this->Hybridauth->error);
$this->redirect($this->Auth->loginAction);
}
} catch (Exception $e) {
++ $count;
if ($count <= 10) {
$this->Flash->set(__('Failed to connect to provider '. $provider. '. Please try again later.'),['element' => 'error']);
$this->redirect($this->Auth->loginAction);
}
else {
$this->social_login($provider, $count);
}
}
}
private function _successfulHybridauth($provider, $incomingProfile){
// #1 - check if user already authenticated using this provider before
$this->SocialProfile->recursive = -1;
$existingProfile = $this->SocialProfile->find('first', array(
'conditions' => array('social_network_id' => $incomingProfile['SocialProfile']['social_network_id'], 'social_network_name' => $provider)
));
if (!empty($existingProfile)) {
if (empty($existingProfile['SocialProfile']['user_id'])) {
$this->_doSocialLogin($existingProfile);
}
else {
// #2 - if an existing profile is available, then we set the user as connected and log them in
$user = $this->User->find('first', array(
'conditions' => array('id' => $existingProfile['SocialProfile']['user_id'])
));
if (empty($user)) {
// Tiene un user_id asignado pero esta mal
}
$this->_doSocialLogin($existingProfile);
}
}
else {
if ($this->Auth->loggedIn()) {
$this->redirect($this->Auth->redirectUrl());
} else {
if($this->SocialProfile->save($incomingProfile)) {
// el perfil se guardo correctamente
}
$this->_doSocialLogin($incomingProfile);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment