public function onAuthSuccess($client) { $sourceName = ['qq' => 'qq', 'weibo' => '微博', 'weixin' => '微信', 'baidu' => '百度']; $me = Yii::$app->getUser(); $source = $client->getId(); $attr = $client->getUserAttributes(); $auth = Auth::findOne(['source' => $source, 'source_id' => (string) $attr['id']]); if ($me->getIsGuest()) { if ($auth) { // login $user = $auth->user; $me->login($user); } else { // signup $attr['source'] = $source; // $attr['sourceName'] = $client->defaultName(); $attr['sourceName'] = $sourceName[$source]; $session = Yii::$app->getSession(); $session->set('authInfo', $attr); return $this->redirect(['auth-bind-account']); } } else { // user already logged in if (!$auth) { // add auth provider $auth = new Auth(['user_id' => Yii::$app->getUser()->id, 'source' => $source, 'source_id' => (string) $attr['id']]); $auth->save(); } } }
/** * Finds the Auth model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Auth the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Auth::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }