From 558821bc546fbb00ece4d4635011e4a6cca5e252 Mon Sep 17 00:00:00 2001
From: Joram Wilander <jwawilander@gmail.com>
Date: Tue, 6 Sep 2016 14:56:03 -0400
Subject: Fix email invitations (#3964)

---
 webapp/components/signup/signup_controller.jsx | 109 +++++++++++++------------
 1 file changed, 56 insertions(+), 53 deletions(-)

(limited to 'webapp/components/signup/signup_controller.jsx')

diff --git a/webapp/components/signup/signup_controller.jsx b/webapp/components/signup/signup_controller.jsx
index a4ec26a5f..229e91762 100644
--- a/webapp/components/signup/signup_controller.jsx
+++ b/webapp/components/signup/signup_controller.jsx
@@ -31,16 +31,15 @@ export default class SignupController extends React.Component {
         let usedBefore = false;
 
         if (props.location.query) {
-            loading = true;
             const hash = props.location.query.h;
             const inviteId = props.location.query.id;
 
-            if (hash && hash.length > 0 && !UserStore.getCurrentUser()) {
+            if (inviteId) {
+                loading = true;
+            } else if (hash && !UserStore.getCurrentUser()) {
                 usedBefore = BrowserStore.getGlobalItem(hash);
-                loading = false;
             } else if (!inviteId && global.window.mm_config.EnableOpenServer !== 'true' && !UserStore.getNoAccounts()) {
                 noOpenServerError = true;
-                loading = false;
                 serverError = (
                     <FormattedMessage
                         id='signup_user_completed.no_open_server'
@@ -66,58 +65,62 @@ export default class SignupController extends React.Component {
             const data = this.props.location.query.d;
             const inviteId = this.props.location.query.id;
 
-            if ((inviteId && inviteId.length > 0) || (hash && hash.length > 0)) {
-                if (UserStore.getCurrentUser()) {
-                    Client.addUserToTeamFromInvite(
-                        data,
-                        hash,
-                        inviteId,
-                        (team) => {
-                            GlobalActions.emitInitialLoad(
-                                () => {
-                                    browserHistory.push('/' + team.name + '/channels/town-square');
-                                }
-                            );
-                        },
-                        (err) => {
-                            this.setState({ // eslint-disable-line react/no-did-mount-set-state
-                                serverError: err.message
-                            });
-                        }
-                    );
-                } else if (!this.state.usedBefore) {
-                    Client.getInviteInfo(
-                        inviteId,
-                        (inviteData) => {
-                            if (!inviteData) {
-                                return;
+            const userLoggedIn = UserStore.getCurrentUser() != null;
+
+            if ((inviteId || hash) && userLoggedIn) {
+                Client.addUserToTeamFromInvite(
+                    data,
+                    hash,
+                    inviteId,
+                    (team) => {
+                        GlobalActions.emitInitialLoad(
+                            () => {
+                                browserHistory.push('/' + team.name + '/channels/town-square');
                             }
+                        );
+                    },
+                    (err) => {
+                        this.setState({ // eslint-disable-line react/no-did-mount-set-state
+                            serverError: err.message
+                        });
+                    }
+                );
 
-                            this.setState({ // eslint-disable-line react/no-did-mount-set-state
-                                serverError: '',
-                                loading: false
-                            });
-                        },
-                        () => {
-                            this.setState({ // eslint-disable-line react/no-did-mount-set-state
-                                noOpenServerError: true,
-                                loading: false,
-                                serverError: (
-                                    <FormattedMessage
-                                        id='signup_user_completed.invalid_invite'
-                                        defaultMessage='The invite link was invalid.  Please speak with your Administrator to receive an invitation.'
-                                    />
-                                )
-                            });
+                return;
+            }
+
+            if (inviteId) {
+                Client.getInviteInfo(
+                    inviteId,
+                    (inviteData) => {
+                        if (!inviteData) {
+                            return;
                         }
-                    );
-                }
-            } else if (UserStore.getCurrentUser()) {
+
+                        this.setState({ // eslint-disable-line react/no-did-mount-set-state
+                            serverError: '',
+                            loading: false
+                        });
+                    },
+                    () => {
+                        this.setState({ // eslint-disable-line react/no-did-mount-set-state
+                            noOpenServerError: true,
+                            loading: false,
+                            serverError: (
+                                <FormattedMessage
+                                    id='signup_user_completed.invalid_invite'
+                                    defaultMessage='The invite link was invalid.  Please speak with your Administrator to receive an invitation.'
+                                />
+                            )
+                        });
+                    }
+                );
+
+                return;
+            }
+
+            if (userLoggedIn) {
                 browserHistory.push('/select_team');
-            } else {
-                this.setState({ // eslint-disable-line react/no-did-mount-set-state
-                    loading: false
-                });
             }
         }
     }
@@ -337,4 +340,4 @@ export default class SignupController extends React.Component {
 
 SignupController.propTypes = {
     location: React.PropTypes.object
-};
\ No newline at end of file
+};
-- 
cgit v1.2.3-1-g7c22

