| 2 | import { GoogleAuthPlugin, InitOptions, User } from './definitions'; |
| 3 | |
| 4 | export class GoogleAuthWeb extends WebPlugin implements GoogleAuthPlugin { |
| 5 | gapiLoaded: Promise<void>; |
| 6 | options: InitOptions; |
| 7 | |
| 8 | constructor() { |
| 9 | super(); |
| 10 | } |
| 11 | |
| 12 | loadScript() { |
| 13 | if (typeof document === 'undefined') { |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | const scriptId = 'gapi'; |
| 18 | const scriptEl = document?.getElementById(scriptId); |
| 19 | |
| 20 | if (scriptEl) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | const head = document.getElementsByTagName('head')[0]; |
| 25 | const script = document.createElement('script'); |
| 26 | |
| 27 | script.type = 'text/javascript'; |
| 28 | script.defer = true; |
| 29 | script.async = true; |
| 30 | script.id = scriptId; |
| 31 | script.onload = this.platformJsLoaded.bind(this); |
| 32 | script.src = 'https://apis.google.com/js/platform.js'; |
| 33 | head.appendChild(script); |
| 34 | } |
| 35 | |
| 36 | initialize( |
| 37 | _options: Partial<InitOptions> = { |
| 38 | clientId: '', |
| 39 | scopes: [], |
| 40 | grantOfflineAccess: false, |
| 41 | } |
| 42 | ) { |
| 43 | if (typeof window === 'undefined') { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | const metaClientId = (document.getElementsByName('google-signin-client_id')[0] as any)?.content; |
| 48 | const clientId = _options.clientId || metaClientId || ''; |
| 49 | |
| 50 | if (!clientId) { |
| 51 | console.warn('GoogleAuthPlugin - clientId is empty'); |
| 52 | } |
| 53 | |
| 54 | this.options = { |
| 55 | clientId, |
| 56 | grantOfflineAccess: _options.grantOfflineAccess ?? false, |
| 57 | scopes: _options.scopes || [], |
| 58 | }; |
| 59 | |
| 60 | this.gapiLoaded = new Promise((resolve) => { |
| 61 | // HACK: Relying on window object, can't get property in gapi.load callback |
nothing calls this directly
no outgoing calls
no test coverage detected