diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index 08331e7..fb16ab2 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -54,22 +54,40 @@ class AuthController extends ControllerBase // NOTE: Should pass $state here also. $data = $client->authenticate($code); - $result = $this->auth->loginOauth($data); + // If user is authed already, we connect. + $user = $this->auth->getUser(); + if ($user) { - // There was an error when creating the account - if (is_array($result)) { - $msg = ''; - foreach($result as $message) { - $msg .= '
  • ' . $message->getMessage() . '
  • '; + $name = ucfirst($provider_name); + $user->{'set' . $name . 'Id'}($data->getId()); + $user->save(); + + $this->flash->message('success', "{$name} was connected!"); + $this->response->redirect('/settings'); + } + // Perform Auth. + else { + $result = $this->auth->loginOauth($data); + + // There was an error when creating the account + if (is_array($result)) { + $msg = ''; + foreach ($result as $message) { + $msg .= '
  • ' . $message->getMessage() . '
  • '; + } + $this->flash->message('error', "Failed to create account: "); + $this->response->redirect('/login'); + } else { + $this->response->redirect('/'); } - $this->flash->message('error', "Failed to create account: "); - $this->response->redirect('/login'); - } else { - $this->response->redirect('/'); } } catch(\Exception $e) { $this->flash->message('error', 'Failed to authenticate.'); - $this->response->redirect('/login'); + if ($this->auth->getUser()) { + $this->response->redirect('/settings'); + } else { + $this->response->redirect('/login'); + } } } // No code