保持 google 当前用户跨路由登录

Keep google current user logged accross routes

我尝试为用户系统制作一个使用 google api 的网络应用程序。

实际上,我只能将我的 google 帐户连接到一条路线,这里是 :

$app->get('/home', function () use($client,$app){
    if(isset($_GET['code'])){
        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
    }
    if(!isset($_SESSION['token'])){
        $url = $client->createAuthUrl();
        $output = '<a href="'.$url.'">Se connecter </a>';
    } else {
        $client->setAccessToken($_SESSION['token']);
        $token = json_decode($_SESSION['token']['access_token']);
        $output = "";
        require "../view/planning.php";
    }

    return $output;
});

这是我的 $client :

$client = new Google_Client();
$client->setApplicationName("Application de test");
$client->setClientId(MY_CLIENT_ID);
$client->setClientSecret(MY_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/calendar.readonly');
$client->addScope('https://www.googleapis.com/auth/userinfo.email');
$client->setRedirectUri(MY_REDIRECT_URI);
$client->setAccessType('online');

我的问题是:我如何才能让当前的客户端访问整个应用程序? 例如,如果我有这条路线:

$app->get('/accueil', function () use($client){
  if(isset($_GET['code'])){
      $client->authenticate($_GET['code']);
      $_SESSION['token'] = $client->getAccessToken();
  }
  if(!isset($_SESSION['token'])){
      $url = $client->createAuthUrl();
      $output = '<a href="'.$url.'">Se connecter </a>';
  } else {
      $client->setAccessToken($_SESSION['token']);
      $token = json_decode($_SESSION['token']['access_token']);
      require ('../view/accueil.php');
      $output = "";
  }
  return $output;
});

但是这条路线不起作用,它告诉我 link 登录。我了解到应用程序无法从 url 中获取 'code',因为什么都没有。

我也试过这样保持 'code' :

if(isset($_GET['code']) || isset($_SESSION['code'])){
    $client->authenticate($_GET['code']);
    $_SESSION['code'] = $_GET['code'];
    $_SESSION['token'] = $client->getAccessToken();
}

如何让用户保持登录状态?

好吧,经过漫长的一夜,我找到了解决办法。

127.0.0.1 上的重定向似乎与重定向到本地主机不同,因此会话变量已重置。