【发布时间】:2020-05-15 14:42:04
【问题描述】:
在我的网页上,我有简单的登录系统 (PHP)。您提供邮件和密码,系统创建 _SESSION 并登录。
我想添加使用 google oAuth 2.0 登录的选项。
我创建了 index.php 文件:
require_once("vendor/autoload.php");
$client = new Google_Client();
$client->addScope(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));
$redirect_uri = '/login.php';
$client->setRedirectUri($redirect_uri);
<a href="<?=$client->createAuthUrl()?>">LOGIN WITH GOOGLE</a>
用户将点击链接,在选择一个谷歌帐户后,他将被重定向到:
login.php 文件:
if(isset($_GET['code'])) {
try {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
}catch (Exception $e){
//redirect to login.php - invalid login
exit();
}
try {
$pay_load = $client->verifyIdToken();
}catch (Exception $e) {
//redirect to login.php - invalid login
exit();
}
//NOW I KNOW USER IS VALID,
//create my own _SESSION and redirect to index.php as logged user (i will use mail and name of user from google).
// i am managing _SESSION so if user logOut i will just destroy session, or will be destroyed after few minutes of inactivity
}
这种方法好吗?
我需要使用吗
setAccessToken($token);
verifyIdToken();
谢谢你的回答。
【问题讨论】:
标签: php google-oauth