Skip to content

Commit c47f72a

Browse files
committed
feat(aws-amplify-react): adding loading page
1 parent 5a02eb9 commit c47f72a

File tree

6 files changed

+96
-15
lines changed

6 files changed

+96
-15
lines changed

packages/auth/src/Auth.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export default class AuthClass {
180180
onFailure: (err) => {
181181
logger.debug("Error in cognito hosted auth response", err);
182182
dispatchAuthEvent('signIn_failure', err);
183+
dispatchAuthEvent('cognitoHostedUI_failure', err);
183184
}
184185
};
185186
// if not logged in, try to parse the url.
@@ -196,6 +197,7 @@ export default class AuthClass {
196197
this._cognitoAuthClient.parseCognitoWebResponse(curUrl);
197198
} catch (err) {
198199
logger.debug('something wrong when parsing the url', err);
200+
dispatchAuthEvent('parsingUrl_failure', null);
199201
}
200202
});
201203
}

packages/aws-amplify-react/src/Auth/Authenticator.jsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ import SignIn from './SignIn';
2020
import ConfirmSignIn from './ConfirmSignIn';
2121
import RequireNewPassword from './RequireNewPassword';
2222
import SignUp from './SignUp';
23+
import Loading from './Loading';
2324
import ConfirmSignUp from './ConfirmSignUp';
2425
import VerifyContact from './VerifyContact';
2526
import ForgotPassword from './ForgotPassword';
2627
import TOTPSetup from './TOTPSetup';
28+
import Constants from './common/constants';
2729

2830
import AmplifyTheme from '../Amplify-UI/Amplify-UI-Theme';
2931
import AmplifyMessageMap from '../AmplifyMessageMap';
@@ -52,7 +54,14 @@ export default class Authenticator extends Component {
5254
Amplify.configure(config);
5355
}
5456
this._isMounted = true;
55-
this.checkUser();
57+
// the workaround for Cognito Hosted UI
58+
// don't check the user immediately if redirected back from Hosted UI
59+
// instead waiting for the hub event sent from Auth module
60+
// the item in the localStorage is a mark to indicate whether
61+
// the app is redirected back from Hosted UI or not
62+
const byHostedUI = localStorage.getItem(Constants.SIGN_IN_WITH_HOSTEDUI_KEY);
63+
localStorage.removeItem(Constants.SIGN_IN_WITH_HOSTEDUI_KEY);
64+
if (!byHostedUI) this.checkUser();
5665
}
5766

5867
componentWillUnmount() {
@@ -66,13 +75,7 @@ export default class Authenticator extends Component {
6675
return Auth.currentAuthenticatedUser()
6776
.then(user => {
6877
if (!this._isMounted) { return; }
69-
if (auth !== 'signedIn') {
70-
this.setState({
71-
authState: 'signedIn',
72-
authData: user
73-
});
74-
this.handleStateChange('signedIn', user);
75-
}
78+
this.handleStateChange('signedIn', user);
7679
})
7780
.catch(err => {
7881
if (!this._isMounted) { return; }
@@ -92,8 +95,20 @@ export default class Authenticator extends Component {
9295

9396
onHubCapsule(capsule) {
9497
const { channel, payload, source } = capsule;
95-
if (channel === 'auth' && (payload.event === 'configured' || payload.event === 'cognitoHostedUI')) {
96-
this.checkUser();
98+
if (channel === 'auth') {
99+
switch (payload.event) {
100+
case 'cognitoHostedUI':
101+
this.handleStateChange('signedIn', payload.data);
102+
break;
103+
case 'cognitoHostedUI_failure':
104+
this.handleStateChange('signIn', null);
105+
break;
106+
case 'parsingUrl_failure':
107+
this.handleStateChange('signIn', null);
108+
break;
109+
default:
110+
break;
111+
}
97112
}
98113
}
99114

@@ -136,7 +151,8 @@ export default class Authenticator extends Component {
136151
ConfirmSignUp,
137152
VerifyContact,
138153
ForgotPassword,
139-
TOTPSetup
154+
TOTPSetup,
155+
Loading
140156
]);
141157
}
142158
const props_children = this.props.children || [];
@@ -150,7 +166,8 @@ export default class Authenticator extends Component {
150166
<ConfirmSignUp/>,
151167
<VerifyContact/>,
152168
<ForgotPassword/>,
153-
<TOTPSetup/>
169+
<TOTPSetup/>,
170+
<Loading/>
154171
];
155172

156173
const props_children_names = React.Children.map(props_children, child => child.type.name);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2017-2017 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+
import * as React from 'react';
15+
import { I18n, ConsoleLogger as Logger } from '@aws-amplify/core';
16+
17+
18+
import AuthPiece from './AuthPiece';
19+
import AmplifyTheme from '../AmplifyTheme';
20+
import {
21+
FormSection,
22+
SectionHeader,
23+
SectionBody,
24+
SectionFooter,
25+
Input,
26+
RadioRow,
27+
Button,
28+
Link,
29+
SectionFooterPrimaryContent,
30+
SectionFooterSecondaryContent
31+
} from '../Amplify-UI/Amplify-UI-Components-React';
32+
33+
const logger = new Logger('Loading');
34+
35+
export default class Loading extends AuthPiece {
36+
constructor(props) {
37+
super(props);
38+
39+
this._validAuthStates = ['loading'];
40+
}
41+
42+
showComponent(theme) {
43+
const { hide } = this.props;
44+
if (hide && hide.includes(Loading)) { return null; }
45+
46+
return (
47+
<FormSection theme={theme}>
48+
<SectionBody theme={theme}>{I18n.get('Loading...')}
49+
</SectionBody>
50+
</FormSection>
51+
)
52+
}
53+
}

packages/aws-amplify-react/src/Auth/Provider/withOAuth.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
SignInButton,
2323
SignInButtonContent
2424
} from '../../Amplify-UI/Amplify-UI-Components-React';
25+
import Constants from '../common/constants';
2526

2627
const logger = new Logger('withOAuth');
2728

@@ -44,7 +45,7 @@ export default function withOAuth(Comp, options) {
4445

4546
logger.debug('withOAuth configuration', config);
4647
const {
47-
domain,
48+
domain,
4849
redirectSignIn,
4950
redirectSignOut,
5051
responseType
@@ -55,7 +56,13 @@ export default function withOAuth(Comp, options) {
5556
+ '/login?redirect_uri=' + redirectSignIn
5657
+ '&response_type=' + responseType
5758
+ '&client_id=' + (options.ClientId || Auth.configure().userPoolWebClientId);
58-
window.location.assign(url);
59+
60+
try {
61+
localStorage.setItem(Constants.SIGN_IN_WITH_HOSTEDUI_KEY, 'true');
62+
} catch (e) {
63+
logger.debug('Failed to set item into localStorage', e);
64+
}
65+
window.location.assign(url);
5966
}
6067

6168
render() {

packages/aws-amplify-react/src/Auth/common/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const constants = {
33
AUTH0: 'auth0',
44
GOOGLE: 'google',
55
FACEBOOK: 'facebook',
6-
AMAZON: 'amazon'
6+
AMAZON: 'amazon',
7+
SIGN_IN_WITH_HOSTEDUI_KEY: 'amplify-react-signin-with-hostedUI'
78
};
89
export default constants;

packages/aws-amplify-react/src/Auth/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export { default as ForgotPassword } from './ForgotPassword';
2929
export { default as Greetings } from './Greetings';
3030
export { default as FederatedSignIn, FederatedButtons } from './FederatedSignIn';
3131
export { default as TOTPSetup } from './TOTPSetup';
32+
export { default as Loading } from './Loading';
3233

3334
export * from './Provider';
3435

0 commit comments

Comments
 (0)