accounts-frontend/src/services/authFlow/CompleteState.js

59 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-03-02 02:06:14 +05:30
import AbstractState from './AbstractState';
import LoginState from './LoginState';
import PermissionsState from './PermissionsState';
import ActivationState from './ActivationState';
import ChangePasswordState from './ChangePasswordState';
import FinishState from './FinishState';
2016-03-02 02:06:14 +05:30
export default class CompleteState extends AbstractState {
constructor(options = {}) {
super(options);
if ('accept' in options) {
this.isPermissionsAccepted = options.accept;
this.isUserReviewedPermissions = true;
}
}
2016-03-02 02:06:14 +05:30
enter(context) {
const {auth, user} = context.getState();
if (user.isGuest) {
context.setState(new LoginState());
} else if (!user.isActive) {
context.setState(new ActivationState());
} else if (user.shouldChangePassword) {
context.setState(new ChangePasswordState());
} else if (auth.oauth && auth.oauth.clientId) {
if (auth.oauth.code) {
context.setState(new FinishState());
} else {
let data = {};
if (this.isUserReviewedPermissions) {
data.accept = this.isPermissionsAccepted;
2016-03-02 02:06:14 +05:30
}
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());
}
});
}
2016-03-02 02:06:14 +05:30
} else {
context.navigate('/');
}
}
}