@@ -169,74 +169,63 @@ const App = () => {
169
169
170
170
### Google sign-in (React)
171
171
172
- ``` js
172
+
173
+ ``` jsx
173
174
import React , { useEffect } from ' react' ;
175
+ import jwt from ' jwt-decode' ;
174
176
import { Auth } from ' aws-amplify' ;
175
- // To federated sign in from Google
176
- const SignInWithGoogle = () => {
177
-
178
- useEffect (() => {
179
- const ga = window .gapi && window .gapi .auth2 ?
180
- window .gapi .auth2 .getAuthInstance () :
181
- null ;
182
-
183
- if (! ga) createScript ();
184
- }, [])
185
-
186
- const signIn = () => {
187
- const ga = window .gapi .auth2 .getAuthInstance ();
188
- ga .signIn ().then (
189
- googleUser => {
190
- getAWSCredentials (googleUser);
191
- },
192
- error => {
193
- console .log (error);
194
- }
195
- );
196
- }
197
-
198
- const getAWSCredentials = async (googleUser ) => {
199
- const { id_token , expires_at } = googleUser .getAuthResponse ();
200
- const profile = googleUser .getBasicProfile ();
201
- let user = {
202
- email: profile .getEmail (),
203
- name: profile .getName ()
204
- };
205
-
206
- const credentials = await Auth .federatedSignIn (
207
- ' google' ,
208
- { token: id_token, expires_at },
209
- user
210
- );
211
- console .log (' credentials' , credentials);
212
- }
213
177
214
- const createScript = () => {
215
- // load the Google SDK
216
- const script = document .createElement (' script' );
217
- script .src = ' https://apis.google.com/js/platform.js' ;
218
- script .async = true ;
219
- script .onload = initGapi;
220
- document .body .appendChild (script);
221
- }
178
+ const SignInWithGoogle = () => {
179
+ useEffect (() => {
180
+ // Check for an existing Google client initialization
181
+ if (! window .google && ! window .google ? .accounts ) createScript ();
182
+ }, []);
183
+
184
+ // Load the Google client
185
+ const createScript = () => {
186
+ const script = document .createElement (' script' );
187
+ script .src = ' https://accounts.google.com/gsi/client' ;
188
+ script .async = true ;
189
+ script .defer = true ;
190
+ script .onload = initGsi;
191
+ document .body .appendChild (script);
192
+ }
222
193
223
- const initGapi = () => {
224
- // init the Google SDK client
225
- const g = window .gapi ;
226
- g .load (' auth2' , function () {
227
- g .auth2 .init ({
228
- client_id: ' your_google_client_id' ,
229
- // authorized scopes
230
- scope: ' profile email openid'
231
- });
232
- });
194
+ // Initialize Google client and render Google button
195
+ const initGsi = () => {
196
+ if (window .google && window .google ? .accounts ) {
197
+ window .google .accounts .id .initialize ({
198
+ client_id: process .env .GOOGLE_CLIENT_ID ,
199
+ callback : (response : any ) => {
200
+ getAWSCredentials (response .credential )
201
+ },
202
+ });
203
+ window .google .accounts .id .renderButton (
204
+ document .getElementById (" googleSignInButton" ),
205
+ { theme: " outline" , size: " large" }
206
+ );
233
207
}
208
+ }
234
209
235
- return (
236
- < div>
237
- < button onClick= {signIn}> Sign in with Google< / button>
238
- < / div>
210
+ // Exchange Google token for temporary AWS credentials
211
+ const getAWSCredentials = async (credential : string ) => {
212
+ const token = jwt (credential) as any;
213
+ const user = {
214
+ email: token .email ,
215
+ name: token .name
216
+ };
217
+ await Auth .federatedSignIn (
218
+ ' google' ,
219
+ { token: credential, expires_at: token .exp },
220
+ user
239
221
);
222
+ }
223
+
224
+ return (
225
+ < div>
226
+ < button id= " googleSignInButton" / >
227
+ < / div>
228
+ );
240
229
}
241
230
` ` `
242
231
0 commit comments