From 3727e275c468d73621fd73988ca15589013fe2c8 Mon Sep 17 00:00:00 2001 From: Alex Liebowitz Date: Sat, 1 Apr 2017 02:36:45 -0400 Subject: [PATCH] Update registration logic and move to app.js --- ui/js/app.js | 42 +++++++++++++++++++++++++++++++++++++++++- ui/js/main.js | 24 +----------------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/ui/js/app.js b/ui/js/app.js index 517008afa..353846266 100644 --- a/ui/js/app.js +++ b/ui/js/app.js @@ -88,6 +88,32 @@ var App = React.createClass({ pageArgs: pageArgs === undefined ? null : pageArgs }; }, + updateRegistrationStatus: function() { + if (localStorage.getItem('accessToken')) { + this.setState({ + registrationCheckComplete: true, + }); + } else { + lbry.status().then(({installation_id}) => { + installation_id += parseInt(Date.now(), 10); // temp + installation_id += "X".repeat(96 - installation_id.length); // temp + lbryio.call('user_install', 'exists', {app_id: installation_id}).then((userExists) => { + // TODO: deal with case where user exists already with the same app ID, but we have no access token. + // Possibly merge in to the existing user with the same app ID. + lbryio.call('user', 'new', { + language: 'en', + app_id: installation_id, + }, 'post').then(({ID}) => { + localStorage.setItem('accessToken', ID); + this.setState({ + registrationCheckComplete: true, + justRegistered: true, + }); + }); + }) + }); + } + }, getInitialState: function() { var match, param, val, viewingPage, pageArgs, drawerOpenRaw = sessionStorage.getItem('drawerOpen'); @@ -98,9 +124,17 @@ var App = React.createClass({ modal: null, downloadProgress: null, downloadComplete: false, + registrationCheckComplete: null, + justRegistered: false, }); }, componentWillMount: function() { + if (!localStorage.getItem('accessToken') && window.location.search != '?discover') { + // User isn't registered but somehow made it to a page other than Discover, so send them to + // Discover to get them registered and show them the welcome screen. + window.location.search = '?discover'; + } + document.addEventListener('unhandledError', (event) => { this.alertError(event.detail); }); @@ -138,6 +172,8 @@ var App = React.createClass({ }); }); } + + this.updateRegistrationStatus(); }, openDrawer: function() { sessionStorage.setItem('drawerOpen', true); @@ -297,10 +333,14 @@ var App = React.createClass({ return ; case 'discover': default: - return ; + return ; } }, render: function() { + if (!this.state.registrationCheckComplete) { + return null; + } + var mainContent = this.getMainContent(), headerLinks = this.getHeaderLinks(), searchQuery = this.state.viewingPage == 'discover' && this.state.pageArgs ? this.state.pageArgs : ''; diff --git a/ui/js/main.js b/ui/js/main.js index 9e10b1b41..ea8d4d0a5 100644 --- a/ui/js/main.js +++ b/ui/js/main.js @@ -47,26 +47,4 @@ let init = function() { } }; -if (localStorage.getItem('accessToken') || window.location.search == '?register') { - // User is already registered, or on the registration page - init(); -} else { - // Send - lbry.status().then(({installation_id}) => { - installation_id += parseInt(Date.now(), 10); // temp - installation_id += "X".repeat(96 - installation_id.length); // temp - lbryio.call('user_install', 'exists', {app_id: installation_id}).then((userExists) => { - if (userExists) { - /* TODO: somehow user exists with the same installation ID, but we don't have the token recorded. What do we do here? */ - } else { - lbryio.call('user', 'new', { - language: 'en', - app_id: installation_id, - }, 'post').then(({ID}) => { - localStorage.setItem('accessToken', ID); - window.location = '?register'; - }); - } - }); - }); -} +init();