| 1 |
import { useState } from 'react' |
| 2 |
import { useConfig } from '../auth' |
| 3 |
import { authenticateByToken } from '../lib/allauth' |
| 4 |
|
| 5 |
function installGoogleOneTap (cb) { |
| 6 |
const id = 'google-one-tap' |
| 7 |
const scr = document.getElementById(id) |
| 8 |
if (!scr) { |
| 9 |
const scr = document.createElement('script') |
| 10 |
scr.id = id |
| 11 |
scr.src = '//accounts.google.com/gsi/client' |
| 12 |
scr.async = true |
| 13 |
scr.addEventListener('load', function () { |
| 14 |
cb() |
| 15 |
}) |
| 16 |
document.body.appendChild(scr) |
| 17 |
} else { |
| 18 |
cb() |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
export default function GoogleOneTap (props) { |
| 23 |
const config = useConfig() |
| 24 |
const [enabled, setEnabled] = useState(() => window.sessionStorage.getItem('googleOneTapEnabled') === 'yes') |
| 25 |
function onGoogleOneTapInstalled () { |
| 26 |
const provider = config.data.socialaccount.providers.find(p => p.id === 'google') |
| 27 |
if (provider && window.google) { |
| 28 |
function handleCredentialResponse (token) { |
| 29 |
authenticateByToken(provider.id, { |
| 30 |
id_token: token.credential, |
| 31 |
client_id: provider.client_id |
| 32 |
}, props.process) |
| 33 |
} |
| 34 |
|
| 35 |
window.google.accounts.id.initialize({ |
| 36 |
client_id: provider.client_id, |
| 37 |
callback: handleCredentialResponse |
| 38 |
}) |
| 39 |
window.google.accounts.id.prompt() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
if (enabled) { |
| 44 |
installGoogleOneTap(onGoogleOneTapInstalled) |
| 45 |
return null |
| 46 |
} |
| 47 |
function enable () { |
| 48 |
window.sessionStorage.setItem('googleOneTapEnabled', 'yes') |
| 49 |
installGoogleOneTap(onGoogleOneTapInstalled) |
| 50 |
setEnabled(true) |
| 51 |
} |
| 52 |
return ( |
| 53 |
<div className='alert alert-danger d-flex justify-content-between'> |
| 54 |
<span><strong>Mind your privacy:</strong> Do you want to demo Google One Tap sign-in?</span> |
| 55 |
<button onClick={() => enable()} className='btn btn-sm btn-danger'>Enable</button> |
| 56 |
</div> |
| 57 |
) |
| 58 |
} |