Exemplo n.º 1
0
 /**
  * This function will be triggered when user is successfuly authenticated using some oAuth client.
  *
  * @param yii\authclient\ClientInterface $client
  * @return boolean|yii\web\Response
  */
 public function oAuthSuccess($client)
 {
     // get user data from client
     //        $client->getName();
     $userAttributes = $client->getUserAttributes();
     //        print_r($client);
     //        echo "<hr>";
     //        print_r($userAttributes);
     //        exit;
     // do some thing with user data. for example with $userAttributes['email']
     $socialaccount = \app\models\SocialAccount::find()->where("client_id=:id", [":id" => $userAttributes["id"]])->one();
     if ($socialaccount) {
         //            print_r($socialaccount);
         //            exit;
         //            print_r($socialaccount->getUser());exit;
         \Yii::$app->getUser()->login($socialaccount->getUser()->one(), 0);
         \Yii::$app->session->setFlash("success", \Yii::t('app', 'Welcome'));
     } else {
         $user = \app\models\User::find()->where("email=:email", [":email" => $userAttributes["email"]])->one();
         if ($user) {
             $socialaccount = new \app\models\SocialAccount();
             $socialaccount->user_id = $user->id;
             $socialaccount->provider = "facebook";
             $socialaccount->client_id = $userAttributes["id"];
             $socialaccount->data = json_encode($userAttributes);
             $socialaccount->save();
             \Yii::$app->session->setFlash("success", \Yii::t('app', 'Facebook is connected successfully'));
         }
     }
     echo "<script>\n            window.opener.location.reload();\n            window.close();\n            </script>";
     exit;
 }
Exemplo n.º 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSocialAccounts()
 {
     return $this->hasMany(SocialAccount::className(), ['user_id' => 'id']);
 }