/* * 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 { amazonSignInButton } from '@aws-amplify/ui'; import { SignInButton, SignInButtonIcon, SignInButtonContent, } from '../../Amplify-UI/Amplify-UI-Components-React'; import { Constants } from '../common/constants'; const logger = new Logger('withAmazon'); export function withAmazon(Comp) { return class extends React.Component { constructor(props: any) { super(props); this.initAmazon = this.initAmazon.bind(this); this.signIn = this.signIn.bind(this); this.signOut = this.signOut.bind(this); this.federatedSignIn = this.federatedSignIn.bind(this); this.state = {}; } signIn() { const amz = window.amazon; const options = { scope: 'profile' }; amz.Login.authorize(options, response => { if (response.error) { logger.debug('Failed to login with amazon: ' + response.error); return; } const payload = { provider: Constants.AMAZON, }; try { localStorage.setItem( Constants.AUTH_SOURCE_KEY, JSON.stringify(payload) ); } catch (e) { logger.debug('Failed to cache auth source into localStorage', e); } this.federatedSignIn(response); }); } federatedSignIn(response) { const { access_token, expires_in } = response; const { onStateChange } = this.props; const date = new Date(); const expires_at = expires_in * 1000 + date.getTime(); if (!access_token) { return; } const amz = window.amazon; amz.Login.retrieveProfile(userInfo => { if (!userInfo.success) { logger.debug('Get user Info failed'); return; } const user = { name: userInfo.profile.Name, email: userInfo.profile.PrimaryEmail, }; if ( !Auth || typeof Auth.federatedSignIn !== 'function' || typeof Auth.currentAuthenticatedUser !== 'function' ) { throw new Error( 'No Auth module found, please ensure @aws-amplify/auth is imported' ); } Auth.federatedSignIn( 'amazon', { token: access_token, expires_at }, user ) .then(credentials => { return Auth.currentAuthenticatedUser(); }) .then(authUser => { if (onStateChange) { onStateChange('signedIn', authUser); } }); }); } signOut() { const amz = window.amazon; if (!amz) { logger.debug('Amazon Login sdk undefined'); return Promise.resolve(); } logger.debug('Amazon signing out'); amz.Login.logout(); } componentDidMount() { const { amazon_client_id } = this.props; if (amazon_client_id && !window.amazon) this.createScript(); } createScript() { const script = document.createElement('script'); script.src = 'https://api-cdn.amazon.com/sdk/login1.js'; script.async = true; script.onload = this.initAmazon; document.body.appendChild(script); } initAmazon() { logger.debug('init amazon'); const { amazon_client_id } = this.props; const amz = window.amazon; amz.Login.setClientId(amazon_client_id); } render() { const amz = window.amazon; return ( ); } }; } const Button = props => ( {I18n.get('Sign In with Amazon')} ); export const AmazonButton = withAmazon(Button); /** * @deprecated use named import */ export default withAmazon;