1
/*
2
 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
 * the License. A copy of the License is located at
6
 *
7
 *     http://aws.amazon.com/apache2.0/
8
 *
9
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
 * and limitations under the License.
12
 */
13

14 1
import * as React from 'react';
15

16 1
import { I18n, ConsoleLogger as Logger } from '@aws-amplify/core';
17 1
import { Auth } from '@aws-amplify/auth';
18 1
import AmplifyTheme from '../../Amplify-UI/Amplify-UI-Theme';
19 1
import { amazonSignInButton } from '@aws-amplify/ui';
20 1
import {
21
	SignInButton,
22
	SignInButtonIcon,
23
	SignInButtonContent,
24
} from '../../Amplify-UI/Amplify-UI-Components-React';
25 1
import { Constants } from '../common/constants';
26

27 1
const logger = new Logger('withAmazon');
28

29 1
export function withAmazon(Comp) {
30 1
	return class extends React.Component<any, any> {
31
		constructor(props: any) {
32 1
			super(props);
33

34 1
			this.initAmazon = this.initAmazon.bind(this);
35 1
			this.signIn = this.signIn.bind(this);
36 1
			this.signOut = this.signOut.bind(this);
37 1
			this.federatedSignIn = this.federatedSignIn.bind(this);
38

39 1
			this.state = {};
40
		}
41

42 1
		signIn() {
43 1
			const amz = window.amazon;
44 1
			const options = { scope: 'profile' };
45 1
			amz.Login.authorize(options, response => {
46 1
				if (response.error) {
47 1
					logger.debug('Failed to login with amazon: ' + response.error);
48 1
					return;
49
				}
50

51 1
				const payload = {
52
					provider: Constants.AMAZON,
53
				};
54 1
				try {
55 1
					localStorage.setItem(
56
						Constants.AUTH_SOURCE_KEY,
57
						JSON.stringify(payload)
58
					);
59
				} catch (e) {
60 0
					logger.debug('Failed to cache auth source into localStorage', e);
61
				}
62

63 1
				this.federatedSignIn(response);
64
			});
65
		}
66

67 1
		federatedSignIn(response) {
68 1
			const { access_token, expires_in } = response;
69 1
			const { onStateChange } = this.props;
70 1
			const date = new Date();
71 1
			const expires_at = expires_in * 1000 + date.getTime();
72 1
			if (!access_token) {
73 1
				return;
74
			}
75

76 1
			const amz = window.amazon;
77 1
			amz.Login.retrieveProfile(userInfo => {
78 1
				if (!userInfo.success) {
79 1
					logger.debug('Get user Info failed');
80 1
					return;
81
				}
82

83 1
				const user = {
84
					name: userInfo.profile.Name,
85
					email: userInfo.profile.PrimaryEmail,
86
				};
87 1
				if (
88 1
					!Auth ||
89
					typeof Auth.federatedSignIn !== 'function' ||
90
					typeof Auth.currentAuthenticatedUser !== 'function'
91
				) {
92 0
					throw new Error(
93
						'No Auth module found, please ensure @aws-amplify/auth is imported'
94
					);
95
				}
96

97 1
				Auth.federatedSignIn(
98
					'amazon',
99
					{ token: access_token, expires_at },
100
					user
101
				)
102
					.then(credentials => {
103 1
						return Auth.currentAuthenticatedUser();
104
					})
105
					.then(authUser => {
106 1
						if (onStateChange) {
107 1
							onStateChange('signedIn', authUser);
108
						}
109
					});
110
			});
111
		}
112

113 1
		signOut() {
114 1
			const amz = window.amazon;
115 1
			if (!amz) {
116 1
				logger.debug('Amazon Login sdk undefined');
117 1
				return Promise.resolve();
118
			}
119

120 1
			logger.debug('Amazon signing out');
121 1
			amz.Login.logout();
122
		}
123

124 1
		componentDidMount() {
125 1
			const { amazon_client_id } = this.props;
126 1
			if (amazon_client_id && !window.amazon) this.createScript();
127
		}
128

129 1
		createScript() {
130 0
			const script = document.createElement('script');
131 0
			script.src = 'https://api-cdn.amazon.com/sdk/login1.js';
132 0
			script.async = true;
133 0
			script.onload = this.initAmazon;
134 0
			document.body.appendChild(script);
135
		}
136

137 1
		initAmazon() {
138 1
			logger.debug('init amazon');
139 1
			const { amazon_client_id } = this.props;
140 1
			const amz = window.amazon;
141 1
			amz.Login.setClientId(amazon_client_id);
142
		}
143

144 1
		render() {
145 1
			const amz = window.amazon;
146 1
			return (
147
				<Comp
148
					{...this.props}
149
					amz={amz}
150
					amazonSignIn={this.signIn}
151
					amazonSignOut={this.signOut}
152
				/>
153
			);
154
		}
155 1
	};
156
}
157

158 1
const Button = props => (
159
	<SignInButton
160
		id={amazonSignInButton}
161
		onClick={props.amazonSignIn}
162 1
		theme={props.theme || AmplifyTheme}
163
		variant={'amazonSignInButton'}
164
	>
165 1
		<SignInButtonIcon theme={props.theme || AmplifyTheme}>
166
			<svg viewBox="0 0 248 268" xmlns="http://www.w3.org/2000/svg">
167
				<g id="Artboard-Copy-2" fill="none" fillRule="evenodd">
168
					<path
169
						d="M139.056521,147.024612 C133.548808,156.744524 124.782731,162.726926 115.087401,162.726926 C101.790721,162.726926 93.9937779,152.612964 93.9937779,137.68681 C93.9937779,108.224571 120.447551,102.879017 145.533369,102.879017 L145.533369,110.365976 C145.533369,123.831358 145.876354,135.063787 139.056521,147.024612 M207.206992,162.579655 C209.400505,165.692256 209.887066,169.437725 207.063416,171.770186 C199.996315,177.653081 187.429476,188.590967 180.513926,194.716661 L180.46208,194.621133 C178.176838,196.663031 174.862638,196.810303 172.27828,195.445057 C160.780281,185.9162 158.686473,181.494078 152.405048,172.403055 C133.405233,191.751331 119.909143,197.534719 95.309886,197.534719 C66.1281801,197.534719 43.4791563,179.599451 43.4791563,143.669212 C43.4791563,115.616003 58.6782107,96.5105248 80.4019706,87.1727225 C99.2063636,78.9096034 125.464714,77.4528107 145.533369,75.1641337 L145.533369,70.694248 C145.533369,62.4749122 146.167493,52.7510201 141.297893,45.6541312 C137.110277,39.2856386 129.018206,36.6586354 121.859376,36.6586354 C108.658413,36.6586354 96.9171331,43.4171982 94.0416364,57.4199213 C93.4593582,60.532522 91.1701278,63.5933787 88.003492,63.7406501 L54.4387473,60.1424518 C51.6150972,59.5095829 48.4484614,57.2248862 49.2740201,52.8982915 C56.9712583,12.2553679 93.7983558,0 126.732964,0 C143.587124,0 165.606011,4.47386604 178.902691,17.2148315 C195.760839,32.917146 194.149604,53.8694866 194.149604,76.6726704 L194.149604,130.542157 C194.149604,146.734049 200.87372,153.830938 207.206992,162.579655 Z M233.826346,208.038962 C230.467669,203.683255 211.550709,205.9821 203.056405,206.998432 C200.470662,207.321077 200.076227,205.042397 202.406981,203.404973 C217.475208,192.664928 242.201125,195.766353 245.081698,199.363845 C247.966255,202.981502 244.336653,228.071183 230.172839,240.049379 C228.001452,241.888455 225.929671,240.904388 226.89783,238.468418 C230.077218,230.430525 237.204944,212.418868 233.826346,208.038962 Z M126.768855,264 C74.0234043,264 42.0764048,241.955028 17.7852554,217.541992 C12.9733903,212.705982 6.71799208,206.295994 3.31151296,200.690918 C1.90227474,198.372135 5.59096074,195.021875 8.0442063,196.84375 C38.2390146,219.267578 82.1011654,239.538304 125.529506,239.538304 C154.819967,239.538304 191.046475,227.469543 220.66851,214.867659 C225.146771,212.966167 225.146771,219.180222 224.511585,221.060516 C224.183264,222.03242 209.514625,236.221149 189.247207,247.047411 C170.304273,257.166172 146.397132,264 126.768855,264 Z"
170
						id="Fill-6"
171
						fill="#FFF"
172
					/>
173
				</g>
174
			</svg>
175
		</SignInButtonIcon>
176 1
		<SignInButtonContent theme={props.theme || AmplifyTheme}>
177
			{I18n.get('Sign In with Amazon')}
178
		</SignInButtonContent>
179
	</SignInButton>
180
);
181

182 1
export const AmazonButton = withAmazon(Button);
183

184
/**
185
 * @deprecated use named import
186
 */
187 1
export default withAmazon;

Read our documentation on viewing source code .

Loading