From afa3cf00428ef17e2cdc49f2f89ec232dfe1e8ab Mon Sep 17 00:00:00 2001 From: "Andrew Revinsky (DART)" Date: Sat, 12 Mar 2016 02:53:37 +0300 Subject: [PATCH] Sign In and Register actions --- js-frontend/src/App.js | 7 +- js-frontend/src/actions/configure.js | 23 ++++--- js-frontend/src/actions/signIn.js | 19 ++---- js-frontend/src/actions/signOut.js | 40 +++++++++++ js-frontend/src/actions/signUp.js | 46 +++++++++++++ js-frontend/src/components/HeaderLinks.js | 66 ++++++++++++++++++ .../src/components/partials/Container.js | 11 +-- js-frontend/src/constants/ACTION_TYPES.js | 4 ++ .../src/controls/bootstrap/EmailSignInForm.js | 24 +++---- .../src/controls/bootstrap/EmailSignUpForm.js | 68 +++++++++++-------- js-frontend/src/controls/bootstrap/Input.js | 2 +- js-frontend/src/reducers/auth/index.js | 2 + js-frontend/src/reducers/auth/signup.js | 43 ++++++++++++ js-frontend/src/utils/api.js | 34 ++++++++++ js-frontend/src/utils/fetch.js | 4 +- js-frontend/src/utils/parseUrl.js | 2 +- js-frontend/src/utils/readProp.js | 16 +++++ js-frontend/src/utils/root.js | 2 + js-frontend/src/utils/sessionStorage.js | 48 +++++++------ 19 files changed, 360 insertions(+), 101 deletions(-) create mode 100644 js-frontend/src/actions/signOut.js create mode 100644 js-frontend/src/actions/signUp.js create mode 100644 js-frontend/src/components/HeaderLinks.js create mode 100644 js-frontend/src/reducers/auth/signup.js create mode 100644 js-frontend/src/utils/api.js create mode 100644 js-frontend/src/utils/readProp.js diff --git a/js-frontend/src/App.js b/js-frontend/src/App.js index f68871e..5e1f0f3 100644 --- a/js-frontend/src/App.js +++ b/js-frontend/src/App.js @@ -86,15 +86,16 @@ export function initialize({cookies, isServer, currentLocation, userAgent} = {}) return store.dispatch(reduxAuthConfigure([ { default: { - apiUrl: '/', - emailSignInPath: 'login', - emailRegistrationPath: 'customers' + //apiUrl: '/', + emailSignInPath: '/login', + emailRegistrationPath: '/customers' } } ], { cookies, isServer, currentLocation, + storage: 'localStorage', tokenFormat: { "access-token": "{{ access-token }}" }, diff --git a/js-frontend/src/actions/configure.js b/js-frontend/src/actions/configure.js index 096e5de..b59751a 100644 --- a/js-frontend/src/actions/configure.js +++ b/js-frontend/src/actions/configure.js @@ -25,6 +25,7 @@ import {applyConfig} from "../utils/clientSettings"; import {destroySession} from "../utils/sessionStorage"; import getRedirectInfo from "../utils/parseUrl"; import {pushState} from "redux-router"; +import root from '../utils/root'; export const SET_ENDPOINT_KEYS = "SET_ENDPOINT_KEYS"; export const STORE_CURRENT_ENDPOINT_KEY = "STORE_CURRENT_ENDPOINT_KEY"; @@ -40,6 +41,7 @@ export function storeCurrentEndpointKey(currentEndpointKey) { export function configure(endpoint={}, settings={}) { return dispatch => { + // don't render anything for OAuth redirects if (settings.currentLocation && settings.currentLocation.match(/blank=true/)) { return Promise.resolve({blank: true}); @@ -53,7 +55,7 @@ export function configure(endpoint={}, settings={}) { user, headers; - let {authRedirectPath, authRedirectHeaders} = getRedirectInfo(window.location); + //let { authRedirectPath, authRedirectHeaders} = getRedirectInfo(root.location); // TODO: FiX! //if (authRedirectPath) { @@ -64,18 +66,21 @@ export function configure(endpoint={}, settings={}) { //if (authRedirectHeaders && authRedirectHeaders["access-token"]) { if (currentHeaders && currentHeaders["access-token"]) { - settings.initialCredentials = { - ...(settings.initialCredentials || {}), - ...authRedirectHeaders, - ...currentHeaders - }; + + //settings.initialCredentials = { + // ...(settings.initialCredentials || {}), + // ...authRedirectHeaders, + // ...currentHeaders + //}; + } else { + destroySession(); } // if tokens were invalidated by server, make sure to clear browser // credentials - if (!settings.initialCredentials) { - destroySession(); - } + //if (!settings.initialCredentials) { + // destroySession(); + //} promise = Promise.resolve(applyConfig({ dispatch, endpoint, settings })); diff --git a/js-frontend/src/actions/signIn.js b/js-frontend/src/actions/signIn.js index 2ec0998..516a93f 100644 --- a/js-frontend/src/actions/signIn.js +++ b/js-frontend/src/actions/signIn.js @@ -2,19 +2,20 @@ * Created by andrew on 26/02/16. */ import { - getEmailSignInUrl, setCurrentEndpointKey, getCurrentEndpointKey } from "../utils/sessionStorage"; import { entityReceived } from './entities'; import { storeCurrentEndpointKey } from "./configure"; -import { parseResponse } from "../utils/handleFetchResponse"; -import fetch from "../utils/fetch"; +//import { parseResponse } from "../utils/handleFetchResponse"; +//import fetch from "../utils/fetch"; + +import { apiSignIn } from '../utils/api'; import T from '../constants/ACTION_TYPES'; -import root from '../utils/root'; +//import root from '../utils/root'; export function emailSignInFormUpdate(key, value) { return { type: T.AUTH.SIGN_IN_FORM_UPDATE, key, value }; @@ -47,14 +48,7 @@ export function emailSignIn(body) { dispatch(emailSignInStart()); - return fetch(getEmailSignInUrl(currentEndpointKey), { - headers: { - "Accept": "application/json", - "Content-Type": "application/json" - }, - method: "post", - body: root.JSON.stringify(body) - }).then(parseResponse) + return apiSignIn(body) .then(function(data = {}) { const { id, customerInfo } = data; if (id && customerInfo) { @@ -62,6 +56,7 @@ export function emailSignIn(body) { ...customerInfo, uid: id }; + debugger; dispatch(entityReceived(id, user)); return user; } diff --git a/js-frontend/src/actions/signOut.js b/js-frontend/src/actions/signOut.js new file mode 100644 index 0000000..e4fc4e9 --- /dev/null +++ b/js-frontend/src/actions/signOut.js @@ -0,0 +1,40 @@ +/** + * Created by andrew on 11/03/16. + */ +import { + getEmailSignInUrl, + setCurrentEndpointKey, + getCurrentEndpointKey +} from "../utils/sessionStorage"; + +import {destroySession} from "../utils/sessionStorage"; + + +import { entityReceived } from './entities'; +import { storeCurrentEndpointKey } from "./configure"; +import { parseResponse } from "../utils/handleFetchResponse"; +import fetch from "../utils/fetch"; + +import T from '../constants/ACTION_TYPES'; + +import root from '../utils/root'; + +export function signOutStart() { + return { type: T.AUTH.SIGN_OUT_START }; +} + +export function signOutComplete() { + return { type: T.AUTH.SIGN_OUT_COMPLETE }; +} + +export function signOut() { + return dispatch => { + + dispatch(signOutStart()); + + destroySession(); + + dispatch(signOutComplete()); + + }; +} \ No newline at end of file diff --git a/js-frontend/src/actions/signUp.js b/js-frontend/src/actions/signUp.js new file mode 100644 index 0000000..b78e72a --- /dev/null +++ b/js-frontend/src/actions/signUp.js @@ -0,0 +1,46 @@ +/** + * Created by andrew on 11/03/16. + */ +import { + getEmailSignUpUrl +} from "../utils/sessionStorage"; + + +import { entityReceived } from './entities'; +import { storeCurrentEndpointKey } from "./configure"; +//import { parseResponse } from "../utils/handleFetchResponse"; +import { apiSignUp } from "../utils/api"; +import { emailSignIn } from './signIn'; + +import T from '../constants/ACTION_TYPES'; + +export function emailSignUpFormUpdate(key, value) { + return { type: T.AUTH.SIGN_UP_FORM_UPDATE, key, value }; +} + +export function emailSignUpStart() { + return { type: T.AUTH.SIGN_UP_START }; +} + +export function emailSignUpComplete(user) { + return { type: T.AUTH.SIGN_UP_COMPLETE, user }; +} + +export function emailSignUpError(errors) { + return { type: T.AUTH.SIGN_UP_ERROR, errors }; +} + +export function emailSignUp(body) { + return dispatch => { + dispatch(emailSignUpStart()); + + return apiSignUp(body) + .then(({data}) => { + dispatch(emailSignUpComplete(data)); + const { email } = body; + return dispatch(emailSignIn({ email })); + }) + .catch(({errors}) => dispatch(emailSignUpError(errors))); + + }; +} \ No newline at end of file diff --git a/js-frontend/src/components/HeaderLinks.js b/js-frontend/src/components/HeaderLinks.js new file mode 100644 index 0000000..dabf0be --- /dev/null +++ b/js-frontend/src/components/HeaderLinks.js @@ -0,0 +1,66 @@ +/** + * Created by andrew on 11/03/16. + */ +import React from 'react'; +import { connect } from 'react-redux'; +import { LinkContainer } from "react-router-bootstrap"; + +import read from '../utils/readProp'; + +//import { PageHeader, OverlayTrigger, Tooltip, Grid, Col, Row, Nav, NavItem, ButtonGroup, Button, Table } from "react-bootstrap"; +import * as BS from "react-bootstrap"; +import { Link, IndexLink } from "react-router"; + +import { signOut } from '../actions/signOut'; + + +export class HeaderLinks extends React.Component { + + signOut(evt, key) { + debugger; + signOut(); + } + + render() { + + let buttonSet = null; + const isSignedIn = read(this.props.auth, 'user.isSignedIn', false); + const { location } = this.props.router; + const isRegister = location.pathname == '/register'; + const isLogin = location.pathname == '/signin'; + + const condition = isSignedIn ? 2 : (isRegister ? 1 : 0); + + switch (condition) { + case 0: + buttonSet = ( + Register + ); + break; + case 1: + buttonSet = ( + Log In + ); + break; + case 2: + buttonSet = (Sign Out); + break; + } + + return ( + + { buttonSet } + + ); + } +} + +export default connect(({ + //dispatch, + router, + auth + }) => ({ + //dispatch, + router, + auth +}))(HeaderLinks); diff --git a/js-frontend/src/components/partials/Container.js b/js-frontend/src/components/partials/Container.js index a34c902..76e2778 100644 --- a/js-frontend/src/components/partials/Container.js +++ b/js-frontend/src/components/partials/Container.js @@ -4,9 +4,12 @@ import React, { PropTypes } from "react"; import { Grid, Col, Navbar, NavItem, Nav, NavbarBrand, Footer } from "react-bootstrap"; import { LinkContainer } from "react-router-bootstrap"; + +import HeaderLinks from '../HeaderLinks'; + //import { SignOutButton } from "redux-auth/bootstrap-theme"; -const SignOutButton = () => (
SignOutButton!
); +//const SignOutButton = () => (
SignOutButton!
); //if (!global.__SERVER__ && !global.__TEST__) { @@ -33,9 +36,9 @@ class Container extends React.Component { Account - +
+ +
diff --git a/js-frontend/src/constants/ACTION_TYPES.js b/js-frontend/src/constants/ACTION_TYPES.js index 458d469..c4d262d 100644 --- a/js-frontend/src/constants/ACTION_TYPES.js +++ b/js-frontend/src/constants/ACTION_TYPES.js @@ -15,6 +15,10 @@ export default defineActionTypes({ SIGN_IN_COMPLETE SIGN_IN_ERROR SIGN_IN_FORM_UPDATE + SIGN_UP_START + SIGN_UP_COMPLETE + SIGN_UP_ERROR + SIGN_UP_FORM_UPDATE SIGN_OUT_START SIGN_OUT_COMPLETE `, diff --git a/js-frontend/src/controls/bootstrap/EmailSignInForm.js b/js-frontend/src/controls/bootstrap/EmailSignInForm.js index c091bed..f901129 100644 --- a/js-frontend/src/controls/bootstrap/EmailSignInForm.js +++ b/js-frontend/src/controls/bootstrap/EmailSignInForm.js @@ -11,6 +11,8 @@ import ButtonLoader from "./ButtonLoader"; import { Glyphicon } from "react-bootstrap"; import { connect } from "react-redux"; +import read from '../../utils/readProp'; + import { emailSignInFormUpdate, emailSignIn } from "../../actions/signIn"; /* @@ -25,19 +27,6 @@ import { emailSignInFormUpdate, emailSignIn } from "../../actions/signIn"; {...this.props.inputProps.password} /> */ -function read(src, path = '', defaultVal = '') { - const [pathItem = null, ...rest] = path.split('.'); - if (pathItem === null ) { - return src; - } else if (rest.length === 0) { - if (!src) { return defaultVal; } - return src[pathItem]; - } else { - if (!src) { return defaultVal; } - return read(src[pathItem], rest.join('.')); - } -} - class EmailSignInForm extends React.Component { static propTypes = { @@ -64,17 +53,20 @@ class EmailSignInForm extends React.Component { handleSubmit (event) { event.preventDefault(); let formData = { ...this.props.auth.signIn.form }; - this.props.dispatch(emailSignIn(formData, null)); + this.props.dispatch(emailSignIn(formData)); } render () { try { - let disabled = ( + const disabled = ( this.props.auth.user.isSignedIn || this.props.auth.signIn.loading ); + //const error = read(this.props.auth, 'signIn.errors.email', null); + //debugger; + return (
@@ -85,7 +77,7 @@ class EmailSignInForm extends React.Component { name="email" disabled={disabled} value={read(this.props.auth, 'signIn.form.email', '')} - errors={read(this.props.auth, 'signIn.errors.email', null)} + errors={read(this.props.auth, 'signIn.errors.email', {})} onChange={this.handleInput.bind(this, "email")} {...this.props.inputProps.email} /> diff --git a/js-frontend/src/controls/bootstrap/EmailSignUpForm.js b/js-frontend/src/controls/bootstrap/EmailSignUpForm.js index be1bda2..23aaa34 100644 --- a/js-frontend/src/controls/bootstrap/EmailSignUpForm.js +++ b/js-frontend/src/controls/bootstrap/EmailSignUpForm.js @@ -9,11 +9,12 @@ import ButtonLoader from "./ButtonLoader"; import IndexPanel from "./../../components/partials/IndexPanel"; import { customerInfoMap } from '../../entities/formToPayloadMappers'; +import read from '../../utils/readProp'; + import { Glyphicon } from "react-bootstrap"; import { connect } from "react-redux"; -const emailSignUpFormUpdate = () => {debugger; }, - emailSignUp = () => {debugger; }; +import {emailSignUpFormUpdate, emailSignUp} from '../../actions/signUp'; class EmailSignUpForm extends React.Component { @@ -44,22 +45,25 @@ class EmailSignUpForm extends React.Component { } handleInput (key, val) { - this.props.dispatch(emailSignUpFormUpdate(this.getEndpoint(), key, val)); + this.props.dispatch(emailSignUpFormUpdate(key, val)); } handleSubmit (event) { event.preventDefault(); - let formData = this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form"]).toJS(); + + let formData = { ...this.props.auth.signUp.form }; + console.log(customerInfoMap(formData)); - this.props.dispatch(emailSignUp(customerInfoMap(formData), this.getEndpoint())); + this.props.dispatch(emailSignUp(customerInfoMap(formData))); } render () { - let disabled = ( - this.props.auth.getIn(["user", "isSignedIn"]) || - this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "loading"]) - ); + try { + const disabled = ( + this.props.auth.user.isSignedIn || + this.props.auth.signUp.loading + ); return ( @@ -82,8 +86,8 @@ class EmailSignUpForm extends React.Component { placeholder="Last name" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "lname"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "lname"])} + value={read(this.props.auth, 'signUp.form.lname', '')} + errors={read(this.props.auth, 'signUp.errors.lname', {})} onChange={this.handleInput.bind(this, "lname")} {...this.props.inputProps.lname} /> @@ -92,8 +96,8 @@ class EmailSignUpForm extends React.Component { placeholder="Email" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "email"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "email"])} + value={read(this.props.auth, 'signUp.form.email', '')} + errors={read(this.props.auth, 'signUp.errors.email', {})} onChange={this.handleInput.bind(this, "email")} {...this.props.inputProps.email} /> @@ -106,8 +110,8 @@ class EmailSignUpForm extends React.Component { placeholder="SSN" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "ssn"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "ssn"])} + value={read(this.props.auth, 'signUp.form.ssn', '')} + errors={read(this.props.auth, 'signUp.errors.ssn', {})} onChange={this.handleInput.bind(this, "ssn")} {...this.props.inputProps.ssn} /> @@ -116,8 +120,8 @@ class EmailSignUpForm extends React.Component { placeholder="Phone" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "phone"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "phone"])} + value={read(this.props.auth, 'signUp.form.phone', '')} + errors={read(this.props.auth, 'signUp.errors.phone', {})} onChange={this.handleInput.bind(this, "phone")} {...this.props.inputProps.phone} /> @@ -126,8 +130,8 @@ class EmailSignUpForm extends React.Component { placeholder="Address 1" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "address1"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "address1"])} + value={read(this.props.auth, 'signUp.form.address1', '')} + errors={read(this.props.auth, 'signUp.errors.address1', {})} onChange={this.handleInput.bind(this, "address1")} {...this.props.inputProps.address1} /> @@ -136,8 +140,8 @@ class EmailSignUpForm extends React.Component { placeholder="Address 2" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "address2"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "address2"])} + value={read(this.props.auth, 'signUp.form.address2', '')} + errors={read(this.props.auth, 'signUp.errors.address2', {})} onChange={this.handleInput.bind(this, "address2")} {...this.props.inputProps.address2} /> @@ -146,8 +150,8 @@ class EmailSignUpForm extends React.Component { placeholder="City" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "city"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "city"])} + value={read(this.props.auth, 'signUp.form.city', '')} + errors={read(this.props.auth, 'signUp.errors.city', {})} onChange={this.handleInput.bind(this, "city")} {...this.props.inputProps.city} /> @@ -156,8 +160,8 @@ class EmailSignUpForm extends React.Component { placeholder="State" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "state"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "state"])} + value={read(this.props.auth, 'signUp.form.state', '')} + errors={read(this.props.auth, 'signUp.errors.state', {})} onChange={this.handleInput.bind(this, "state")} {...this.props.inputProps.state} /> @@ -166,8 +170,8 @@ class EmailSignUpForm extends React.Component { placeholder="ZIP" className="email-sign-up-email" disabled={disabled} - value={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "form", "zip"])} - errors={this.props.auth.getIn(["emailSignUp", this.getEndpoint(), "errors", "zip"])} + value={read(this.props.auth, 'signUp.form.zip', '')} + errors={read(this.props.auth, 'signUp.errors.zip', {})} onChange={this.handleInput.bind(this, "zip")} {...this.props.inputProps.zip} /> @@ -175,7 +179,7 @@ class EmailSignUpForm extends React.Component { - } @@ -186,6 +190,10 @@ class EmailSignUpForm extends React.Component {
); + } catch (ex){ + console.error('Render exception: ', ex); + return [' ERROR ']; + } } } diff --git a/js-frontend/src/controls/bootstrap/Input.js b/js-frontend/src/controls/bootstrap/Input.js index bda633c..b5ee2c4 100644 --- a/js-frontend/src/controls/bootstrap/Input.js +++ b/js-frontend/src/controls/bootstrap/Input.js @@ -53,7 +53,7 @@ class AuthInput extends React.Component { return (
{this.renderErrorList()}
diff --git a/js-frontend/src/reducers/auth/index.js b/js-frontend/src/reducers/auth/index.js index 449fadc..fbe4769 100644 --- a/js-frontend/src/reducers/auth/index.js +++ b/js-frontend/src/reducers/auth/index.js @@ -6,12 +6,14 @@ import { combineReducers } from 'redux'; import { configReducer } from './configure'; import { authReducer } from './authenticate'; import { signInReducer } from './signin'; +import { signUpReducer } from './signup'; import { signOutReducer } from './signout'; import { userReducer } from './user'; const authStateReducer = combineReducers({ configure: configReducer, signIn: signInReducer, + signUp: signUpReducer, signOut: signOutReducer, authentication: authReducer, user: userReducer diff --git a/js-frontend/src/reducers/auth/signup.js b/js-frontend/src/reducers/auth/signup.js new file mode 100644 index 0000000..c2d23c3 --- /dev/null +++ b/js-frontend/src/reducers/auth/signup.js @@ -0,0 +1,43 @@ +/** + * Created by andrew on 25/02/16. + */ +import T from '../../constants/ACTION_TYPES'; + + +const signUpInitialState = { + loading: false, + errors: null, + form: {} +}; + +export const signUpReducer = (state = { ...signUpInitialState, form: {...signUpInitialState.form }}, action) => { + switch(action.type) { + case T.AUTH.SIGN_UP_START: + return { + ...state, + loading: true + }; + case T.AUTH.SIGN_UP_COMPLETE: + return { + ...signUpInitialState, + form: { ...signUpInitialState.form } + }; + case T.AUTH.SIGN_UP_ERROR: + const { errors } = action; + return { + ...state, + loading: false, + errors + }; + case T.AUTH.SIGN_UP_FORM_UPDATE: + const { key, value } = action; + return { + ...state, + form: { + ...state.form, + [key]: value + } + }; + default: return state; + } +}; \ No newline at end of file diff --git a/js-frontend/src/utils/api.js b/js-frontend/src/utils/api.js new file mode 100644 index 0000000..c4e7796 --- /dev/null +++ b/js-frontend/src/utils/api.js @@ -0,0 +1,34 @@ +/** + * Created by andrew on 12/03/16. + */ +import fetch from './fetch'; +import { + getEmailSignInUrl, + getEmailSignUpUrl +} from "./sessionStorage"; +import root from './root'; + + +import { parseResponse } from "./handleFetchResponse"; + +export function apiSignIn(body) { + return fetch(getEmailSignInUrl(), { + headers: { + "Accept": "application/json", + "Content-Type": "application/json" + }, + method: "post", + body: root.JSON.stringify(body) + }).then(parseResponse); +} + +export function apiSignUp(body) { + return fetch(getEmailSignUpUrl(), { + headers: { + "Accept": "application/json", + "Content-Type": "application/json" + }, + method: "post", + body: root.JSON.stringify(body) + }).then(parseResponse); +} \ No newline at end of file diff --git a/js-frontend/src/utils/fetch.js b/js-frontend/src/utils/fetch.js index 4ee2a64..74ca794 100644 --- a/js-frontend/src/utils/fetch.js +++ b/js-frontend/src/utils/fetch.js @@ -3,13 +3,11 @@ */ import originalFetch from "isomorphic-fetch"; import * as C from "./constants"; -//import extend from "extend"; + import { - getApiUrl, retrieveData, persistData, getTokenFormat, - getSessionEndpointKey, isApiRequest } from "./sessionStorage"; diff --git a/js-frontend/src/utils/parseUrl.js b/js-frontend/src/utils/parseUrl.js index 4e98732..a25e2be 100644 --- a/js-frontend/src/utils/parseUrl.js +++ b/js-frontend/src/utils/parseUrl.js @@ -126,7 +126,7 @@ export default function getRedirectInfo(currentLocation) { var authRedirectPath = getLocationWithoutParams(currentLocation, authKeys); if (authRedirectPath !== currentLocation) { - return {authRedirectHeaders, authRedirectPath}; + return { authRedirectHeaders, authRedirectPath }; } else { return {}; } diff --git a/js-frontend/src/utils/readProp.js b/js-frontend/src/utils/readProp.js new file mode 100644 index 0000000..67d72b1 --- /dev/null +++ b/js-frontend/src/utils/readProp.js @@ -0,0 +1,16 @@ +/** + * Created by andrew on 11/03/16. + */ +export default function read(src, path = '', defaultVal = null) { + const [pathItem = null, ...rest] = path.split('.'); + + if (pathItem === null ) { + return src; + } else if (rest.length === 0) { + if (!src) { return defaultVal; } + return src[pathItem]; + } + + if (!src) { return defaultVal; } + return read(src[pathItem], rest.join('.'), defaultVal); +} \ No newline at end of file diff --git a/js-frontend/src/utils/root.js b/js-frontend/src/utils/root.js index b1ab384..cf91550 100644 --- a/js-frontend/src/utils/root.js +++ b/js-frontend/src/utils/root.js @@ -1,4 +1,6 @@ /** * Created by andrew on 27/02/16. */ +// even though this code shouldn't be used server-side, node will throw +// errors if "window" is used export default Function("return this")() || (42, eval)("this"); \ No newline at end of file diff --git a/js-frontend/src/utils/sessionStorage.js b/js-frontend/src/utils/sessionStorage.js index a4464d9..eb0b7b2 100644 --- a/js-frontend/src/utils/sessionStorage.js +++ b/js-frontend/src/utils/sessionStorage.js @@ -3,13 +3,11 @@ */ import Cookies from "js-cookie"; import * as C from "./constants"; + +import root from './root'; //import "babel-polyfill"; -// even though this code shouldn't be used server-side, node will throw -// errors if "window" is used -var root = Function("return this")() || (42, eval)("this"); - // stateful variables that persist throughout session root.authState = { currentSettings: {}, @@ -33,14 +31,22 @@ export function getCurrentEndpoint () { return root.authState.currentEndpoint; } +/** + * @deprecated + * @param k + */ export function setCurrentEndpointKey (k) { persistData(C.SAVED_CONFIG_KEY, k || getDefaultEndpointKey()); } export function getCurrentEndpointKey () { - return retrieveData(C.SAVED_CONFIG_KEY) || getDefaultEndpointKey(); + return getDefaultEndpointKey(); } +/** + * @deprecated + * @param k + */ export function setDefaultEndpointKey (k) { persistData(C.DEFAULT_CONFIG_KEY, k); } @@ -81,7 +87,7 @@ export function destroySession () { function unescapeQuotes (val) { return val && val.replace(/("|')/g, ""); -}; +} export function getInitialEndpointKey () { return unescapeQuotes( @@ -94,35 +100,33 @@ export function isApiRequest(url) { return true; } -// TODO: make this really work -export function getSessionEndpointKey (k) { - let key = k || getCurrentEndpointKey(); - if (!key) { - throw "You must configure redux-auth before use."; - } else { - return key; - } +export function getSessionEndpointKey () { + return getCurrentEndpointKey(); } export function getSessionEndpoint (k) { - return getCurrentEndpoint()[getSessionEndpointKey(k)]; + return getCurrentEndpoint()[getSessionEndpointKey()]; } -// only should work for current session -export function getSignOutUrl (endpointKey) { - return `${getApiUrl(endpointKey)}${getSessionEndpoint(endpointKey).signOutPath}` -} +//// only should work for current session +//export function getSignOutUrl (endpointKey) { +// return `${getApiUrl(endpointKey)}${getSessionEndpoint(endpointKey).signOutPath}` +//} export function getEmailSignInUrl (endpointKey) { - return `${getApiUrl(endpointKey)}${getSessionEndpoint(endpointKey).emailSignInPath}` + return `${getSessionEndpoint(endpointKey).emailSignInPath}` } export function getEmailSignUpUrl (endpointKey) { - return `${getApiUrl(endpointKey)}${getSessionEndpoint(endpointKey).emailRegistrationPath}?config_name=${endpointKey}` + return `${getSessionEndpoint(endpointKey).emailRegistrationPath}` } - +/** + * @deprecated + * @param key + * @returns {string|string} + */ export function getApiUrl(key) { let configKey = getSessionEndpointKey(key); return root.authState.currentEndpoint[configKey].apiUrl;