From 76a9485f0be1b92670a337117069b834c455fa47 Mon Sep 17 00:00:00 2001
From: Akinwale Ariwodola <akinwale@gmail.com>
Date: Thu, 16 Aug 2018 13:21:28 +0100
Subject: [PATCH] tweaks to the user email verification process

---
 app/src/component/AppNavigator.js |  4 ++
 app/src/page/splash/index.js      | 11 +++-
 app/src/page/splash/view.js       | 91 ++++++++++++++++++-------------
 3 files changed, 68 insertions(+), 38 deletions(-)

diff --git a/app/src/component/AppNavigator.js b/app/src/component/AppNavigator.js
index 479f90db..477ead4f 100644
--- a/app/src/component/AppNavigator.js
+++ b/app/src/component/AppNavigator.js
@@ -216,6 +216,10 @@ class AppWithNavigationState extends React.Component {
           this.setState({ emailVerifyDone: true });
           const message = emailVerifyErrorMessage ?
             String(emailVerifyErrorMessage) : 'Your email address was successfully verified.';
+          if (!emailVerifyErrorMessage) {
+            AsyncStorage.removeItem(Constants.KEY_FIRST_RUN_EMAIL);
+          }
+          AsyncStorage.removeItem(Constants.KEY_SHOULD_VERIFY_EMAIL);
           dispatch(doNotify({ message, displayType: ['toast'] }));
         }
       });
diff --git a/app/src/page/splash/index.js b/app/src/page/splash/index.js
index f1818532..a8eb503b 100644
--- a/app/src/page/splash/index.js
+++ b/app/src/page/splash/index.js
@@ -1,16 +1,25 @@
 import { connect } from 'react-redux';
 import { doBalanceSubscribe, doNotify } from 'lbry-redux';
-import { doAuthenticate, doUserEmailVerify, doUserEmailVerifyFailure, selectUser } from 'lbryinc';
+import {
+  doAuthenticate,
+  doUserEmailToVerify,
+  doUserEmailVerify,
+  doUserEmailVerifyFailure,
+  selectUser,
+  selectEmailToVerify
+} from 'lbryinc';
 import SplashScreen from './view';
 
 const select = state => ({
   user: selectUser(state),
+  emailToVerify: selectEmailToVerify(state)
 });
 
 const perform = dispatch => ({
     authenticate: (appVersion, deviceId) => dispatch(doAuthenticate(appVersion, deviceId)),
     balanceSubscribe: () => dispatch(doBalanceSubscribe()),
     notify: data => dispatch(doNotify(data)),
+    setEmailToVerify: email => dispatch(doUserEmailToVerify(email)),
     verifyUserEmail: (token, recaptcha) => dispatch(doUserEmailVerify(token, recaptcha)),
     verifyUserEmailFailure: error => dispatch(doUserEmailVerifyFailure(error)),
 });
diff --git a/app/src/page/splash/view.js b/app/src/page/splash/view.js
index c8394079..56214aac 100644
--- a/app/src/page/splash/view.js
+++ b/app/src/page/splash/view.js
@@ -57,47 +57,62 @@ class SplashScreen extends React.PureComponent {
     });
   }
 
-  componentWillUpdate(nextProps) {
-    const { navigation, verifyUserEmail, verifyUserEmailFailure } = this.props;
+  componentWillReceiveProps(nextProps) {
+    const {
+      emailToVerify,
+      navigation,
+      setEmailToVerify,
+      verifyUserEmail,
+      verifyUserEmailFailure
+    } = this.props;
     const { user } = nextProps;
-    if (this.state.daemonReady && this.state.shouldAuthenticate && user && user.id) {
-      // user is authenticated, navigate to the main view
-      const resetAction = NavigationActions.reset({
-        index: 0,
-        actions: [
-          NavigationActions.navigate({ routeName: 'Main'})
-        ]
-      });
-      navigation.dispatch(resetAction);
 
-      const launchUrl = navigation.state.params.launchUrl || this.state.launchUrl;
-      if (launchUrl) {
-        if (launchUrl.startsWith('lbry://?verify=')) {
-          let verification = {};
-          try {
-            verification = JSON.parse(atob(launchUrl.substring(15)));
-          } catch (error) {
-            console.log(error);
+    if (this.state.daemonReady && this.state.shouldAuthenticate && user && user.id) {
+      this.setState({ shouldAuthenticate: false }, () => {
+        AsyncStorage.getItem(Constants.KEY_FIRST_RUN_EMAIL).then(email => {
+          if (email) {
+            setEmailToVerify(email);
           }
-          if (verification.token && verification.recaptcha) {
-            AsyncStorage.setItem(Constants.KEY_SHOULD_VERIFY_EMAIL, 'true');
-            try {
-              verifyUserEmail(verification.token, verification.recaptcha);
-            } catch (error) {
-              const message = 'Invalid Verification Token';
-              verifyUserEmailFailure(message);
-              notify({ message, displayType: ['toast'] });
+
+          // user is authenticated, navigate to the main view
+          const resetAction = NavigationActions.reset({
+            index: 0,
+            actions: [
+              NavigationActions.navigate({ routeName: 'Main'})
+            ]
+          });
+          navigation.dispatch(resetAction);
+
+          const launchUrl = navigation.state.params.launchUrl || this.state.launchUrl;
+          if (launchUrl) {
+            if (launchUrl.startsWith('lbry://?verify=')) {
+              let verification = {};
+              try {
+                verification = JSON.parse(atob(launchUrl.substring(15)));
+              } catch (error) {
+                console.log(error);
+              }
+              if (verification.token && verification.recaptcha) {
+                AsyncStorage.setItem(Constants.KEY_SHOULD_VERIFY_EMAIL, 'true');
+                try {
+                  verifyUserEmail(verification.token, verification.recaptcha);
+                } catch (error) {
+                  const message = 'Invalid Verification Token';
+                  verifyUserEmailFailure(message);
+                  notify({ message, displayType: ['toast'] });
+                }
+              } else {
+                notify({
+                  message: 'Invalid Verification URI',
+                  displayType: ['toast'],
+                });
+              }
+            } else {
+              navigation.navigate({ routeName: 'File', key: launchUrl, params: { uri: launchUrl } });
             }
-          } else {
-            notify({
-              message: 'Invalid Verification URI',
-              displayType: ['toast'],
-            });
           }
-        } else {
-          navigation.navigate({ routeName: 'File', key: launchUrl, params: { uri: launchUrl } });
-        }
-      }
+        });
+      });
     }
   }
 
@@ -129,6 +144,7 @@ class SplashScreen extends React.PureComponent {
 
         balanceSubscribe();
         NativeModules.VersionInfo.getAppVersion().then(appVersion => {
+          this.setState({ shouldAuthenticate: true });
           if (NativeModules.UtilityModule) {
             // authenticate with the device ID if the method is available
             NativeModules.UtilityModule.getDeviceId().then(deviceId => {
@@ -137,9 +153,9 @@ class SplashScreen extends React.PureComponent {
           } else {
             authenticate(appVersion);
           }
-          this.setState({ shouldAuthenticate: true });
         });
       });
+
       return;
     }
 
@@ -177,6 +193,7 @@ class SplashScreen extends React.PureComponent {
         details: 'Initializing LBRY service...'
       });
     }
+
     setTimeout(() => {
       this.updateStatus();
     }, 500);