Skip to content

Commit a797af0

Browse files
author
Bannon Tanner
authored
fix(JS/RN): fixed code indent for JS/RN in Auth category (#5267)
* fixed indent for JS/RN in Auth category * added a couple more pages and removed the CLI commands from this PR
1 parent 6bbc3c8 commit a797af0

File tree

6 files changed

+365
-366
lines changed

6 files changed

+365
-366
lines changed

src/fragments/lib/auth/js/advanced.mdx

Lines changed: 149 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@ import { Auth } from 'aws-amplify';
1313

1414
// To derive necessary data from the provider
1515
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
2121
} = getFromProvider(); // arbitrary function
2222

2323
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
3131
).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();
3535
}).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);
3838
}).catch(e => {
39-
console.log(e)
39+
console.log(e)
4040
});
4141
```
4242

@@ -52,85 +52,85 @@ import { Auth } from 'aws-amplify';
5252
// To federated sign in from Facebook
5353
const SignInWithFacebook = () => {
5454

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;
7769
}
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;
7987
}
8088

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+
}
88102

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+
}
112112

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+
}
117117

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+
);
134134
}
135135
```
136136
### Facebook Sign-in (React Native - Expo)
@@ -268,23 +268,23 @@ Note: Automatic token refresh for Google and Facebook is not supported in React
268268
import { Auth } from 'aws-amplify';
269269

270270
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+
});
282282
}
283283

284284
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+
}
288288
})
289289
```
290290
@@ -301,23 +301,23 @@ const { idToken, domain, name, email, phoneNumber } = getFromAuth0(); // get the
301301
const { exp } = decodeJWTToken(idToken); // Please decode the id token in order to get the expiration time
302302

303303
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+
}
319319
).then(cred => {
320-
console.log(cred);
320+
console.log(cred);
321321
});
322322
```
323323
@@ -333,23 +333,23 @@ Step 4. You can pass a refresh handler to the Auth module to refresh the id toke
333333
334334
```js
335335
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+
});
347347
}
348348

349349
Auth.configure({
350-
refreshHandlers: {
351-
'your_auth0_domain': refreshToken
352-
}
350+
refreshHandlers: {
351+
'your_auth0_domain': refreshToken
352+
}
353353
})
354354
```
355355
@@ -363,9 +363,9 @@ If you have a Pre Sign-up or Pre Authentication Lambda trigger enabled, you can
363363
364364
```js
365365
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.
369369
}).then(user => console.log(user))
370370
.catch(err => console.log(err));
371371
```
@@ -379,17 +379,17 @@ If you have `autoSignIn` enabled, you can pass `validationData` as property for
379379
380380
```js
381381
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+
}
393393
})
394394
```
395395

0 commit comments

Comments
 (0)