Skip to content

Commit b22fb0a

Browse files
committed
feat: 🎸 add GoogleAuthButton scaffolding
1 parent 6b2a4ca commit b22fb0a

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {GoogleAuthButton} from '..';
4+
// import ShowDocs from '../../../.storybook/ShowDocs'
5+
6+
const clientId = '305188012168-htfit0k0u4vegn0f6hn10rcqoj1m77ca.apps.googleusercontent.com';
7+
8+
storiesOf('Context/GoogleAuthButton', module)
9+
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/GoogleAuthButton.md')}))
10+
.add('Default', () =>
11+
<GoogleAuthButton clientId={clientId}>{(state) =>
12+
<button>Sign in with Google!</button>
13+
}</GoogleAuthButton>
14+
);

‎src/GoogleAuth/index.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {Component} from 'react';
2+
import renderProp from '../util/renderProp';
3+
import {IUniversalInterfaceProps} from '../typing';
4+
5+
export interface IGoogleAuthButtonProps extends IUniversalInterfaceProps<IGoogleAuthButtonState> {
6+
clientId: string;
7+
}
8+
9+
export interface IGoogleAuthButtonState {
10+
loading?: boolean;
11+
}
12+
13+
export class GoogleAuthButton extends Component<IGoogleAuthButtonProps, IGoogleAuthButtonState> {
14+
render () {
15+
return renderProp(this.props, this.state);
16+
}
17+
}

‎src/GoogleAuth/util.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export type GoogleAPI = any;
2+
3+
let gapiCache: GoogleAPI;
4+
5+
export const getGapi = async (): Promise<GoogleAPI> => {
6+
if (gapiCache) {
7+
return gapiCache;
8+
}
9+
10+
await new Promise(resolve => {
11+
const gapicallback = `__gapicb${Date.now().toString(36)}`;
12+
(window as any)[gapicallback] = () => {
13+
delete (window as any)[gapicallback];
14+
gapiCache = (window as any).gapi;
15+
resolve();
16+
};
17+
18+
const script = document.createElement('script');
19+
20+
script.src = 'https://apis.google.com/js/platform.js?onload=' + gapicallback;
21+
document.head.appendChild(script);
22+
});
23+
24+
return gapiCache;
25+
};
26+
27+
let gapiAuth2Cache;
28+
29+
export const getGapiAuth2 = async (): Promise<any> => {
30+
if (gapiAuth2Cache) {
31+
return gapiAuth2Cache;
32+
}
33+
34+
const gapi = await getGapi();
35+
36+
await new Promise(resolve => {
37+
gapi.load('auth2', () => {
38+
gapiAuth2Cache = gapi.auth2;
39+
resolve();
40+
});
41+
});
42+
43+
return gapiAuth2Cache;
44+
};
45+
46+
let GoogleAuthCache;
47+
48+
export const getGapiAuthInstance = async (client_id: string) => {
49+
if (GoogleAuthCache) {
50+
return GoogleAuthCache;
51+
}
52+
53+
const gapiAuth2 = await getGapiAuth2();
54+
55+
GoogleAuthCache = await gapiAuth2.init({client_id});
56+
57+
return GoogleAuthCache;
58+
};

0 commit comments

Comments
 (0)