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 1
import * as React from 'react';
15 1
import { I18n, ConsoleLogger as Logger, Hub } from '@aws-amplify/core';
16 1
import { Auth } from '@aws-amplify/auth';
17

18 1
import { AuthPiece, IAuthPieceProps, IAuthPieceState } from './AuthPiece';
19 1
import { NavButton } from '../Amplify-UI/Amplify-UI-Components-React';
20 1
import AmplifyTheme from '../Amplify-UI/Amplify-UI-Theme';
21

22 1
import { Constants } from './common/constants';
23

24 1
import { auth } from '../Amplify-UI/data-test-attributes';
25

26 1
const logger = new Logger('SignOut');
27

28
export interface ISignOutProps extends IAuthPieceProps {
29
	googleSignOut?: any;
30
	facebookSignOut?: any;
31
	amazonSignOut?: any;
32
	auth0SignOut?: any;
33
	stateFromStorage?: any;
34
}
35

36
export interface ISignOutState extends IAuthPieceState {
37
	authData?: any;
38
	authState?: any;
39
	stateFromStorage?: any;
40
}
41

42 1
export class SignOut extends AuthPiece<ISignOutProps, ISignOutState> {
43
	public _isMounted: boolean;
44

45
	constructor(props: ISignOutProps) {
46 1
		super(props);
47

48 1
		this.signOut = this.signOut.bind(this);
49 1
		this.onHubCapsule = this.onHubCapsule.bind(this);
50 1
		Hub.listen('auth', this.onHubCapsule);
51 1
		this.state = {};
52
	}
53

54 1
	componentDidMount() {
55 1
		this._isMounted = true;
56 1
		this.findState();
57
	}
58

59 1
	componentWillUnmount() {
60 1
		this._isMounted = false;
61
	}
62

63 1
	findState() {
64 1
		if (!this.props.authState && !this.props.authData) {
65 1
			Auth.currentAuthenticatedUser()
66
				.then(user => {
67 1
					this.setState({
68
						authState: 'signedIn',
69
						authData: user,
70
						stateFromStorage: true,
71
					});
72
				})
73 1
				.catch(err => logger.error(err));
74 1
		} else if (this.props.stateFromStorage) {
75 0
			this.setState({
76
				stateFromStorage: true,
77
			});
78
		}
79
	}
80

81 1
	onHubCapsule(capsule) {
82 1
		if (this._isMounted) {
83 1
			const { channel, payload, source } = capsule;
84 1
			if (channel === 'auth' && payload.event === 'signIn') {
85 1
				this.setState({
86
					authState: 'signedIn',
87
					authData: payload.data,
88
				});
89 1
			} else if (
90 1
				channel === 'auth' &&
91
				payload.event === 'signOut' &&
92
				!this.props.authState
93
			) {
94 1
				this.setState({
95
					authState: 'signIn',
96
				});
97
			}
98

99 1
			if (
100 1
				channel === 'auth' &&
101
				payload.event === 'signIn' &&
102
				!this.props.authState
103
			) {
104 1
				this.setState({ stateFromStorage: true });
105
			}
106
		}
107
	}
108

109 1
	signOut() {
110 1
		let payload = {};
111 1
		try {
112 1
			payload =
113 1
				JSON.parse(localStorage.getItem(Constants.AUTH_SOURCE_KEY)) || {};
114 1
			localStorage.removeItem(Constants.AUTH_SOURCE_KEY);
115
		} catch (e) {
116 0
			logger.debug(
117
				`Failed to parse the info from ${Constants.AUTH_SOURCE_KEY} from localStorage with ${e}`
118
			);
119
		}
120 1
		logger.debug('sign out from the source', payload);
121 1
		const {
122 1
			googleSignOut,
123 1
			facebookSignOut,
124 1
			amazonSignOut,
125 1
			auth0SignOut,
126
		} = this.props;
127
		// @ts-ignore
128 1
		switch (payload.provider) {
129 1
			case Constants.GOOGLE:
130 1
				if (googleSignOut) googleSignOut();
131 0
				else logger.debug('No Google signout method provided');
132 0
				break;
133
			case Constants.FACEBOOK:
134 1
				if (facebookSignOut) facebookSignOut();
135 0
				else logger.debug('No Facebook signout method provided');
136 0
				break;
137
			case Constants.AMAZON:
138 1
				if (amazonSignOut) amazonSignOut();
139 0
				else logger.debug('No Amazon signout method provided');
140 0
				break;
141
			case Constants.AUTH0:
142
				// @ts-ignore
143 1
				if (auth0SignOut) auth0SignOut(payload.opts);
144 0
				else logger.debug('No Auth0 signout method provided');
145 0
				break;
146
			default:
147 1
				break;
148
		}
149

150 1
		if (!Auth || typeof Auth.signOut !== 'function') {
151 0
			throw new Error(
152
				'No Auth module found, please ensure @aws-amplify/auth is imported'
153
			);
154
		}
155 1
		Auth.signOut()
156
			.then(() => {
157 1
				if (!this.state.stateFromStorage) {
158 1
					this.changeState('signedOut');
159
				}
160
			})
161
			.catch(err => {
162 1
				logger.debug(err);
163 1
				this.error(err);
164
			});
165
	}
166

167 1
	render() {
168 1
		const { hide } = this.props;
169 1
		if (hide && hide.includes(SignOut)) {
170 1
			return null;
171
		}
172

173 1
		const authState = this.props.authState || this.state.authState;
174 1
		const signedIn = authState === 'signedIn';
175

176 1
		const theme = this.props.theme || AmplifyTheme;
177 1
		if (!signedIn) {
178 1
			return null;
179
		}
180

181 1
		return (
182
			<NavButton
183
				theme={theme}
184
				onClick={this.signOut}
185
				data-test={auth.signOut.button}
186
			>
187
				{I18n.get('Sign Out')}
188
			</NavButton>
189
		);
190
	}
191 1
}
192

193
/**
194
 * @deprecated use named import
195
 */
196 1
export default SignOut;

Read our documentation on viewing source code .

Loading