Advertisement
Guest User

yii2-aut-client-auth-controller

a guest
Jan 13th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. public function actions() {
  2. return [
  3. 'auth' => [
  4. 'class' => 'yii\authclient\AuthAction',
  5. 'successCallback' => [$this, 'oAuthSuccess'],
  6. ],
  7. ];
  8. }
  9.  
  10. ...
  11. public function oAuthSuccess($client) {
  12. // get user data from client
  13. $userAttributes = $client->getUserAttributes();
  14.  
  15. $model = Users::find()->where(['social' => $_GET['authclient'], 'social_id' => $userAttributes['id']])->one();
  16. if (!$model) {
  17. $model = new Users();
  18. if($_GET['authclient'] == 'vkontakte'){
  19. $model->username = $userAttributes['screen_name'];
  20. $model->password = \Yii::$app->security->generateRandomString(6);
  21. $model->email = "";
  22. $model->first_name = $userAttributes['first_name'];
  23. $model->last_name = $userAttributes['last_name'];
  24. $model->avatar = $userAttributes['photo'];
  25. $model->social = $_GET['authclient'];
  26. $model->social_id = $userAttributes['id'];
  27. $model->is_active = 1;
  28. $model->lang_id = 1;
  29. $model->save(false);
  30. }
  31. }
  32.  
  33. $identity = UserIdentity::findIdentity($model->id);
  34. \Yii::$app->getUser()->login($identity);
  35.  
  36. // do some thing with user data. for example with $userAttributes['email']
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement