Esempio n. 1
0
 public function getUser($id)
 {
     FacebookSession::setDefaultApplication(self::$config['APPID'], self::$config['SECURITY_KEY']);
     $accessToken = \Yii::$app->session->get('USER')->token;
     $session = new FacebookSession($accessToken);
     $currentUser = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
     $user = new SocialUser();
     $user->first_name = $currentUser->asArray()['first_name'];
     $user->last_name = $currentUser->asArray()['last_name'];
     return $user;
 }
Esempio n. 2
0
 public function actionFb()
 {
     \yii::$app->session->set('1', 1);
     $fbAPI = \yii::$app->params['fbAPI'];
     FacebookSession::setDefaultApplication($fbAPI['APPID'], $fbAPI['SECURITY_KEY']);
     $helper = new FacebookRedirectLoginHelper($fbAPI['redirectURL']);
     try {
         $session = $helper->getSessionFromRedirect();
     } catch (FacebookRequestException $ex) {
         \yii::$app->response->redirect(\yii::$app->params['loginURL']);
     } catch (\Exception $ex) {
         \yii::$app->response->redirect(\yii::$app->params['loginURL']);
     }
     if (!isset($session)) {
         \yii::$app->response->redirect(\yii::$app->params['loginURL']);
     }
     $currentUser = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
     if (is_null($currentUser->getId())) {
         \yii::$app->response->redirect('/auth');
     }
     $user = User::find()->where(['site' => User::SITE_FB, 'socialid' => $currentUser->getId()])->one();
     if (!$user) {
         $user = new User();
         $user->site = User::SITE_FB;
         $user->status = User::STATUS_SOCIAL_APPROVE;
         $user->socialid = (string) $currentUser->getId();
         $user->token = $session->getAccessToken();
         $isSaved = $user->save();
         if (!$isSaved) {
             throw new Exception('Не удалось сохранить пользователя.');
         }
     } else {
         $user->token = $session->getAccessToken();
         $user->save();
     }
     \yii::$app->session->set('USER', $user);
     \yii::$app->response->redirect('/auth/checktype');
 }