login with account cookie
[pharext/pharext.org] / app / Controller / Github / Callback.php
index d6d11920c6bd14ab3ecd465356536b1b49e94175..e6429fcf7de18f57ef2b313fa2247224b75b7cc4 100644 (file)
@@ -4,14 +4,13 @@ namespace app\Controller\Github;
 
 use app\Controller\Github;
 use app\Github\API;
-use app\Github\Exception;
 use app\Model\Accounts;
 use app\Session;
 use app\Web;
+use http\Cookie;
 
 class Callback extends Github
 {
-       
        /**
         * @var Accounts
         */
@@ -28,43 +27,40 @@ class Callback extends Github
                                "error" => $this->app->getRequest()->getQuery("error_description")
                        ]);
                } else {
-                       try {
-                               $this->github->fetchToken(
-                                       $this->app->getRequest()->getQuery("code"),
-                                       $this->app->getRequest()->getQuery("state"),
-                                       function($token) {
-                                               $this->github->setToken($token->access_token);
-                                               $this->github->fetchUser($this->createUserCallback($token));
-                               })->send();
-                               if (isset($this->session->returnto)) {
-                                       $returnto = $this->session->returnto;
-                                       unset($this->session->returnto);
-                                       $this->app->redirect($returnto);
-                               } else {
-                                       $this->app->redirect(
-                                               $this->app->getBaseUrl()->mod("./github"));
-                               }
-                       } catch (Exception $exception) {
-                               $this->app->getView()->addData(compact("exception"));
+                       $this->github->fetchToken(
+                               $this->app->getRequest()->getQuery("code"),
+                               $this->app->getRequest()->getQuery("state"),
+                               function($token) {
+                                       $this->github->setToken($token->access_token);
+                                       $this->github->readAuthUser($this->createUserCallback($token));
+                       })->send();
+                       if (isset($this->session->returnto)) {
+                               $returnto = $this->session->returnto;
+                               unset($this->session->returnto);
+                               $this->app->redirect($returnto);
+                       } else {
+                               $this->app->redirect(
+                                       $this->app->getBaseUrl()->mod("./github"));
                        }
                }
                $this->app->display("github/callback");
        }
        
-       function createUserCallback($token) {
-               return function($user) use($token) {
+       function createUserCallback($oauth) {
+               return function($user) use($oauth) {
                        $tx = $this->accounts->getConnection()->startTransaction();
-                       
-                       if (!($account = $this->accounts->byOAuth("github", $token->access_token, $user->login))) {
-                               $account = $this->accounts->createOAuthAccount("github", $token->access_token, $user->login);
+
+                       if (($cookie = $this->app->getRequest()->getCookie("account"))) {
+                               $account = $this->accounts->find(["account=" => $cookie])->current();
+                       } elseif (!($account = $this->accounts->byOAuth("github", $oauth->access_token, $user->login))) {
+                               $account = $this->accounts->createOAuthAccount("github", $oauth->access_token, $user->login);
                        }
-                       $account->updateToken("github", $token->access_token, $token);
+                       $token = $account->updateToken("github", $oauth->access_token, $oauth);
                        $owner = $account->updateOwner("github", $user->login, $user);
                        
                        $tx->commit();
                        
-                       $this->session->account = $account->account->get();
-                       $this->session->github = (object) $owner->export();
+                       $this->login($account, $token, $owner);
                };
        }
 }