diff --git a/Server/api/auth.js b/Server/api/auth.js
index e03e8ef..15ddd8c 100644
--- a/Server/api/auth.js
+++ b/Server/api/auth.js
@@ -51,6 +51,10 @@ module.exports = (R, $) => {
})
R.get('/api/auth/redirect', ctx => {
- ctx.redirect($.discord.getAuthUrl())
+ ctx.body = { url: $.discord.getAuthUrl() }
+ })
+
+ R.post('/api/auth/logout', ctx => {
+ ctx.session = null
})
}
diff --git a/Server/services/discord.js b/Server/services/discord.js
index cf9be6c..72b556e 100644
--- a/Server/services/discord.js
+++ b/Server/services/discord.js
@@ -92,6 +92,10 @@ class DiscordService extends Service {
async getUser (authToken) {
const url = 'https://discordapp.com/api/v6/users/@me'
try {
+ if (authToken == null || authToken === '') {
+ throw new Error('not logged in')
+ }
+
const rsp =
await superagent
.get(url)
diff --git a/UI/src/actions/index.js b/UI/src/actions/index.js
index 50d0f73..13e8d5a 100644
--- a/UI/src/actions/index.js
+++ b/UI/src/actions/index.js
@@ -19,7 +19,19 @@ export const userInit = async dispatch => {
})
dispatch(fetchServers)
- } catch(e) {
- console.log(e)
+ } catch (e) {
+ if (!window.location.pathname.startsWith('/oauth')) {
+ window.location.href = '/oauth/flow'
+ }
}
-}
\ No newline at end of file
+}
+
+export const userLogout = async dispatch => {
+ await superagent.post('/api/auth/logout')
+
+ dispatch({
+ type: Symbol.for('reset user')
+ })
+
+ window.location.href = '/'
+}
diff --git a/UI/src/components/oauth-callback/index.js b/UI/src/components/oauth-callback/index.js
index 0cb1d0c..c593105 100644
--- a/UI/src/components/oauth-callback/index.js
+++ b/UI/src/components/oauth-callback/index.js
@@ -1,7 +1,10 @@
import React, { Component } from 'react'
import { Redirect } from 'react-router-dom'
import superagent from 'superagent'
+import { connect } from 'react-redux'
+import { fetchServers } from '../../actions'
+@connect()
class OauthCallback extends Component {
state = {
notReady: true,
@@ -17,15 +20,36 @@ class OauthCallback extends Component {
this.setState({ message: 'token missing, what are you trying to do?!' })
return
}
-
this.props.history.replace(this.props.location.pathname)
+
+ let counter = 0
+ const retry = async () => {
+ try {
+ const rsp = await superagent.get('/api/auth/user')
+ this.setState({ notReady: false })
+ this.props.dispatch({
+ type: Symbol.for('set user'),
+ data: rsp.body
+ })
+ this.props.dispatch(fetchServers)
+ } catch (e) {
+ counter++
+ if (counter > 12) {
+ this.setState({ message: "i couldn't log you in. :c" })
+ } else {
+ setTimeout(() => { retry() }, 250)
+ }
+ }
+ }
// pass token to backend, await it to finish it's business.
try {
- const rsp = await superagent.post('/api/auth/token').send({ token })
- this.setState({ notReady: false })
- this.props.onLogin(rsp.body)
+ await superagent.post('/api/auth/token').send({ token })
+ // this.props.onLogin(rsp.body)
+
+ retry()
+
} catch (e) {
console.error('token pass error', e)
this.setState({ message: 'g-gomen nasai... i broke it...' })
diff --git a/UI/src/components/oauth-flow/index.js b/UI/src/components/oauth-flow/index.js
new file mode 100644
index 0000000..217ff93
--- /dev/null
+++ b/UI/src/components/oauth-flow/index.js
@@ -0,0 +1,34 @@
+import React, { Component } from 'react'
+import { Redirect } from 'react-router-dom'
+import superagent from 'superagent'
+import { connect } from 'react-redux'
+import { fetchServers } from '../../actions'
+
+@connect()
+class OauthCallback extends Component {
+ state = {
+ notReady: true,
+ message: 'chotto matte kudasai...'
+ }
+
+ async componentDidMount () {
+ const { body: { url } } = await superagent.get('/api/auth/redirect')
+ try {
+ const rsp = await superagent.get('/api/auth/user')
+ this.props.dispatch({
+ type: Symbol.for('set user'),
+ data: rsp.body
+ })
+ this.props.dispatch(fetchServers)
+ this.setState({ notReady: false })
+ } catch (e) {
+ window.location.href = url
+ }
+ }
+
+ render () {
+ return (this.state.notReady) ? this.state.message :