/* * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is located at * * http://aws.amazon.com/apache2.0/ * * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ import * as React from 'react'; import { I18n, ConsoleLogger as Logger } from '@aws-amplify/core'; import { Auth } from '@aws-amplify/auth'; import AmplifyTheme from '../../Amplify-UI/Amplify-UI-Theme'; import { googleSignInButton } from '@aws-amplify/ui'; import { SignInButton, SignInButtonIcon, SignInButtonContent, } from '../../Amplify-UI/Amplify-UI-Components-React'; import { Constants } from '../common/constants'; const logger = new Logger('withGoogle'); export function withGoogle(Comp) { return class extends React.Component { constructor(props: any) { super(props); this.initGapi = this.initGapi.bind(this); this.signIn = this.signIn.bind(this); this.signOut = this.signOut.bind(this); this.federatedSignIn = this.federatedSignIn.bind(this); this.state = {}; } signIn() { const ga = window.gapi.auth2.getAuthInstance(); const { onError } = this.props; ga.signIn().then( googleUser => { this.federatedSignIn(googleUser); const payload = { provider: Constants.GOOGLE, }; try { localStorage.setItem( Constants.AUTH_SOURCE_KEY, JSON.stringify(payload) ); } catch (e) { logger.debug('Failed to cache auth source into localStorage', e); } }, error => { if (onError) onError(error); else throw error; } ); } async federatedSignIn(googleUser) { const { id_token, expires_at } = googleUser.getAuthResponse(); const profile = googleUser.getBasicProfile(); let user = { email: profile.getEmail(), name: profile.getName(), picture: profile.getImageUrl(), }; const { onStateChange } = this.props; if ( !Auth || typeof Auth.federatedSignIn !== 'function' || typeof Auth.currentAuthenticatedUser !== 'function' ) { throw new Error( 'No Auth module found, please ensure @aws-amplify/auth is imported' ); } await Auth.federatedSignIn( 'google', { token: id_token, expires_at }, user ); user = await Auth.currentAuthenticatedUser(); if (onStateChange) { onStateChange('signedIn', user); } } signOut() { const authInstance = window.gapi && window.gapi.auth2 ? window.gapi.auth2.getAuthInstance() : null; if (!authInstance) { return Promise.resolve(); } authInstance.then(googleAuth => { if (!googleAuth) { logger.debug('google Auth undefined'); return Promise.resolve(); } logger.debug('google signing out'); return googleAuth.signOut(); }); } componentDidMount() { const { google_client_id } = this.props; const ga = window.gapi && window.gapi.auth2 ? window.gapi.auth2.getAuthInstance() : null; if (google_client_id && !ga) this.createScript(); } createScript() { const script = document.createElement('script'); script.src = 'https://apis.google.com/js/platform.js'; script.async = true; script.onload = this.initGapi; document.body.appendChild(script); } initGapi() { logger.debug('init gapi'); const that = this; const { google_client_id } = this.props; const g = window.gapi; g.load('auth2', function() { g.auth2.init({ client_id: google_client_id, scope: 'profile email openid', }); }); } render(): React.ReactNode { const ga = window.gapi && window.gapi.auth2 ? window.gapi.auth2.getAuthInstance() : null; return ( ); } }; } const Button = props => ( {I18n.get('Sign In with Google')} ); export const GoogleButton = withGoogle(Button); /** * @deprecated use named import */ export default withGoogle;