Merge pull request #327 from jorilallo/slack-fix
Fixes: Adding Slack command
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Redirect } from 'react-router';
|
import { Redirect } from 'react-router';
|
||||||
import queryString from 'query-string';
|
import queryString from 'query-string';
|
||||||
|
import { observable } from 'mobx';
|
||||||
import { observer, inject } from 'mobx-react';
|
import { observer, inject } from 'mobx-react';
|
||||||
import { client } from 'utils/ApiClient';
|
import { client } from 'utils/ApiClient';
|
||||||
|
|
||||||
@ -12,17 +13,15 @@ type Props = {
|
|||||||
location: Object,
|
location: Object,
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
|
||||||
redirectTo: string,
|
|
||||||
};
|
|
||||||
|
|
||||||
@observer class SlackAuth extends React.Component {
|
@observer class SlackAuth extends React.Component {
|
||||||
props: Props;
|
props: Props;
|
||||||
state: State;
|
@observable redirectTo: string;
|
||||||
state = {};
|
|
||||||
|
|
||||||
// $FlowIssue Flow doesn't like async lifecycle components https://github.com/facebook/flow/issues/1803
|
componentDidMount() {
|
||||||
async componentDidMount(): void {
|
this.redirect();
|
||||||
|
}
|
||||||
|
|
||||||
|
async redirect() {
|
||||||
const { error, code, state } = queryString.parse(
|
const { error, code, state } = queryString.parse(
|
||||||
this.props.location.search
|
this.props.location.search
|
||||||
);
|
);
|
||||||
@ -30,18 +29,18 @@ type State = {
|
|||||||
if (error) {
|
if (error) {
|
||||||
if (error === 'access_denied') {
|
if (error === 'access_denied') {
|
||||||
// User selected "Deny" access on Slack OAuth
|
// User selected "Deny" access on Slack OAuth
|
||||||
this.setState({ redirectTo: '/dashboard' });
|
this.redirectTo = '/dashboard';
|
||||||
} else {
|
} else {
|
||||||
this.setState({ redirectTo: '/auth/error' });
|
this.redirectTo = '/auth/error';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.props.location.pathname === '/auth/slack/commands') {
|
if (this.props.location.pathname === '/auth/slack/commands') {
|
||||||
// User adding webhook integrations
|
// User adding webhook integrations
|
||||||
try {
|
try {
|
||||||
await client.post('/auth.slackCommands', { code });
|
await client.post('/auth.slackCommands', { code });
|
||||||
this.setState({ redirectTo: '/dashboard' });
|
this.redirectTo = '/dashboard';
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.setState({ redirectTo: '/auth/error' });
|
this.redirectTo = '/auth/error';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Regular Slack authentication
|
// Regular Slack authentication
|
||||||
@ -50,18 +49,15 @@ type State = {
|
|||||||
|
|
||||||
const { success } = await this.props.auth.authWithSlack(code, state);
|
const { success } = await this.props.auth.authWithSlack(code, state);
|
||||||
success
|
success
|
||||||
? this.setState({ redirectTo: redirectTo || '/dashboard' })
|
? (this.redirectTo = redirectTo || '/dashboard')
|
||||||
: this.setState({ redirectTo: '/auth/error' });
|
: (this.redirectTo = '/auth/error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
if (this.redirectTo) return <Redirect to={this.redirectTo} />;
|
||||||
<div>
|
return null;
|
||||||
{this.state.redirectTo && <Redirect to={this.state.redirectTo} />}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,6 @@ export async function request(endpoint: string, body: Object) {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw httpErrors.BadRequest();
|
throw httpErrors.BadRequest();
|
||||||
}
|
}
|
||||||
console.log('DATA', data);
|
|
||||||
if (!data.ok) throw httpErrors.BadRequest(data.error);
|
if (!data.ok) throw httpErrors.BadRequest(data.error);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
@ -28,7 +27,7 @@ export async function oauthAccess(
|
|||||||
return request('oauth.access', {
|
return request('oauth.access', {
|
||||||
client_id: process.env.SLACK_KEY,
|
client_id: process.env.SLACK_KEY,
|
||||||
client_secret: process.env.SLACK_SECRET,
|
client_secret: process.env.SLACK_SECRET,
|
||||||
redirect_uri: `${process.env.URL || ''}/auth/slack`,
|
redirect_uri,
|
||||||
code,
|
code,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user