summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/client/api/crud.fetch.js2
-rw-r--r--app/client/auth/auth.actions.js27
-rw-r--r--app/client/auth/login.component.js2
-rw-r--r--app/client/index.jsx16
-rw-r--r--app/client/system/system.component.js10
-rw-r--r--app/server/util/auth.js11
6 files changed, 53 insertions, 15 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),
diff --git a/app/server/util/auth.js b/app/server/util/auth.js
index 6a9bfe0..e7ee275 100644
--- a/app/server/util/auth.js
+++ b/app/server/util/auth.js
@@ -14,7 +14,7 @@ export function route(app, serve_index){
app.get("/login", serve_index)
app.get("/signup", serve_index)
- app.get("/logout", logout)
+ app.get("/logout", serve_logout)
app.put("/api/signup",
checkIfUserExists,
@@ -26,6 +26,8 @@ export function route(app, serve_index){
app.put("/api/checkin",
ensureAPIAuthenticated,
checkin)
+ app.get("/api/logout",
+ logout)
}
export function ensureAuthenticated(req, res, next) {
@@ -169,7 +171,12 @@ export function checkin(req, res) {
res.json({ user: sanitizeUser(req.user) })
}
-export const logout = (req, res) => {
+export const serve_logout = (req, res) => {
req.logout()
res.redirect('/')
+}
+
+export const logout = (req, res) => {
+ req.logout()
+ res.json({ status: 'logged_out' })
} \ No newline at end of file