$this->layout = null; $this->Auth->allow('authenticate', 'endpoint'); } public function authenticate($provider = null) { //try { $hybridauth = new Hybridauth(APP . 'Config' . DS . 'oauth.php'); $adapter = $hybridauth->authenticate($provider); $profile = $adapter->getUserProfile(); if (!$this->Auth->user()) { $auth = $this->Authentication->find('first', array('conditions' => array('Authentication.provider' => strtolower($adapter->getProviderId()), 'Authentication.identifier' => $profile->getIdentifier()), 'contain' => array('User'))); if (!empty($auth)) { $this->Auth->login($auth['User']); return $this->redirect($this->Auth->redirectUrl()); } else { if (($id = $this->User->register($adapter, User::REGISTER_SOCIAL)) != false) { $auth = $this->User->find('first', array('conditions' => array('User.id' => $id))); $this->Auth->login($auth['User']); return $this->redirect($this->Auth->redirectUrl()); } } } else { $user = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')), 'contain' => array('Authentication'))); if (!$this->Authentication->hasAny(array('Authentication.provider' => strtolower($adapter->getProviderId()), 'Authentication.identifier' => $profile->getIdentifier())) && !in_array(strtolower($adapter->getProviderId()), Set::extract('{n}.provider', $user['Authentication']))) { $auth = array($this->Authentication->alias => array('user_id' => $this->Auth->user('id'), 'provider' => strtolower($adapter->getProviderId()), 'identifier' => $profile->getIdentifier(), 'display_name' => $profile->getDisplayName(), 'email' => $profile->getEmail(), 'avatar' => $profile->getPhotoURL())); $this->Authentication->create(); $this->Authentication->save($auth); } return $this->redirect($this->referer()); } //} catch (Exception $e){
public function socialAction() { $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REDIRECT_URL']; $config = $this->getServiceLocator()->get('Config'); //var_dump($config);exit; $config = array("base_url" => $url, "providers" => array("Google" => array("enabled" => true, "keys" => array("id" => "131581937601-sj40188ms1s9kuv20ff7j54kcunbclkg.apps.googleusercontent.com", "secret" => "54sj6EaYfwxQ0d8lwHsoEuQl")), "Facebook" => array("enabled" => true, "keys" => array("id" => "869805579739468", "secret" => "57865f2e5be88ba15ee4e581d91367af"), "scope" => "email, user_about_me, user_birthday, user_hometown", "trustForwarded" => false)), "debug_mode" => true, "debug_file" => ""); //var_dump($_REQUEST);exit; if ($provider = $this->getRequest()->getQuery('provider')) { try { // create an instance for Hybridauth with the configuration file path as parameter //$hybridauth = new Hybridauth($config['hybrid_auth']); $hybridauth = new Hybridauth($config); // try to authenticate the user with twitter, // user will be redirected to Twitter for authentication, // if he already did, then Hybridauth will ignore this step and return an instance of the adapter $auth = $hybridauth->authenticate($provider); //var_dump($auth); exit; // get the user profile $profile = $auth->getUserProfile(); // Create new user by social profile $translator = $this->getTranslator(); $entityManager = $this->getEntityManager(); $user = $entityManager->getRepository('User\\Entity\\User')->findOneBy(array('email' => $profile->getEmail())); if ($user) { $auth->logout(); $user->authenticate(); $this->redirect()->toUrl('/user/dashboard'); } else { // TODO: redirect to register social action $this->flashMessenger()->addInfoMessage($translator->translate('You have to register by your social account first.')); $this->redirect()->toUrl('/user/register'); } } catch (Exception $e) { // Display the recived error, // to know more please refer to Exceptions handling section on the userguide $translator = $this->getTranslator(); $this->flashMessenger()->addErrorMessage($translator->translate('Cannot login by social account.')); } } /* var_dump($_REQUEST);exit; $endpoint = new Endpoints(); $endpoint->process(); */ if (isset($_REQUEST['hauth_start']) || isset($_REQUEST['hauth_done'])) { //echo 'helo';exit; $endpoint = new Endpoint(); $endpoint->process(); } return new ViewModel(); }
/** * login using twitter */ public function loginAction() { $provider = $this->params()->fromRoute('provider'); try { $backend = $this->authenticator->authenticate($provider); if (!$backend->isAuthorized()) { throw new \UnexpectedValueException('User is not connected'); } $profile = $backend->getUserProfile(); // error_log(print_R($this->userWrapperFactory->factory($profile),true)); $this->session->offsetSet('authenticated', $backend->isAuthorized()); $this->session->offsetSet('user', $this->userWrapperFactory->factory($profile)); $this->session->offsetSet('backend', $backend); } catch (\Exception $e) { $this->session->offsetSet('authenticated', false); } return $this->doRedirect(); }
public function socialAction() { $request = $this->getRequest(); $userType = $request->getQuery('type'); $provider = $request->getQuery('provider'); $config = $this->getServiceLocator()->get('Config'); if ($provider) { try { // create an instance for Hybridauth with the configuration file path as parameter $hybridauth = new Hybridauth($config['hybrid_auth']); // try to authenticate the user with twitter, // user will be redirected to Twitter for authentication, // if he already did, then Hybridauth will ignore this step and return an instance of the adapter $auth = $hybridauth->authenticate($provider); // get the user profile $profile = $auth->getUserProfile(); // Create new user by social profile $translator = $this->getTranslator(); $entityManager = $this->getEntityManager(); $user = $entityManager->getRepository('User\\Entity\\User')->findOneBy(array('email' => $profile->getEmail())); if ($user) { $auth->logout(); $user->authenticate(); } else { $auth->logout(); $newUser = new User(); $newUser->createUserBySocialProfile($this, $profile, $userType); } $this->redirect()->toUrl('/' . $lang_code . '/user/dashboard'); } catch (Exception $e) { // Display the recived error, // to know more please refer to Exceptions handling section on the userguide $translator = $this->getTranslator(); $this->flashMessenger()->addErrorMessage($translator->translate('Cannot login by social account.')); } } if (isset($_REQUEST['hauth_start']) || isset($_REQUEST['hauth_done'])) { (new Endpoint())->process(); } return new ViewModel(); }
/** * @param string $providerId * @param array $parameters * * @return mixed */ public function authenticate($providerId, array $parameters = array()) { return $this->hybridAuth->authenticate($providerId, $parameters); }