Интеграция appInfo с API

This commit is contained in:
SleepWalker
2016-02-23 07:57:16 +02:00
parent aa422cb4f2
commit 404684b8d9
6 changed files with 146 additions and 15 deletions

View File

@@ -0,0 +1,42 @@
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { oAuthValidate } from 'components/auth/actions';
class OAuthInit extends Component {
static displayName = 'OAuthInit';
static propTypes = {
query: PropTypes.shape({
client_id: PropTypes.string.isRequired,
redirect_uri: PropTypes.string.isRequired,
response_type: PropTypes.string.isRequired,
scope: PropTypes.string.isRequired,
state: PropTypes.string
}),
validate: PropTypes.func.isRequired
};
componentWillMount() {
const {query} = this.props;
this.props.validate({
clientId: query.client_id,
redirectUrl: query.redirect_uri,
responseType: query.response_type,
scope: query.scope,
state: query.state
});
}
render() {
return <span />;
}
}
export default connect((state) => ({
query: state.routing.location.query
}), {
validate: oAuthValidate
})(OAuthInit);

View File

@@ -107,3 +107,45 @@ export function logout() {
dispatch(routeActions.push('/login'));
};
}
// TODO: move to oAuth actions?
// test request: /oauth?client_id=ely&redirect_uri=http%3A%2F%2Fely.by&response_type=code&scope=minecraft_server_session
export function oAuthValidate({clientId, redirectUrl, responseType, scope, state}) {
return (dispatch) =>
request.get(
'/api/oauth/validate',
{
client_id: clientId,
redirect_uri: redirectUrl,
response_type: responseType,
scope,
state
}
)
.then((resp) => {
dispatch(setClient(resp.client));
dispatch(routeActions.push('/oauth/permissions'));
})
.catch((resp = {}) => { // TODO
if (resp.statusCode === 400 && resp.error === 'invalid_request') {
alert(`Invalid request (${resp.parameter} required).`);
}
if (resp.statusCode === 401 && resp.error === 'invalid_client') {
alert('Can not find application you are trying to authorize.');
}
if (resp.statusCode === 400 && resp.error === 'unsupported_response_type') {
alert(`Invalid response type '${resp.parameter}'.`);
}
if (resp.statusCode === 400 && resp.error === 'invalid_scope') {
alert(`Invalid scope '${resp.parameter}'.`);
}
});
}
export const SET_CLIENT = 'set_client';
export function setClient({id, name, description}) {
return {
type: SET_CLIENT,
payload: {id, name, description}
};
}

View File

@@ -1,9 +1,10 @@
import { combineReducers } from 'redux';
import { ERROR } from './actions';
import { ERROR, SET_CLIENT } from './actions';
export default combineReducers({
error
error,
client
});
function error(
@@ -21,3 +22,20 @@ function error(
return state;
}
}
function client(
state = null,
{type, payload = {}}
) {
switch (type) {
case SET_CLIENT:
return {
id: payload.id,
name: payload.name,
description: payload.description
};
default:
return state;
}
}