From f22ec536231f6edc862928c8e06cb13404df5648 Mon Sep 17 00:00:00 2001 From: David Green Date: Thu, 23 Aug 2018 03:22:33 -0600 Subject: [PATCH] Authentication, App State, and basic App logic I'm implementing a production/development system., implementing an application state system that simply updates the state when the application is ready to be reloaded. Started to develop out my authentication system. Using a localStorage token and UID system for session control. --- .gitignore | 2 ++ src/App.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/.gitignore b/.gitignore index d30f40e..8f62aee 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ npm-debug.log* yarn-debug.log* yarn-error.log* +package-lock.json +package-lock.json diff --git a/src/App.js b/src/App.js index 0004a1f..a5180d4 100644 --- a/src/App.js +++ b/src/App.js @@ -12,7 +12,95 @@ import OptionsPanel from './com_OptionsPanel.js'; import Core from './com_Core.js'; import Footer from './com_Footer.js'; +//PRODUCTION BUILD CHECKLIST: +//Fork to staging +//Take out debug code +//Tune production settings +//Fork to production +//Upload changed production files. + class App extends Component { + + //constructor: Set default application state. + constructor( props ){ + super(props); + + this.appstate = { + applicationState : { + state: 'AUTH', //RUNNING, CLOSING + env: { + type: 'DEV', //PRO, BETA + build: '0.1.0.1', //Major release. Minor release. Update. Development build number. + settings: { + authenticated: true //false + } + }, + core: 'LOADING', //INV_MAIN, INV_COUNT, ORDER_OUT, ORDER_IN, FORECASTING + optionsPanel: 'CLOSED', //OPEN_APPSETTINGS, OPEN_USERSETTINGS + } + }; + + this.state = this.appstate; + } + + //updateState: Update the state with the current application state so the app will rerender. + updateState () { + this.setState(this.appstate); + } + + //componentWillMount: This is here to auth the user and load information on application start, perhaps to process runtime requests too. + componentWillMount(){ + //Auth user, load information, redirect if necessary. + + switch(this.state.applicationState.state){ + case 'AUTH': + //This is where the real magic happens. This is where the application is going to authenticate itself and redirect if not authenticated. + //It will then load the information that it neads. + + //Authentication + if(typeof(Storage) !== "undefined"){ + if(localStorage.token !== undefined && localStorage.UID !== undefined){ + //TODO: Add if statement to be able to turn off authenticated state. + $.ajax({ + //TODO: Put in server URL + url: '', + method: 'POST', + data: { + module: 'AUTH_USER', + authToken: localStorage.token, + UID: localStorage.UID + } + }).done(function (){ + //Function that is fired when the request comes back. + //TODO: Review Authentication. + //TODO: Load information + }.bind(this)); + }else{ + //User doesn't have access to this website. + //TODO: Redirect to login application. + if( this.appstate.applicationState.env.type == 'DEV' && + this.appstate.applicationState.env.settings.authenticated ){ + //TODO: Set localStorage state. + } + } + }else{ + //No localstorage... + //TODO: Redirect to not supported page. + } + + break; + case 'RUNNING': + //Might be cut because I can't think of a reason to calculate anything here right now. + break; + case 'CLOSING': + //Probably will be cut because I can't think of a reason for this to be here right now. + break; + default: + //This shouldn't happen. Notify server, review application variables, correct application, or reload. + break; + } + } + callback = (data) => { console.log(data); }