diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-09-17 12:40:40 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-09-17 12:40:40 +0200 |
| commit | 15e5df27774fef8d976c74b6ec7bcf8f878e5834 (patch) | |
| tree | b9d0110849789382bf9f255fcadf4587e338d2f1 /app/client | |
| parent | 439888003a41279a0fe5a74ac5b92898cabd827b (diff) | |
log in / log out
Diffstat (limited to 'app/client')
| -rw-r--r-- | app/client/api/crud.fetch.js | 2 | ||||
| -rw-r--r-- | app/client/auth/auth.actions.js | 27 | ||||
| -rw-r--r-- | app/client/auth/login.component.js | 2 | ||||
| -rw-r--r-- | app/client/index.jsx | 16 | ||||
| -rw-r--r-- | app/client/system/system.component.js | 10 |
5 files changed, 44 insertions, 13 deletions
diff --git a/app/client/api/crud.fetch.js b/app/client/api/crud.fetch.js index 716ab3e..5f94995 100644 --- a/app/client/api/crud.fetch.js +++ b/app/client/api/crud.fetch.js @@ -42,7 +42,7 @@ function _get_url(_url, data) { } return url } -function _get_headers() { +export function _get_headers() { return { method: 'GET', credentials: 'same-origin', diff --git a/app/client/auth/auth.actions.js b/app/client/auth/auth.actions.js index c075646..aa46d1b 100644 --- a/app/client/auth/auth.actions.js +++ b/app/client/auth/auth.actions.js @@ -1,5 +1,5 @@ import types from '../types' -import { put } from '../api/crud.fetch' +import { put, _get_headers } from '../api/crud.fetch' export const setToken = (data) => { return { type: types.auth.set_token, data } @@ -13,7 +13,7 @@ export const setError = (data) => { export const setCurrentUser = (data) => { return { type: types.auth.set_current_user, data } } -export function logout() { +export const clearCurrentUser = () => { return { type: types.auth.logout_user } } export function initialized() { @@ -45,6 +45,7 @@ export function login(username, password) { .then(req => req.json()) .then(data => { console.log(data) + console.log('logged in', data.user.username) dispatch(setCurrentUser(data.user)) }) .catch(error => { @@ -61,6 +62,7 @@ export function signup(data) { .then(req => req.json()) .then(data => { console.log(data) + console.log('signed up', data.user.username) dispatch(setCurrentUser(data.user)) }) .catch(error => { @@ -76,7 +78,8 @@ export function checkin() { fetch(api.checkin, put({})) .then(req => req.json()) .then(data => { - console.log(data) + // console.log(data) + console.log('checked in', data.user.username) dispatch(setCurrentUser(data.user)) }) .catch(error => { @@ -85,3 +88,21 @@ export function checkin() { }) } } + +export function logout() { + return (dispatch) => { + dispatch(loading()) + fetch(api.logout, _get_headers({})) + .then(req => req.json()) + .then(data => { + console.log('logged out', data) + dispatch(clearCurrentUser()) + dispatch(initialized()) + }) + .catch(error => { + console.error('err', error) + dispatch(clearCurrentUser()) + dispatch(initialized()) + }) + } +} diff --git a/app/client/auth/login.component.js b/app/client/auth/login.component.js index 723d68a..2ef01a6 100644 --- a/app/client/auth/login.component.js +++ b/app/client/auth/login.component.js @@ -15,7 +15,6 @@ class Login extends Component { } constructor() { super() - console.log('yooooooo') this.handleChange = this.handleChange.bind(this) this.handleSubmit = this.handleSubmit.bind(this) } @@ -31,7 +30,6 @@ class Login extends Component { this.props.actions.login(this.state.username, this.state.password) } render(){ - console.log('yooooooo') if (this.props.auth.isAuthenticated) { let { returnTo } = this.props.auth if (!returnTo || returnTo.match(/(login|logout|signup)/i)) { diff --git a/app/client/index.jsx b/app/client/index.jsx index c2c0047..33e4148 100644 --- a/app/client/index.jsx +++ b/app/client/index.jsx @@ -24,12 +24,16 @@ const app = ( <Auth.Gate> <BrowserRouter> <div> - <Route exact path='/' component={Dashboard} /> - <Route exact path='/system/' component={System} /> - <Route exact path='/dashboard/' component={Dashboard} /> - <Route exact path='/logout/' component={Auth.Logout} /> - {module_list} - <Route exact path='/' component={Header} /> + <Switch> + <Route exact path='/' component={Dashboard} /> + <Route exact path='/system/' component={System} /> + <Route exact path='/dashboard/' component={Dashboard} /> + {module_list} + <Route exact path='/logout/' component={Auth.Logout} /> + <Redirect from='/login/' to='/' /> + <Redirect from='/signup/' to='/' /> + </Switch> + <Route path='/' component={Header} /> </div> </BrowserRouter> <AudioPlayer /> diff --git a/app/client/system/system.component.js b/app/client/system/system.component.js index 2aa4cd4..3783862 100644 --- a/app/client/system/system.component.js +++ b/app/client/system/system.component.js @@ -8,6 +8,7 @@ import Param from '../common/param.component' import * as systemActions from './system.actions' import * as liveActions from '../live/live.actions' import * as queueActions from '../queue/queue.actions' +import * as authActions from '../auth/auth.actions' const cpu_test_task = { activity: 'cpu', @@ -50,7 +51,7 @@ class System extends Component { } } render(){ - const { site, server, relay, runner, rpc, actions } = this.props + const { site, server, relay, runner, rpc, actions, user } = this.props return ( <div className='app system'> <div className='heading'> @@ -92,6 +93,11 @@ class System extends Component { <button onClick={() => actions.queue.stop_queue()}>Stop</button> </Param> </Group> + <Group title="Auth"> + <Param title={'Logged in as ' + user.username}> + <button onClick={() => actions.auth.logout()}>Logout</button> + </Param> + </Group> <Group title="Test"> <Param title='CPU Test Task'> <button onClick={() => actions.queue.start_task(cpu_test_task, { preempt: true, watch: true })}>Start</button> @@ -164,12 +170,14 @@ class System extends Component { } } const mapStateToProps = state => ({ + user: state.auth.user, ...state.system, ...state.live, }) const mapDispatchToProps = (dispatch, ownProps) => ({ actions: { + auth: bindActionCreators(authActions, dispatch), system: bindActionCreators(systemActions, dispatch), queue: bindActionCreators(queueActions, dispatch), live: bindActionCreators(liveActions, dispatch), |
