diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-09-17 01:51:32 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-09-17 01:51:32 +0200 |
| commit | d2b4aaf625f10c659614d7326d8dc3e2e31ae0f3 (patch) | |
| tree | 4755ec719eceb0de2c4fb77b49ef974dd5bca805 /app | |
| parent | a77d0812d52d52d80bc750832b4e0fe065ce4cac (diff) | |
auth gate/router combo
Diffstat (limited to 'app')
| -rw-r--r-- | app/client/auth/auth.actions.js | 37 | ||||
| -rw-r--r-- | app/client/auth/auth.gate.js | 42 | ||||
| -rw-r--r-- | app/client/auth/auth.reducer.js | 43 | ||||
| -rw-r--r-- | app/client/auth/login.component.js | 1 | ||||
| -rw-r--r-- | app/client/auth/signup.component.js | 7 | ||||
| -rw-r--r-- | app/client/types.js | 2 | ||||
| -rw-r--r-- | app/server/util/auth.js | 5 |
7 files changed, 84 insertions, 53 deletions
diff --git a/app/client/auth/auth.actions.js b/app/client/auth/auth.actions.js index 991a3f9..3171996 100644 --- a/app/client/auth/auth.actions.js +++ b/app/client/auth/auth.actions.js @@ -4,6 +4,9 @@ import { put } from '../api/crud.fetch' export const setToken = (data) => { return { type: types.auth.set_token, data } } +export const setReturnTo = (data) => { + return { type: types.auth.set_return_to, data } +} export const setError = (data) => { return { type: types.auth.set_error, data } } @@ -13,7 +16,10 @@ export const setCurrentUser = (data) => { export function logout() { return { type: types.auth.logout_user } } -export function authLoading() { +export function initialized() { + return { type: types.auth.initialized } +} +export function loading() { return { type: types.auth.loading } } @@ -31,29 +37,27 @@ const api = { export function login(username, password) { return (dispatch) => { - dispatch(authLoading()) + dispatch(loading()) fetch(api.login, put({ username, password })) .then(req => req.json()) .then(data => { + console.log(data) dispatch(setToken(data.token)) dispatch(checkin()) }) .catch(error => { + console.log(error) dispatch(setError(true)) - if (error.response.status === 400) { - throw new InvalidCredentialsException(error) - } - throw error }) } } export function signup(data) { return (dispatch) => { - dispatch(authLoading()) + dispatch(loading()) fetch(api.signup, put(data)) .then(req => req.json()) .then(data => { @@ -62,19 +66,15 @@ export function signup(data) { }) .catch(error => { console.log(error) - if (error.response.status === 400) { - // dispatch(accountError("There was an error creating your account.")) - throw new InvalidCredentialsException(error) - } - throw error + dispatch(initialized()) }) } } -export function checkin() { +export function checkin(history) { return (dispatch) => { - dispatch(authLoading()) - fetch(api.checkin) + dispatch(loading()) + fetch(api.checkin, put({})) .then(req => req.json()) .then(data => { console.log(data) @@ -82,10 +82,9 @@ export function checkin() { console.log('set current user') }) .catch(error => { - if (error.response.status === 400) { - throw new InvalidCredentialsException(error) - } - throw error + console.log(error) + // history.go('/login') + dispatch(initialized(true)) }) } } diff --git a/app/client/auth/auth.gate.js b/app/client/auth/auth.gate.js index e7a9940..8bedfa9 100644 --- a/app/client/auth/auth.gate.js +++ b/app/client/auth/auth.gate.js @@ -1,9 +1,10 @@ import { h, Component } from 'preact'; // import PropTypes from 'prop-types'; -import { BrowserRouter, Route } from 'react-router-dom' +import { BrowserRouter, Route, Switch, Redirect, withRouter } from 'react-router-dom' import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; -import { Redirect } from 'react-router-dom'; + +import * as authActions from './auth.actions'; import Login from './login.component'; import Logout from './logout.component'; @@ -11,23 +12,43 @@ import Signup from './signup.component'; import { randint } from '../util/math' -class AuthGate extends Component { +class AuthRouter extends Component { render(){ - if (this.props.auth.isAuthenticated) return children return ( <BrowserRouter> <div> - <div className="spinfx"></div> - <Route exact path='/' component={Login} /> - <Route exact path='/login' component={Login} /> - <Route exact path='/logout' component={Logout} /> - <Route exact path='/signup' component={Signup} /> + <div className="diamond"></div> + <Switch> + <Route exact path='/' component={Login} /> + <Route exact path='/login' component={Login} /> + <Route exact path='/logout' component={Logout} /> + <Route exact path='/signup' component={Signup} /> + <Route component={() => { + props.actions.setReturnTo(props.location.pathname) + return ( + <Redirect to="/login" /> + ) + }} /> + </Switch> </div> </BrowserRouter> ) } componentDidMount(){ - document.querySelector('.spinfx').style.backgroundImage = 'linear-gradient(' + (randint(40)-5) + 'deg, #fde, #ffe)' + document.querySelector('.diamond').style.backgroundImage = 'linear-gradient(' + (randint(40)-5) + 'deg, #fde, #ffe)' + } +} + +class AuthGate extends Component { + render(){ + if (!this.props.auth.initialized) { + return <div className='loading'>Loading</div> + } + if (this.props.auth.isAuthenticated) return children + return <AuthRouter {...this.props} /> + } + componentDidMount(){ + this.props.actions.checkin(history) } } @@ -36,6 +57,7 @@ const mapStateToProps = (state) => ({ }); const mapDispatchToProps = (dispatch) => ({ + actions: bindActionCreators(authActions, dispatch) }); export default connect(mapStateToProps, mapDispatchToProps)(AuthGate); diff --git a/app/client/auth/auth.reducer.js b/app/client/auth/auth.reducer.js index cacb0d5..7b3193a 100644 --- a/app/client/auth/auth.reducer.js +++ b/app/client/auth/auth.reducer.js @@ -1,12 +1,14 @@ -import types from '../types'; +import types from '../types' const authInitialState = { token: null, user: {}, groups: {}, + initialized: false, loading: false, isAuthenticated: false, -}; + returnTo: null, +} const auth = (state = authInitialState, action) => { switch(action.type) { @@ -17,35 +19,40 @@ const auth = (state = authInitialState, action) => { isAuthenticated: !!action.data, loading: false, error: null, - }; + } + + case types.auth.initialized: + return { + ...state, + loading: false, + initialized: true, + error: null, + } case types.auth.loading: return { ...state, loading: true, error: null, - }; + } case types.auth.set_current_user: - const groups = {} - action.data.groups.forEach(g => groups[g.name.toLowerCase()] = true) - if (action.data.is_staff) { - groups['staff'] = true - } - if (action.data.is_superuser) { - groups['superuser'] = true - } return { ...state, user: action.data, - groups, error: null, - }; + } + + case types.auth.set_return_to: + return { + ...state, + returnTo: action.data, + } case types.auth.logout_user: return { ...authInitialState - }; + } case types.auth.set_error: return { @@ -72,11 +79,11 @@ const auth = (state = authInitialState, action) => { // console.error("error loading initial state") // } // } - return state; + return state default: - return state; + return state } } -export default auth; +export default auth diff --git a/app/client/auth/login.component.js b/app/client/auth/login.component.js index 17c6d67..d2b7145 100644 --- a/app/client/auth/login.component.js +++ b/app/client/auth/login.component.js @@ -26,6 +26,7 @@ class Login extends Component { } handleSubmit(e) { e.preventDefault() + if (this.props.auth.loading) return this.props.actions.login(this.state.username, this.state.password) } render(){ diff --git a/app/client/auth/signup.component.js b/app/client/auth/signup.component.js index e54084b..6f9cdaf 100644 --- a/app/client/auth/signup.component.js +++ b/app/client/auth/signup.component.js @@ -30,7 +30,8 @@ class Signup extends Component { } handleSubmit(e) { e.preventDefault() - if (!this.validate) { + if (this.props.auth.loading) return + if (!this.validate()) { return this.props.actions.setError('bad password') } let { ...user } = this.state @@ -81,10 +82,10 @@ class Signup extends Component { renderAuthError(){ if (this.props.auth.error) { return ( - <div className='form-input-hint'>{"Please doublecheck the form"}</div> + <div className='form-input-hint'>{"Please doublecheck the form (o=_o~~)"}</div> ) } - return null + return <div className='form-input-hint'></div> } } diff --git a/app/client/types.js b/app/client/types.js index 44fe434..cfb590a 100644 --- a/app/client/types.js +++ b/app/client/types.js @@ -42,6 +42,8 @@ export default { 'set_current_user', 'logout_user', 'loading', + 'initialized', + 'set_return_to', ]), socket: { connect: 'SOCKET_CONNECT', diff --git a/app/server/util/auth.js b/app/server/util/auth.js index 5fc5d1f..b2faf2a 100644 --- a/app/server/util/auth.js +++ b/app/server/util/auth.js @@ -26,13 +26,12 @@ export function route(app, serve_index){ login) app.put("/api/checkin", ensureAuthenticated, - checkin - ) + checkin) } export function ensureAuthenticated(req, res, next) { if (!req.isAuthenticated()) { - req.session.returnTo = req.path + if (req.session) req.session.returnTo = req.path return res.redirect('/login') } next() |
