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 : + } +} + +export default OauthCallback diff --git a/UI/src/components/servers/UserCard.js b/UI/src/components/servers/UserCard.js index f633439..20b8937 100644 --- a/UI/src/components/servers/UserCard.js +++ b/UI/src/components/servers/UserCard.js @@ -1,8 +1,11 @@ import React, { Component } from 'react' import ImmutablePropTypes from 'react-immutable-proptypes' import { NavLink } from 'react-router-dom' +import { connect } from 'react-redux' +import * as Actions from '../../actions' import './UserCard.sass' +@connect() class UserCard extends Component { static propTypes = { user: ImmutablePropTypes.map @@ -33,7 +36,7 @@ class UserCard extends Component {
- {this.props.children} + { + this.props.children + }
} } -export default Radium(Wrapper) +export default Wrapper diff --git a/UI/src/reducers/user.js b/UI/src/reducers/user.js index 09c28a8..f033d41 100644 --- a/UI/src/reducers/user.js +++ b/UI/src/reducers/user.js @@ -1,6 +1,7 @@ import { Map } from 'immutable' const initialState = Map({ + isLoggedIn: false, username: 'あたし', discriminator: '0001', id: '', @@ -9,10 +10,12 @@ const initialState = Map({ export default (state = initialState, { type, data }) => { switch (type) { - case Symbol.for('set user'): - return Map(data) + return Map({...data, isLoggedIn: true}) + case Symbol.for('reset user'): + return initialState + default: return state } diff --git a/UI/src/router/index.js b/UI/src/router/index.js index a53e16a..0a71896 100644 --- a/UI/src/router/index.js +++ b/UI/src/router/index.js @@ -3,6 +3,7 @@ import { Route } from 'react-router-dom' import Servers from '../components/servers' import OauthCallback from '../components/oauth-callback' +import OauthFlow from '../components/oauth-flow' const aaa = (props) => (
{ JSON.stringify(props) }
) @@ -13,6 +14,7 @@ export default class AppRouter extends Component { + } }