-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
SignOut.jsx
148 lines (130 loc) · 4.97 KB
/
SignOut.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import * as React from 'react';
import { I18n, ConsoleLogger as Logger, Hub } from '@aws-amplify/core';
import Auth from '@aws-amplify/auth';
import AuthPiece from './AuthPiece';
import { NavButton } from '../Amplify-UI/Amplify-UI-Components-React';
import AmplifyTheme from '../Amplify-UI/Amplify-UI-Theme';
import Constants from './common/constants';
import { auth } from '../Amplify-UI/data-test-attributes';
const logger = new Logger('SignOut');
export default class SignOut extends AuthPiece {
constructor(props) {
super(props);
this.signOut = this.signOut.bind(this);
this.onHubCapsule = this.onHubCapsule.bind(this)
Hub.listen('auth', this.onHubCapsule)
this.state = {};
}
componentDidMount() {
this._isMounted = true;
this.findState();
}
componentWillUnmount() {
this._isMounted = false;
}
findState(){
if (!this.props.authState && !this.props.authData) {
Auth.currentAuthenticatedUser()
.then(user => {
this.setState({
authState: 'signedIn',
authData: user,
stateFromStorage: true
})
})
.catch(err => logger.error(err));
} else if (this.props.stateFromStorage) {
this.setState({
stateFromStorage: true
})
}
}
onHubCapsule(capsule) {
if (this._isMounted) {
const { channel, payload, source } = capsule;
if (channel === 'auth' && payload.event === 'signIn') {
this.setState({
authState: 'signedIn',
authData: payload.data
})
} else if (channel === 'auth' && payload.event === 'signOut' && (!this.props.authState)) {
this.setState({
authState: 'signIn'
})
}
if (channel === 'auth' && payload.event === 'signIn' && (!this.props.authState)) {
this.setState({stateFromStorage: true})
}
}
}
signOut() {
let payload = {};
try {
payload = JSON.parse(localStorage.getItem(Constants.AUTH_SOURCE_KEY)) || {};
localStorage.removeItem(Constants.AUTH_SOURCE_KEY);
} catch (e) {
logger.debug(`Failed to parse the info from ${Constants.AUTH_SOURCE_KEY} from localStorage with ${e}`);
}
logger.debug('sign out from the source', payload);
const { googleSignOut, facebookSignOut, amazonSignOut, auth0SignOut } = this.props;
switch (payload.provider) {
case Constants.GOOGLE:
if (googleSignOut) googleSignOut();
else logger.debug('No Google signout method provided');
break;
case Constants.FACEBOOK:
if (facebookSignOut) facebookSignOut();
else logger.debug('No Facebook signout method provided');
break;
case Constants.AMAZON:
if (amazonSignOut) amazonSignOut();
else logger.debug('No Amazon signout method provided');
break;
case Constants.AUTH0:
if (auth0SignOut) auth0SignOut(payload.opts);
else logger.debug('No Auth0 signout method provided');
break;
default:
break;
}
if (!Auth || typeof Auth.signOut !== 'function') {
throw new Error('No Auth module found, please ensure @aws-amplify/auth is imported');
}
Auth.signOut()
.then(() => {
if (!this.state.stateFromStorage) {
this.changeState('signedOut');
}
})
.catch(err => { logger.debug(err); this.error(err); });
}
render() {
const { hide } = this.props;
if (hide && hide.includes(SignOut)) { return null; }
const authState = this.props.authState || this.state.authState;
const signedIn = (authState === 'signedIn');
const theme = this.props.theme || AmplifyTheme;
if (!signedIn) { return null; }
return (
<NavButton
theme={theme}
onClick={this.signOut}
data-test={auth.signOut.button}
>
{I18n.get('Sign Out')}
</NavButton>
);
}
}