Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function onAuthSuccess(ClientInterface $client)
- {
- return;
- $attributes = $client->getUserAttributes();
- $email = ArrayHelper::getValue($attributes, 'email');
- $id = ArrayHelper::getValue($attributes, 'id');
- #$nickname = ArrayHelper::getValue($attributes, 'login');
- #Y::dump($_REQUEST, false);
- #Y::dump($attributes, true);
- $email = null;
- /* @var Auth $auth */
- $auth = Auth::find()->where([
- 'source' => $client->getId(),
- 'source_id' => $id,
- ])->one();
- if (Yii::$app->user->isGuest) {
- if ($auth) { // login
- /* @var User $user */
- $user = $auth->user;
- #$this->updateUserInfo($user);
- Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
- } else { // signup
- if ($email === null) {
- Y::app()->getSession()->set('emptyEmail', ArrayHelper::getValue($attributes, 'User'));
- Yii::$app->getSession()->setFlash('error', 'User with the same email already exists but isn\'t linked to it. Login using email first to link it.');
- } elseif ($user = User::findOne(['email' => $email])) {
- $transaction = User::getDb()->beginTransaction();
- $auth = new Auth([
- 'user_id' => $user->id,
- 'source' => $client->getId(),
- 'source_id' => (string)$id,
- ]);
- if ($auth->save()) {
- $transaction->commit();
- Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
- } else {
- Yii::$app->getSession()->setFlash('error', 'Unable to save, errors: ' . json_encode($auth->getErrors()));
- }
- } else {
- #Y::dump(Yii::$app->params, true);
- $password = Yii::$app->security->generateRandomString(6);
- $user = new User([
- #'username' => $nickname,
- #'github' => $nickname,
- 'email' => $email,
- 'password' => $password,
- ]);
- $user->generateAuthKey();
- $user->generatePasswordResetToken();
- $transaction = User::getDb()->beginTransaction();
- if ($user->save()) {
- $auth = new Auth([
- 'user_id' => $user->id,
- 'source' => $client->getId(),
- 'source_id' => (string)$id,
- ]);
- if ($auth->save()) {
- $transaction->commit();
- Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
- } else {
- Yii::$app->getSession()->setFlash('error', [
- Yii::t('app', 'Unable to save {client} account: {errors}', [
- 'client' => $client->getTitle(),
- 'errors' => json_encode($auth->getErrors()),
- ]),
- ]);
- }
- } else {
- Yii::$app->getSession()->setFlash('error', [
- Yii::t('app', 'Unable to save user: {errors}', [
- 'client' => $client->getTitle(),
- 'errors' => json_encode($user->getErrors()),
- ]),
- ]);
- }
- }
- }
- } else { // user already logged in
- if (!$auth) { // add auth provider
- Y::dump($attributes, true);
- $auth = new Auth([
- 'user_id' => Yii::$app->user->id,
- 'source' => $client->getId(),
- 'source_id' => (string)$attributes['id'],
- ]);
- if ($auth->save()) {
- /** @var User $user */
- $user = $auth->user;
- #$this->updateUserInfo($user);
- Yii::$app->getSession()->setFlash('success', [
- Yii::t('app', 'Linked {client} account.', [
- 'client' => $client->getTitle()
- ]),
- ]);
- } else {
- Yii::$app->getSession()->setFlash('error', [
- Yii::t('app', 'Unable to link {client} account: {errors}', [
- 'client' => $client->getTitle(),
- 'errors' => json_encode($auth->getErrors()),
- ]),
- ]);
- }
- } else { // there's existing auth
- Yii::$app->getSession()->setFlash('error', [
- Yii::t('app',
- 'Unable to link {client} account. There is another user using it.',
- ['client' => $client->getTitle()]),
- ]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement