|
| 1 | +import {h} from '../util'; |
| 2 | +import {Component, createContext} from 'react'; |
| 3 | +import { |
| 4 | + getGapiAuthInstance, |
| 5 | + GApiAuth2InitOptions, |
| 6 | + GApiAuth2Instance, |
| 7 | + GApiAuth2InstanceIsSignedInListener, |
| 8 | + GApiAuth2InstanceCurrentUserListener, |
| 9 | + GApiAuth2User |
| 10 | +} from './gapi'; |
| 11 | + |
| 12 | +export interface IGoogleAuthProviderProps { |
| 13 | + children; |
| 14 | +} |
| 15 | + |
| 16 | +export interface IGoogleAuthProviderState { |
| 17 | + loading: boolean; |
| 18 | + signIn: () => Promise<GApiAuth2User>; |
| 19 | + signOut: () => Promise<void>; |
| 20 | + user: GApiAuth2User | null; |
| 21 | + isSignedIn: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +export interface IGoogleAuthConsumerProps { |
| 25 | + children: (state: IGoogleAuthProviderState) => React.ReactNode; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @param clientId Google App client ID. |
| 30 | + * @param scope Scopes as a comma separated string. |
| 31 | + */ |
| 32 | +export const createGoogleAuthContext = (options: GApiAuth2InitOptions) => { |
| 33 | + const context = createContext({}); |
| 34 | + const googleAuthPromise = getGapiAuthInstance(options); |
| 35 | + let googleAuthInstance: GApiAuth2Instance | undefined; |
| 36 | + let isSignedInListener: GApiAuth2InstanceIsSignedInListener | undefined; |
| 37 | + let currentUserListener: GApiAuth2InstanceCurrentUserListener | undefined; |
| 38 | + |
| 39 | + googleAuthPromise.then((instance) => { |
| 40 | + googleAuthInstance = instance; |
| 41 | + googleAuthInstance.isSignedIn.listen((isSignedIn) => { |
| 42 | + if (isSignedInListener) isSignedInListener(isSignedIn); |
| 43 | + }); |
| 44 | + googleAuthInstance.currentUser.listen((user) => { |
| 45 | + if (currentUserListener) currentUserListener(user); |
| 46 | + }); |
| 47 | + }, console.error); |
| 48 | + |
| 49 | + class Provider extends Component<IGoogleAuthProviderProps, IGoogleAuthProviderState> { |
| 50 | + signIn = async (): Promise<GApiAuth2User> => { |
| 51 | + if (!googleAuthInstance) { |
| 52 | + googleAuthInstance = await googleAuthPromise; |
| 53 | + } |
| 54 | + |
| 55 | + return await googleAuthInstance.signIn(); |
| 56 | + }; |
| 57 | + |
| 58 | + signOut = async (): Promise<void> => { |
| 59 | + if (!googleAuthInstance) { |
| 60 | + googleAuthInstance = await googleAuthPromise; |
| 61 | + } |
| 62 | + |
| 63 | + await googleAuthInstance.signOut(); |
| 64 | + }; |
| 65 | + |
| 66 | + onIsSignedIn: GApiAuth2InstanceIsSignedInListener = (isSignedIn) => { |
| 67 | + if (isSignedIn) { |
| 68 | + // Don't do anything here, because this case will be handled by |
| 69 | + // this.onCurrentUser method. To prevent double re-render. |
| 70 | + } else { |
| 71 | + this.setState({ |
| 72 | + isSignedIn: false, |
| 73 | + user: null, |
| 74 | + }); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + onCurrentUser: GApiAuth2InstanceCurrentUserListener = (user) => { |
| 79 | + // Only handle the case when user signs in. The other case should |
| 80 | + // be handled by this.onIsSignedIn. To prevent double re-render. |
| 81 | + if (user.isSignedIn()) { |
| 82 | + this.setState({ |
| 83 | + isSignedIn: true, |
| 84 | + user |
| 85 | + }); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + state: IGoogleAuthProviderState = { |
| 90 | + loading: !googleAuthInstance, |
| 91 | + signIn: this.signIn, |
| 92 | + signOut: this.signOut, |
| 93 | + user: null, |
| 94 | + isSignedIn: false, |
| 95 | + }; |
| 96 | + |
| 97 | + async componentDidMount () { |
| 98 | + if (!googleAuthInstance) { |
| 99 | + googleAuthInstance = await googleAuthPromise; |
| 100 | + } |
| 101 | + |
| 102 | + isSignedInListener = this.onIsSignedIn; |
| 103 | + currentUserListener = this.onCurrentUser; |
| 104 | + |
| 105 | + const isSignedIn = googleAuthInstance.isSignedIn.get(); |
| 106 | + |
| 107 | + this.setState({ |
| 108 | + loading: false, |
| 109 | + isSignedIn, |
| 110 | + user: isSignedIn ? googleAuthInstance.currentUser.get() : null, |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + componentWillUnmount () { |
| 115 | + if (isSignedInListener === this.onIsSignedIn) |
| 116 | + isSignedInListener = undefined; |
| 117 | + if (currentUserListener === this.onCurrentUser) |
| 118 | + currentUserListener = undefined; |
| 119 | + } |
| 120 | + |
| 121 | + render () { |
| 122 | + return h(context.Provider, {value: this.state}, this.props.children); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return { |
| 127 | + Provider, |
| 128 | + Consumer: context.Consumer as React.SFC<IGoogleAuthConsumerProps>, |
| 129 | + }; |
| 130 | +}; |
0 commit comments