-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(SSR): withSSRContext #6146
Changes from 58 commits
b6e2d72
1158231
a07e0ac
7dd9dde
aaa31f5
9ce2624
a733a4e
b1e41c8
fea8126
8dd5fe2
0baf796
10a8141
4733b5c
644a5f1
a267d05
280e685
8636462
f8d0d94
1b2d2a0
6f92a34
c9e1c25
edba55b
8083938
b476969
fb25ade
bf5d5d5
a34bd42
eca5e86
dc048de
2ce91b9
96d7845
8bd59e6
8f431f9
864f7b8
a4c6cc6
952bedf
87a332d
c56bd06
63107ed
8e4785c
af53902
13f01e2
b805139
dfff4e5
167c787
d896644
d499bc8
a579c9b
b9a767d
9bb784e
fbfa4ae
27ae726
19d0393
8def8f4
4d29a12
9a9342b
39f57e4
875c10a
123e90c
ec15663
c77c0e2
48efc32
fc140e0
0d70618
1cb7ca1
e1de0e5
60b84d3
bf3c38a
3da1663
d0c7d58
c4d0535
c8156b4
13282f5
1ea7e71
6861ead
3151357
43bf3ea
fd68da2
9b0ecdd
1e6ed37
8aa0290
20a1191
3da5f46
84a3fab
75490f2
e96f3b5
17888e0
39f03ee
af0167b
abedee1
06c3b12
8b6db08
1e467e7
4582562
53a3355
0ba9e3a
c365524
96fc69b
fd830eb
80bf422
a8c0e49
e3baf30
cf51392
bf11192
75d4cf4
08c17d4
2bb6c86
4b65b77
83cf9cc
5135af4
7975c74
4ef5503
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -64,6 +64,7 @@ | |||||||||||||||||||||||||||||||||||||||
"dependencies": { | ||||||||||||||||||||||||||||||||||||||||
"buffer": "4.9.1", | ||||||||||||||||||||||||||||||||||||||||
"crypto-js": "^3.3.0", | ||||||||||||||||||||||||||||||||||||||||
"isomorphic-unfetch": "^3.0.0", | ||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Necessary on the server due to usage of amplify-js/packages/amazon-cognito-identity-js/src/Client.js Lines 55 to 73 in 38350d7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im good with this @elorzafe what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll be doing build comparisons to determine if there are any bundle size concerns. By design, this package is a polyfill that falls back to |
||||||||||||||||||||||||||||||||||||||||
"js-cookie": "^2.1.4" | ||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||
"devDependencies": { | ||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import 'isomorphic-unfetch'; | ||
|
||
import UserAgent from './UserAgent'; | ||
|
||
class CognitoError extends Error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,6 @@ export class AnalyticsClass { | |
Hub.listen('auth', listener); | ||
Hub.listen('storage', listener); | ||
Hub.listen('analytics', listener); | ||
Amplify.register(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is moved outside of the constructor because we want there to be a single instance of New instances of |
||
} | ||
|
||
public getModuleName() { | ||
|
@@ -402,3 +401,4 @@ const sendEvents = () => { | |
}; | ||
|
||
export const Analytics = new AnalyticsClass(); | ||
Amplify.register(Analytics); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue of having it inside of the constructor is for multiple Analytics instances? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elorzafe That's correct! Otherwise, ever instance will get pushed onto |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,13 +46,14 @@ export class GraphQLAPIClass { | |
private _options; | ||
private _api = null; | ||
|
||
Credentials = Credentials; | ||
|
||
ericclemmons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Initialize GraphQL API with AWS configuration | ||
* @param {Object} options - Configuration object for API | ||
*/ | ||
constructor(options) { | ||
this._options = options; | ||
Amplify.register(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, new instances should be registered to the global |
||
logger.debug('API Options', this._options); | ||
} | ||
|
||
|
@@ -70,6 +71,10 @@ export class GraphQLAPIClass { | |
let opt = { ...otherOptions, ...API }; | ||
logger.debug('configure GraphQL API', { opt }); | ||
|
||
if (opt['Credentials']) { | ||
this.Credentials = opt['Credentials']; | ||
} | ||
|
||
if (opt['aws_project_region']) { | ||
opt = Object.assign({}, opt, { | ||
region: opt['aws_project_region'], | ||
|
@@ -355,10 +360,10 @@ export class GraphQLAPIClass { | |
* @private | ||
*/ | ||
_ensureCredentials() { | ||
return Credentials.get() | ||
return this.Credentials.get() | ||
.then(credentials => { | ||
if (!credentials) return false; | ||
const cred = Credentials.shear(credentials); | ||
const cred = this.Credentials.shear(credentials); | ||
logger.debug('set credentials for api', cred); | ||
|
||
return true; | ||
|
@@ -371,3 +376,4 @@ export class GraphQLAPIClass { | |
} | ||
|
||
export const GraphQLAPI = new GraphQLAPIClass(null); | ||
Amplify.register(GraphQLAPI); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ export class RestAPIClass { | |
*/ | ||
constructor(options) { | ||
this._options = options; | ||
Amplify.register(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, new instances should be registered to the global |
||
logger.debug('API Options', this._options); | ||
} | ||
|
||
|
@@ -49,6 +48,10 @@ export class RestAPIClass { | |
let opt = { ...otherOptions, ...API }; | ||
logger.debug('configure Rest API', { opt }); | ||
|
||
if (opt['Credentials']) { | ||
this._api.Credentials = opt['Credentials']; | ||
} | ||
|
||
if (opt['aws_project_region']) { | ||
if (opt['aws_cloud_logic_custom']) { | ||
const custom = opt['aws_cloud_logic_custom']; | ||
|
@@ -245,3 +248,4 @@ export class RestAPIClass { | |
} | ||
|
||
export const RestAPI = new RestAPIClass(null); | ||
Amplify.register(RestAPI); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,6 @@ export class APIClass { | |
this._options = options; | ||
this._restApi = new RestAPIClass(options); | ||
this._graphqlApi = new GraphQLAPIClass(options); | ||
Amplify.register(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, new instances should be registered to the global |
||
logger.debug('API Options', this._options); | ||
} | ||
|
||
|
@@ -179,3 +178,4 @@ export class APIClass { | |
} | ||
|
||
export const API = new APIClass(null); | ||
Amplify.register(API); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,8 @@ export class AuthClass { | |
private _storageSync; | ||
private oAuthFlowInProgress: boolean = false; | ||
|
||
Credentials = Credentials; | ||
|
||
/** | ||
* Initialize Auth with AWS configurations | ||
* @param {Object} config - Configuration of the Auth | ||
|
@@ -122,7 +124,6 @@ export class AuthClass { | |
break; | ||
} | ||
}); | ||
Amplify.register(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, new instances should be registered to the global |
||
} | ||
|
||
public getModuleName() { | ||
|
@@ -181,7 +182,7 @@ export class AuthClass { | |
this.userPool = new CognitoUserPool(userPoolData); | ||
} | ||
|
||
Credentials.configure({ | ||
this.Credentials.configure({ | ||
mandatorySignIn, | ||
region: identityPoolRegion || region, | ||
userPoolId, | ||
|
@@ -480,8 +481,8 @@ export class AuthClass { | |
delete user['challengeName']; | ||
delete user['challengeParam']; | ||
try { | ||
await Credentials.clear(); | ||
const cred = await Credentials.set(session, 'session'); | ||
await this.Credentials.clear(); | ||
const cred = await this.Credentials.set(session, 'session'); | ||
logger.debug('succeed to get cognito credentials', cred); | ||
} catch (e) { | ||
logger.debug('cannot get cognito credentials', e); | ||
|
@@ -701,7 +702,9 @@ export class AuthClass { | |
user: CognitoUser | any, | ||
mfaMethod: 'TOTP' | 'SMS' | 'NOMFA' | ||
): Promise<string> { | ||
const userData = await this._getUserData(user, { bypassCache: true }); | ||
const userData = await this._getUserData(user, { | ||
bypassCache: true, | ||
}); | ||
let smsMfaSettings = null; | ||
let totpMfaSettings = null; | ||
|
||
|
@@ -903,8 +906,8 @@ export class AuthClass { | |
onSuccess: async session => { | ||
logger.debug(session); | ||
try { | ||
await Credentials.clear(); | ||
const cred = await Credentials.set(session, 'session'); | ||
await this.Credentials.clear(); | ||
const cred = await this.Credentials.set(session, 'session'); | ||
logger.debug('succeed to get cognito credentials', cred); | ||
} catch (e) { | ||
logger.debug('cannot get cognito credentials', e); | ||
|
@@ -945,8 +948,8 @@ export class AuthClass { | |
onSuccess: async session => { | ||
logger.debug(session); | ||
try { | ||
await Credentials.clear(); | ||
const cred = await Credentials.set(session, 'session'); | ||
await this.Credentials.clear(); | ||
const cred = await this.Credentials.set(session, 'session'); | ||
logger.debug('succeed to get cognito credentials', cred); | ||
} catch (e) { | ||
logger.debug('cannot get cognito credentials', e); | ||
|
@@ -1356,23 +1359,23 @@ export class AuthClass { | |
|
||
if (federatedInfo) { | ||
// refresh the jwt token here if necessary | ||
return Credentials.refreshFederatedToken(federatedInfo); | ||
return this.Credentials.refreshFederatedToken(federatedInfo); | ||
} else { | ||
return this.currentSession() | ||
.then(session => { | ||
logger.debug('getting session success', session); | ||
return Credentials.set(session, 'session'); | ||
return this.Credentials.set(session, 'session'); | ||
}) | ||
.catch(error => { | ||
logger.debug('getting session failed', error); | ||
return Credentials.set(null, 'guest'); | ||
return this.Credentials.set(null, 'guest'); | ||
}); | ||
} | ||
} | ||
|
||
public currentCredentials(): Promise<ICredentials> { | ||
logger.debug('getting current credentials'); | ||
return Credentials.get(); | ||
return this.Credentials.get(); | ||
} | ||
|
||
/** | ||
|
@@ -1539,7 +1542,7 @@ export class AuthClass { | |
|
||
private async cleanCachedItems() { | ||
// clear cognito cached item | ||
await Credentials.clear(); | ||
await this.Credentials.clear(); | ||
} | ||
|
||
/** | ||
|
@@ -1685,7 +1688,7 @@ export class AuthClass { | |
* @return {Object }- current User's information | ||
*/ | ||
public async currentUserInfo() { | ||
const source = Credentials.getCredSource(); | ||
const source = this.Credentials.getCredSource(); | ||
|
||
if (!source || source === 'aws' || source === 'userPool') { | ||
const user = await this.currentUserPoolUser().catch(err => | ||
|
@@ -1808,9 +1811,9 @@ export class AuthClass { | |
} catch (e) {} | ||
|
||
const { token, identity_id, expires_at } = response; | ||
// Because Credentials.set would update the user info with identity id | ||
// Because this.Credentials.set would update the user info with identity id | ||
// So we need to retrieve the user again. | ||
const credentials = await Credentials.set( | ||
const credentials = await this.Credentials.set( | ||
{ provider, token, identity_id, user, expires_at }, | ||
'federation' | ||
); | ||
|
@@ -1877,13 +1880,15 @@ export class AuthClass { | |
RefreshToken: new CognitoRefreshToken({ | ||
RefreshToken: refreshToken, | ||
}), | ||
AccessToken: new CognitoAccessToken({ AccessToken: accessToken }), | ||
AccessToken: new CognitoAccessToken({ | ||
AccessToken: accessToken, | ||
}), | ||
}); | ||
|
||
let credentials; | ||
// Get AWS Credentials & store if Identity Pool is defined | ||
if (this._config.identityPoolId) { | ||
credentials = await Credentials.set(session, 'session'); | ||
credentials = await this.Credentials.set(session, 'session'); | ||
logger.debug('AWS credentials', credentials); | ||
} | ||
|
||
|
@@ -2044,3 +2049,5 @@ export class AuthClass { | |
} | ||
|
||
export const Auth = new AuthClass(null); | ||
|
||
Amplify.register(Auth); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,18 @@ | |
* and limitations under the License. | ||
*/ | ||
|
||
import { Auth, CognitoHostedUIIdentityProvider } from './Auth'; | ||
import { CognitoUser, CookieStorage, appendToCognitoUserAgent } from 'amazon-cognito-identity-js'; | ||
export { | ||
CognitoUser, | ||
CookieStorage, | ||
appendToCognitoUserAgent, | ||
} from 'amazon-cognito-identity-js'; | ||
|
||
/** | ||
* @deprecated use named import | ||
*/ | ||
export default Auth; | ||
export { Auth, CognitoUser, CookieStorage, CognitoHostedUIIdentityProvider, appendToCognitoUserAgent }; | ||
export { | ||
/** | ||
* @deprecated use named import | ||
*/ | ||
Auth as default, | ||
Auth, | ||
AuthClass, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Por que? |
||
CognitoHostedUIIdentityProvider, | ||
} from './Auth'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a crapshoot whether CRA/Next/whatever wants the ESM or CJS build when
yarn link
ing. This ensures we build both 🤷