Заимплементил finish state

This commit is contained in:
SleepWalker 2016-03-15 08:36:13 +02:00
parent 779cf2d187
commit e0d93c3058
9 changed files with 87 additions and 21 deletions

View File

@ -151,6 +151,7 @@ export function oAuthComplete(params = {}) {
if (resp.statusCode === 401 && resp.error === 'access_denied') {
// user declined permissions
return {
success: false,
redirectUri: resp.redirectUri
};
}
@ -168,6 +169,18 @@ export function oAuthComplete(params = {}) {
error.acceptRequired = true;
throw error;
}
})
.then((resp) => {
if (resp.redirectUri === 'static_page' || resp.redirectUri === 'static_page_with_code') {
resp.displayCode = resp.redirectUri === 'static_page_with_code';
dispatch(setOAuthCode({
success: resp.success,
code: resp.code,
displayCode: resp.displayCode
}));
}
return resp;
});
};
}
@ -224,6 +237,18 @@ export function setOAuthRequest(oauth) {
};
}
export const SET_OAUTH_RESULT = 'set_oauth_result';
export function setOAuthCode(oauth) {
return {
type: SET_OAUTH_RESULT,
payload: {
success: oauth.success,
code: oauth.code,
displayCode: oauth.displayCode
}
};
}
export const SET_SCOPES = 'set_scopes';
export function setScopes(scopes) {
if (!(scopes instanceof Array)) {

View File

@ -104,9 +104,9 @@ class Finish extends Component {
}
}
export default connect((state) => ({
appName: state.auth.client ? state.auth.client.name : 'Undefined',
code: 'HW9vkZA6Y4vRN3ciSm1IIDk98PHLkPPlv3jvo1MX',
displayCode: true,
success: true
export default connect(({auth}) => ({
appName: auth.client.name,
code: auth.oauth.code,
displayCode: auth.oauth.displayCode,
success: auth.oauth.success
}))(Finish);

View File

@ -1,6 +1,6 @@
import { combineReducers } from 'redux';
import { ERROR, SET_CLIENT, SET_OAUTH, SET_SCOPES } from './actions';
import { ERROR, SET_CLIENT, SET_OAUTH, SET_OAUTH_RESULT, SET_SCOPES } from './actions';
export default combineReducers({
error,
@ -56,6 +56,14 @@ function oauth(
state: payload.state
};
case SET_OAUTH_RESULT:
return {
...state,
success: payload.success,
code: payload.code,
displayCode: payload.displayCode
};
default:
return state;
}

View File

@ -5,8 +5,6 @@ import { connect } from 'react-redux';
import AppInfo from 'components/auth/appInfo/AppInfo';
import PanelTransition from 'components/auth/PanelTransition';
import Finish from 'components/auth/Finish';
import styles from './auth.scss';
class AuthPage extends Component {

View File

@ -16,7 +16,7 @@ import Password from 'components/auth/password/Password';
import Logout from 'components/auth/Logout';
import PasswordChange from 'components/auth/passwordChange/PasswordChange';
import ForgotPassword from 'components/auth/forgotPassword/ForgotPassword';
import Finish from 'components/auth/Finish';
import Finish from 'components/auth/finish/Finish';
import authFlow from 'services/authFlow';
@ -46,7 +46,7 @@ export default function routesFactory(store) {
<Route path="/register" components={new Register()} {...onEnter} />
<Route path="/activation" components={new Activation()} {...onEnter} />
<Route path="/oauth/permissions" components={new Permissions()} {...onEnter} />
<Route path="/oauth/finish" component={Finish} />
<Route path="/oauth/finish" component={Finish} {...onEnter} />
<Route path="/password-change" components={new PasswordChange()} {...onEnter} />
<Route path="/forgot-password" components={new ForgotPassword()} {...onEnter} />
</Route>

View File

@ -93,6 +93,7 @@ export default class AuthFlow {
case '/activation':
case '/password-change':
case '/oauth/permissions':
case '/oauth/finish':
this.setState(new LoginState());
break;

View File

@ -3,8 +3,18 @@ import LoginState from './LoginState';
import PermissionsState from './PermissionsState';
import ActivationState from './ActivationState';
import ChangePasswordState from './ChangePasswordState';
import FinishState from './FinishState';
export default class CompleteState extends AbstractState {
constructor(options = {}) {
super(options);
if ('accept' in options) {
this.isPermissionsAccepted = options.accept;
this.isUserReviewedPermissions = true;
}
}
enter(context) {
const {auth, user} = context.getState();
@ -15,16 +25,32 @@ export default class CompleteState extends AbstractState {
} else if (user.shouldChangePassword) {
context.setState(new ChangePasswordState());
} else if (auth.oauth) {
context.run('oAuthComplete').then((resp) => {
location.href = resp.redirectUri;
}, (resp) => {
// TODO
if (resp.unauthorized) {
context.setState(new LoginState());
} else if (resp.acceptRequired) {
context.setState(new PermissionsState());
if (auth.oauth.code) {
context.setState(new FinishState());
} else {
let data = {};
if (this.isUserReviewedPermissions) {
data.accept = this.isPermissionsAccepted;
}
});
context.run('oAuthComplete', data).then((resp) => {
switch (resp.redirectUri) {
case 'static_page':
case 'static_page_with_code':
context.setState(new FinishState());
break;
default:
location.href = resp.redirectUri;
break;
}
}, (resp) => {
// TODO
if (resp.unauthorized) {
context.setState(new LoginState());
} else if (resp.acceptRequired) {
context.setState(new PermissionsState());
}
});
}
} else {
context.navigate('/');
}

View File

@ -0,0 +1,7 @@
import AbstractState from './AbstractState';
export default class CompleteState extends AbstractState {
enter(context) {
context.navigate('/oauth/finish');
}
}

View File

@ -1,4 +1,5 @@
import AbstractState from './AbstractState';
import CompleteState from './CompleteState';
export default class PermissionsState extends AbstractState {
enter(context) {
@ -14,8 +15,8 @@ export default class PermissionsState extends AbstractState {
}
process(context, accept) {
context.run('oAuthComplete', {
context.setState(new CompleteState({
accept
}).then((resp) => location.href = resp.redirectUri);
}));
}
}