處理憑證回應
您可以在 data-callback
屬性中設定 JavaScript 函式,以便處理傳回的憑證回應。請參閱下列程式碼片段:
<div id="g_id_onload"
data-client_id="YOUR_GOOGLE_CLIENT_ID"
data-callback="handleCredentialResponse">
</div>
<script>
function handleCredentialResponse(response) {
// decodeJwtResponse() is a custom function defined by you
// to decode the credential response.
const responsePayload = decodeJwtResponse(response.credential);
console.log("ID: " + responsePayload.sub);
console.log('Full Name: ' + responsePayload.name);
console.log('Given Name: ' + responsePayload.given_name);
console.log('Family Name: ' + responsePayload.family_name);
console.log("Image URL: " + responsePayload.picture);
console.log("Email: " + responsePayload.email);
}
</script>
您可以使用適用於您語言的 JWT 解碼程式庫來驗證及解碼 JWT 憑證。建議您也在伺服器端驗證 Google ID 權杖。