Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function actions() {
- return [
- 'auth' => [
- 'class' => 'yii\authclient\AuthAction',
- 'successCallback' => [$this, 'oAuthSuccess'],
- ],
- ];
- }
- ...
- public function oAuthSuccess($client) {
- // get user data from client
- $userAttributes = $client->getUserAttributes();
- $model = Users::find()->where(['social' => $_GET['authclient'], 'social_id' => $userAttributes['id']])->one();
- if (!$model) {
- $model = new Users();
- if($_GET['authclient'] == 'vkontakte'){
- $model->username = $userAttributes['screen_name'];
- $model->password = \Yii::$app->security->generateRandomString(6);
- $model->email = "";
- $model->first_name = $userAttributes['first_name'];
- $model->last_name = $userAttributes['last_name'];
- $model->avatar = $userAttributes['photo'];
- $model->social = $_GET['authclient'];
- $model->social_id = $userAttributes['id'];
- $model->is_active = 1;
- $model->lang_id = 1;
- $model->save(false);
- }
- }
- $identity = UserIdentity::findIdentity($model->id);
- \Yii::$app->getUser()->login($identity);
- // do some thing with user data. for example with $userAttributes['email']
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement