@@ -13,30 +13,30 @@ import { Auth } from 'aws-amplify';
13
13
14
14
// To derive necessary data from the provider
15
15
const {
16
- token , // the token you get from the provider
17
- domainOrProviderName, // Either the domain of the provider(e.g. accounts.your-openid-provider.com) or the provider name, for now the library only supports 'google', 'facebook', 'amazon', 'developer'
18
- expiresIn, // the time in ms which describes how long the token could live
19
- user, // the user object you defined, e.g. { username, email, phone_number }
20
- identity_id // Optional, the identity id specified by the provider
16
+ token , // the token you get from the provider
17
+ domainOrProviderName, // Either the domain of the provider(e.g. accounts.your-openid-provider.com) or the provider name, for now the library only supports 'google', 'facebook', 'amazon', 'developer'
18
+ expiresIn, // the time in ms which describes how long the token could live
19
+ user, // the user object you defined, e.g. { username, email, phone_number }
20
+ identity_id // Optional, the identity id specified by the provider
21
21
} = getFromProvider (); // arbitrary function
22
22
23
23
Auth .federatedSignIn (
24
- domain,
25
- {
26
- token,
27
- identity_id, // Optional
28
- expires_at: expiresIn * 1000 + new Date ().getTime () // the expiration timestamp
29
- },
30
- user
24
+ domain,
25
+ {
26
+ token,
27
+ identity_id, // Optional
28
+ expires_at: expiresIn * 1000 + new Date ().getTime () // the expiration timestamp
29
+ },
30
+ user
31
31
).then (cred => {
32
- // If success, you will get the AWS credentials
33
- console .log (cred);
34
- return Auth .currentAuthenticatedUser ();
32
+ // If success, you will get the AWS credentials
33
+ console .log (cred);
34
+ return Auth .currentAuthenticatedUser ();
35
35
}).then (user => {
36
- // If success, the user object you passed in Auth.federatedSignIn
37
- console .log (user);
36
+ // If success, the user object you passed in Auth.federatedSignIn
37
+ console .log (user);
38
38
}).catch (e => {
39
- console .log (e)
39
+ console .log (e)
40
40
});
41
41
```
42
42
@@ -52,85 +52,85 @@ import { Auth } from 'aws-amplify';
52
52
// To federated sign in from Facebook
53
53
const SignInWithFacebook = () => {
54
54
55
- useEffect (() => {
56
- if (! window .FB ) createScript ();
57
- }, [])
58
-
59
- const signIn = () => {
60
- const fb = window .FB ;
61
- fb .getLoginStatus (response => {
62
- if (response .status === ' connected' ) {
63
- getAWSCredentials (response .authResponse );
64
- } else {
65
- fb .login (
66
- response => {
67
- if (! response || ! response .authResponse ) {
68
- return ;
69
- }
70
- getAWSCredentials (response .authResponse );
71
- },
72
- {
73
- // the authorized scopes
74
- scope: ' public_profile,email'
75
- }
76
- );
55
+ useEffect (() => {
56
+ if (! window .FB ) createScript ();
57
+ }, [])
58
+
59
+ const signIn = () => {
60
+ const fb = window .FB ;
61
+ fb .getLoginStatus (response => {
62
+ if (response .status === ' connected' ) {
63
+ getAWSCredentials (response .authResponse );
64
+ } else {
65
+ fb .login (
66
+ response => {
67
+ if (! response || ! response .authResponse ) {
68
+ return ;
77
69
}
78
- });
70
+ getAWSCredentials (response .authResponse );
71
+ },
72
+ {
73
+ // the authorized scopes
74
+ scope: ' public_profile,email'
75
+ }
76
+ );
77
+ }
78
+ });
79
+ }
80
+
81
+ const getAWSCredentials = (response ) => {
82
+ const { accessToken , expiresIn } = response;
83
+ const date = new Date ();
84
+ const expires_at = expiresIn * 1000 + date .getTime ();
85
+ if (! accessToken) {
86
+ return ;
79
87
}
80
88
81
- const getAWSCredentials = (response ) => {
82
- const { accessToken , expiresIn } = response;
83
- const date = new Date ();
84
- const expires_at = expiresIn * 1000 + date .getTime ();
85
- if (! accessToken) {
86
- return ;
87
- }
89
+ const fb = window .FB ;
90
+ fb .api (' /me' , { fields: ' name,email' }, response => {
91
+ const user = {
92
+ name: response .name ,
93
+ email: response .email
94
+ };
95
+
96
+ Auth .federatedSignIn (' facebook' , { token: accessToken, expires_at }, user)
97
+ .then (credentials => {
98
+ console .log (credentials);
99
+ });
100
+ });
101
+ }
88
102
89
- const fb = window .FB ;
90
- fb .api (' /me' , { fields: ' name,email' }, response => {
91
- const user = {
92
- name: response .name ,
93
- email: response .email
94
- };
95
-
96
- Auth .federatedSignIn (' facebook' , { token: accessToken, expires_at }, user)
97
- .then (credentials => {
98
- console .log (credentials);
99
- });
100
- });
101
- }
102
-
103
- const createScript = () => {
104
- // load the sdk
105
- window .fbAsyncInit = fbAsyncInit;
106
- const script = document .createElement (' script' );
107
- script .src = ' https://connect.facebook.net/en_US/sdk.js' ;
108
- script .async = true ;
109
- script .onload = initFB;
110
- document .body .appendChild (script);
111
- }
103
+ const createScript = () => {
104
+ // load the sdk
105
+ window .fbAsyncInit = fbAsyncInit;
106
+ const script = document .createElement (' script' );
107
+ script .src = ' https://connect.facebook.net/en_US/sdk.js' ;
108
+ script .async = true ;
109
+ script .onload = initFB;
110
+ document .body .appendChild (script);
111
+ }
112
112
113
- const initFB = () => {
114
- const fb = window .FB ;
115
- console .log (' FB SDK initialized' );
116
- }
113
+ const initFB = () => {
114
+ const fb = window .FB ;
115
+ console .log (' FB SDK initialized' );
116
+ }
117
117
118
- const fbAsyncInit = () => {
119
- // init the fb sdk client
120
- const fb = window .FB ;
121
- fb .init ({
122
- appId : ' your_facebook_app_id' ,
123
- cookie : true ,
124
- xfbml : true ,
125
- version : ' v2.11'
126
- });
127
- }
128
-
129
- return (
130
- < div>
131
- < button onClick= {signIn}> Sign in with Facebook< / button>
132
- < / div>
133
- );
118
+ const fbAsyncInit = () => {
119
+ // init the fb sdk client
120
+ const fb = window .FB ;
121
+ fb .init ({
122
+ appId : ' your_facebook_app_id' ,
123
+ cookie : true ,
124
+ xfbml : true ,
125
+ version : ' v2.11'
126
+ });
127
+ }
128
+
129
+ return (
130
+ < div>
131
+ < button onClick= {signIn}> Sign in with Facebook< / button>
132
+ < / div>
133
+ );
134
134
}
135
135
```
136
136
### Facebook Sign-in (React Native - Expo)
@@ -268,23 +268,23 @@ Note: Automatic token refresh for Google and Facebook is not supported in React
268
268
import { Auth } from ' aws-amplify' ;
269
269
270
270
function refreshToken () {
271
- // refresh the token here and get the new token info
272
- // ......
273
-
274
- return new Promise (res, rej => {
275
- const data = {
276
- token, // the token from the provider
277
- expires_at, // the timestamp for the expiration
278
- identity_id, // optional, the identityId for the credentials
279
- }
280
- res (data);
281
- });
271
+ // refresh the token here and get the new token info
272
+ // ......
273
+
274
+ return new Promise (res, rej => {
275
+ const data = {
276
+ token, // the token from the provider
277
+ expires_at, // the timestamp for the expiration
278
+ identity_id, // optional, the identityId for the credentials
279
+ }
280
+ res (data);
281
+ });
282
282
}
283
283
284
284
Auth .configure ({
285
- refreshHandlers: {
286
- ' developer' : refreshToken // the property could be 'google', 'facebook', 'amazon', 'developer', OpenId domain
287
- }
285
+ refreshHandlers: {
286
+ ' developer' : refreshToken // the property could be 'google', 'facebook', 'amazon', 'developer', OpenId domain
287
+ }
288
288
})
289
289
` ` `
290
290
@@ -301,23 +301,23 @@ const { idToken, domain, name, email, phoneNumber } = getFromAuth0(); // get the
301
301
const { exp } = decodeJWTToken (idToken); // Please decode the id token in order to get the expiration time
302
302
303
303
Auth .federatedSignIn (
304
- domain, // The Auth0 Domain,
305
- {
306
- token: idToken, // The id token from Auth0
307
- // expires_at means the timestamp when the token provided expires,
308
- // here you can derive it from the expiresIn parameter provided,
309
- // then convert its unit from second to millisecond, and add the current timestamp
310
- expires_at: exp * 1000 // the expiration timestamp
311
- },
312
- {
313
- // the user object, you can put whatever property you get from the Auth0
314
- // for example:
315
- name, // the user name
316
- email, // Optional, the email address
317
- phoneNumber, // Optional, the phone number
318
- }
304
+ domain, // The Auth0 Domain,
305
+ {
306
+ token: idToken, // The id token from Auth0
307
+ // expires_at means the timestamp when the token provided expires,
308
+ // here you can derive it from the expiresIn parameter provided,
309
+ // then convert its unit from second to millisecond, and add the current timestamp
310
+ expires_at: exp * 1000 // the expiration timestamp
311
+ },
312
+ {
313
+ // the user object, you can put whatever property you get from the Auth0
314
+ // for example:
315
+ name, // the user name
316
+ email, // Optional, the email address
317
+ phoneNumber, // Optional, the phone number
318
+ }
319
319
).then (cred => {
320
- console .log (cred);
320
+ console .log (cred);
321
321
});
322
322
` ` `
323
323
@@ -333,23 +333,23 @@ Step 4. You can pass a refresh handler to the Auth module to refresh the id toke
333
333
334
334
` ` ` js
335
335
function refreshToken () {
336
- // refresh the token here and get the new token info
337
- // ......
338
-
339
- return new Promise (res, rej => {
340
- const data = {
341
- token, // the token from the provider
342
- expires_at, // the timestamp when the token expires (in milliseconds)
343
- identity_id, // optional, the identityId for the credentials
344
- }
345
- res (data);
346
- });
336
+ // refresh the token here and get the new token info
337
+ // ......
338
+
339
+ return new Promise (res, rej => {
340
+ const data = {
341
+ token, // the token from the provider
342
+ expires_at, // the timestamp when the token expires (in milliseconds)
343
+ identity_id, // optional, the identityId for the credentials
344
+ }
345
+ res (data);
346
+ });
347
347
}
348
348
349
349
Auth .configure ({
350
- refreshHandlers: {
351
- ' your_auth0_domain' : refreshToken
352
- }
350
+ refreshHandlers: {
351
+ ' your_auth0_domain' : refreshToken
352
+ }
353
353
})
354
354
` ` `
355
355
@@ -363,9 +363,9 @@ If you have a Pre Sign-up or Pre Authentication Lambda trigger enabled, you can
363
363
364
364
` ` ` js
365
365
Auth .signIn ({
366
- username, // Required, the username
367
- password, // Optional, the password
368
- validationData, // Optional, an object of key-value pairs which can contain any key and will be passed to your Lambda trigger as-is.
366
+ username, // Required, the username
367
+ password, // Optional, the password
368
+ validationData, // Optional, an object of key-value pairs which can contain any key and will be passed to your Lambda trigger as-is.
369
369
}).then (user => console .log (user))
370
370
.catch (err => console .log (err));
371
371
` ` `
@@ -379,17 +379,17 @@ If you have `autoSignIn` enabled, you can pass `validationData` as property for
379
379
380
380
` ` ` js
381
381
Auth .signUp ({
382
- username,
383
- password,
384
- attributes: {
385
- email, // optional
386
- phone_number, // optional - E.164 number convention
387
- // other custom attributes
388
- },
389
- autoSignIn: { // optional - enables auto sign in after user is confirmed
390
- enabled: true ,
391
- validationData, // optional
392
- }
382
+ username,
383
+ password,
384
+ attributes: {
385
+ email, // optional
386
+ phone_number, // optional - E.164 number convention
387
+ // other custom attributes
388
+ },
389
+ autoSignIn: { // optional - enables auto sign in after user is confirmed
390
+ enabled: true ,
391
+ validationData, // optional
392
+ }
393
393
})
394
394
` ` `
395
395
0 commit comments