/** * 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; }
/** * 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 $userAttributes = $client->getUserAttributes(); //cuida de logar $user = User::find()->where(['=', 'email', $userAttributes['email']])->one(); if ($user != null) { Yii::$app->user->login($user); copy('http://graph.facebook.com/' . $user->username . '/picture?type=large', 'images/' . $user->username . '.jpeg'); return $this->goHome(); } else { $modelCadastro = new SignupForm(); $modelCadastro->email = $userAttributes['email']; $modelCadastro->username = $userAttributes['id']; $modelCadastro->password = $userAttributes['id']; $nome = explode(" ", $userAttributes['name']); if ($user = $modelCadastro->signup()) { $modelPerfil = new Perfil(); $modelPerfil->id = $user->id; $modelPerfil->data = date('Y-m-d'); $modelPerfil->nome = $nome[0]; $modelPerfil->sobrenome = $nome[1]; copy('http://graph.facebook.com/' . $modelCadastro->username . '/picture?type=large', 'images/' . $modelCadastro->username . '.jpeg'); $modelPerfil->foto = $modelCadastro->username . '.jpeg'; $modelPerfil->save(); if (Yii::$app->getUser()->login($user)) { return $this->goHome(); } } } //Cuida do cadastro do usuario na base // do some thing with user data. for example with $userAttributes['email'] }