11import React from 'react'
22import PropTypes from 'prop-types'
33import { browserHistory , withRouter } from 'react-router'
4+ import { decodeToken } from 'jsontokens'
45import App from '../App'
56import {
67 selectConnectedStorageAtLeastOnce ,
@@ -47,7 +48,10 @@ import {
4748 Password ,
4849 Success ,
4950 Username ,
50- RecoveryInformationScreen
51+ RecoveryInformationScreen ,
52+ GaiaHubSelect ,
53+ CustomGaiaHub ,
54+ RecommendedGaiaHub
5155} from './views'
5256import { notify } from 'reapop'
5357
@@ -62,23 +66,32 @@ const views = [
6266 Initial ,
6367 Username ,
6468 Password ,
69+ GaiaHubSelect ,
70+ CustomGaiaHub ,
6571 Email ,
6672 RecoveryInformationScreen ,
67- Success
73+ Success ,
74+ RecommendedGaiaHub
6875]
6976const VIEWS = {
7077 INITIAL : 0 ,
7178 USERNAME : 1 ,
7279 PASSWORD : 2 ,
73- EMAIL : 3 ,
74- INFO : 4 ,
75- HOORAY : 5
80+ GAIA : 3 ,
81+ RECOMMENDED_GAIA_HUB : 8 ,
82+ CUSTOMHUB : 4 ,
83+ EMAIL : 5 ,
84+ INFO : 6 ,
85+ HOORAY : 7
7686}
7787const VIEW_EVENTS = {
7888 [ VIEWS . INITIAL ] : 'Onboarding - Initial' ,
7989 [ VIEWS . EMAIL ] : 'Onboarding - Email' ,
8090 [ VIEWS . PASSWORD ] : 'Onboarding - Password' ,
8191 [ VIEWS . USERNAME ] : 'Onboarding - Username' ,
92+ [ VIEWS . RECOMMENDED_GAIA_HUB ] : 'Onboarding - Recommended Gaia Hub' ,
93+ [ VIEWS . GAIA ] : 'Onboarding - Gaia Hub Select' ,
94+ [ VIEWS . CUSTOMHUB ] : 'Onboarding - Custom Gaia Hub' ,
8295 [ VIEWS . INFO ] : 'Onboarding - Info' ,
8396 [ VIEWS . HOORAY ] : 'Onboarding - Complete'
8497}
@@ -132,7 +145,10 @@ class Onboarding extends React.Component {
132145 restoreEmailError : null ,
133146 loading : false ,
134147 view : VIEWS . INITIAL ,
135- usernameRegistrationInProgress : false
148+ usernameRegistrationInProgress : false ,
149+ hubURL : '' ,
150+ decodedAuthToken : null ,
151+ customHubError : null
136152 }
137153 updateValue = ( key , value ) => {
138154 this . setState ( { [ key ] : value } )
@@ -169,12 +185,61 @@ class Onboarding extends React.Component {
169185 * Submit our password
170186 */
171187 submitPassword = async ( ) => {
188+ const decodedAuthRequest = this . getDecodedAuthRequest ( )
189+ if ( decodedAuthRequest && ( decodedAuthRequest . solicitGaiaHubUrl || decodedAuthRequest . recommendedGaiaHub ) ) {
190+ if ( decodedAuthRequest . recommendedGaiaHubUrl ) {
191+ this . updateView ( VIEWS . RECOMMENDED_GAIA_HUB )
192+ } else {
193+ this . updateView ( VIEWS . GAIA )
194+ }
195+ } else {
196+ this . setState ( {
197+ loading : true
198+ } )
199+ await this . createAccount ( )
200+ this . updateView ( VIEWS . EMAIL )
201+ }
202+ }
203+
204+ /**
205+ * Set gaia hub
206+ */
207+ defaultGaiaHub = async ( ) => {
172208 this . setState ( {
173209 loading : true
174210 } )
175211 await this . createAccount ( )
176212 this . updateView ( VIEWS . EMAIL )
177213 }
214+
215+ /**
216+ * Set a custom gaia hub
217+ */
218+ customGaiaHub = async ( ) => {
219+ this . setState ( {
220+ loading : true
221+ } )
222+ const success = await this . validateGaiaURL ( )
223+ if ( success ) {
224+ await this . createAccount ( )
225+ this . updateView ( VIEWS . EMAIL )
226+ }
227+ }
228+
229+ submitRecommendedGaiaHub = async ( ) => {
230+ const decodedAuthRequest = this . getDecodedAuthRequest ( )
231+ this . setState ( {
232+ loading : true ,
233+ hubURL : decodedAuthRequest . recommendedGaiaHubUrl
234+ } , async ( ) => {
235+ const success = await this . validateGaiaURL ( )
236+ if ( success ) {
237+ await this . createAccount ( )
238+ this . updateView ( VIEWS . EMAIL )
239+ }
240+ } )
241+ }
242+
178243 /**
179244 * Submit Username
180245 * This will create our account and then register a name and connect storage
@@ -203,7 +268,7 @@ class Onboarding extends React.Component {
203268 // Create new ID and owner address and then set to default
204269 await this . createNewIdAndSetDefault ( )
205270 // Connect our default storage
206- await this . props . connectStorage ( )
271+ await this . props . connectStorage ( this . state . hubURL )
207272 // Register the username
208273 await this . registerUsername ( )
209274 }
@@ -357,6 +422,33 @@ class Onboarding extends React.Component {
357422 } )
358423 }
359424
425+ async validateGaiaURL ( ) {
426+ const { hubURL } = this . state
427+ if ( ! / ^ h t t p s : \/ \/ / . test ( hubURL ) ) {
428+ this . setState ( {
429+ loading : false ,
430+ customHubError : 'A Gaia Hub URL must use SSL.'
431+ } )
432+ return false
433+ }
434+
435+ try {
436+ await fetch ( `${ hubURL } /hub_info` )
437+ } catch ( error ) {
438+ this . setState ( {
439+ loading : false ,
440+ customHubError : 'Your Gaia URL does not appear to be a valid Gaia hub.'
441+ } )
442+ return false
443+ }
444+
445+ this . setState ( {
446+ customHubError : null
447+ } )
448+
449+ return true
450+ }
451+
360452 /**
361453 * Decode and save auth request
362454 *
@@ -380,6 +472,13 @@ class Onboarding extends React.Component {
380472 }
381473 }
382474
475+ getDecodedAuthRequest ( ) {
476+ const authRequest = this . props . authRequest || this . state . authRequest
477+ if ( ! authRequest ) return null
478+ const decodedAuthRequest = decodeToken ( authRequest ) . payload
479+ return decodedAuthRequest
480+ }
481+
383482 /**
384483 * Check for Auth Request
385484 * this returns the authRequest query param if one exists
@@ -489,11 +588,13 @@ class Onboarding extends React.Component {
489588 // If we were waiting on an appManifest, we haven't tracked yet.
490589 this . trackViewEvent ( this . state . view , nextProps . appManifest )
491590 }
591+ const decodedAuthRequest = this . getDecodedAuthRequest ( )
592+ this . setState ( { decodedAuthRequest } )
492593 }
493594
494595 render ( ) {
495596 const { appManifest } = this . props
496- const { email, password, username, emailSubmitted, view } = this . state
597+ const { email, password, username, emailSubmitted, view, decodedAuthRequest } = this . state
497598
498599 const app = formatAppManifest ( appManifest )
499600
@@ -525,6 +626,24 @@ class Onboarding extends React.Component {
525626 updateValue : this . updateValue
526627 }
527628 } ,
629+ {
630+ show : VIEWS . GAIA ,
631+ props : {
632+ loading : this . state . loading ,
633+ next : this . defaultGaiaHub ,
634+ customHub : ( ) => this . updateView ( VIEWS . CUSTOMHUB )
635+ }
636+ } ,
637+ {
638+ show : VIEWS . CUSTOMHUB ,
639+ props : {
640+ loading : this . state . loading ,
641+ hubURL : this . state . hubURL ,
642+ next : this . customGaiaHub ,
643+ updateValue : this . updateValue ,
644+ customHubError : this . state . customHubError
645+ }
646+ } ,
528647 {
529648 show : VIEWS . EMAIL ,
530649 props : {
@@ -561,6 +680,19 @@ class Onboarding extends React.Component {
561680 goToRecovery : this . goToBackup ,
562681 finish : ( ) => this . finish ( )
563682 }
683+ } ,
684+ {
685+ show : VIEWS . RECOMMENDED_GAIA_HUB ,
686+ props : {
687+ recommendedGaiaHubUrl : decodedAuthRequest && decodedAuthRequest . recommendedGaiaHubUrl ,
688+ updateValue : this . updateValue ,
689+ next : ( ) => this . submitRecommendedGaiaHub ( ) ,
690+ customHub : ( ) => this . updateView ( VIEWS . CUSTOMHUB ) ,
691+ defaultHub : this . defaultGaiaHub ,
692+ loading : this . state . loading ,
693+ customHubError : this . state . customHubError ,
694+ app
695+ }
564696 }
565697 ]
566698
@@ -613,7 +745,8 @@ Onboarding.propTypes = {
613745 verifyAuthRequestAndLoadManifest : PropTypes . func . isRequired ,
614746 encryptedBackupPhrase : PropTypes . string ,
615747 notify : PropTypes . func . isRequired ,
616- connectStorage : PropTypes . func . isRequired
748+ connectStorage : PropTypes . func . isRequired ,
749+ authRequest : PropTypes . string
617750}
618751
619752export default withRouter (
0 commit comments