Nếu muốn, bạn chỉ có thể sử dụng mã JavaScript để kích hoạt lời nhắc Một lần nhấn hoặc hiển thị nút Đăng nhập bằng Google.
Để định cấu hình lời nhắc Một lần nhấn trong JavaScript, trước tiên, bạn cần gọi phương thức initialize()
. Sau đó, hãy gọi phương thức prompt()
để hiển thị giao diện người dùng của lời nhắc. Hãy xem đoạn mã sau:
<script>
window.onload = function () {
google.accounts.id.initialize({
client_id: 'YOUR_GOOGLE_CLIENT_ID ',
callback: handleCredentialResponse
});
google.accounts.id.prompt();
}
</script>
Để nhận thông báo nhanh về trạng thái giao diện người dùng, hãy cung cấp một hàm gọi lại cho phương thức prompt()
. Hãy xem đoạn mã sau:
<script>
window.onload = function () {
google.accounts.id.initialize({
client_id: 'YOUR_GOOGLE_CLIENT_ID ',
callback: handleCredentialResponse
});
google.accounts.id.prompt((notification) => {
if (notification.isNotDisplayed() || notification.isSkippedMoment()) {
// try next provider if OneTap is not displayed or skipped
}
});
}
</script>
Mã ví dụ bên dưới cho thấy cách hiển thị cả tính năng Một lần nhấn và nút Đăng nhập bằng Google trong JavaScript.
<script>
window.onload = function () {
google.accounts.id.initialize({
client_id: 'YOUR_GOOGLE_CLIENT_ID ',
callback: handleCredentialResponse
});
const parent = document.getElementById('google_btn');
google.accounts.id.renderButton(parent, {theme: "filled_blue"});
google.accounts.id.prompt();
}
</script>