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

191 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-03-02 02:06:14 +05:30
import { routeActions } from 'react-router-redux';
import RegisterState from './RegisterState';
import LoginState from './LoginState';
import OAuthState from './OAuthState';
import ForgotPasswordState from './ForgotPasswordState';
import RecoverPasswordState from './RecoverPasswordState';
import ActivationState from './ActivationState';
import ResendActivationState from './ResendActivationState';
2016-03-02 02:06:14 +05:30
export default class AuthFlow {
constructor(actions) {
if (typeof actions !== 'object') {
throw new Error('AuthFlow requires an actions object');
}
this.actions = actions;
if (Object.freeze) {
Object.freeze(this.actions);
}
}
2016-03-02 02:06:14 +05:30
setStore(store) {
this.navigate = (route) => {
if (this.getRequest().path !== route) {
this.currentRequest = {
path: route
};
2016-03-02 02:06:14 +05:30
if (this.replace) {
this.replace(route);
}
store.dispatch(routeActions.push(route)); // TODO: may be deleted?
2016-03-02 02:06:14 +05:30
}
this.replace = null;
};
this.getState = store.getState.bind(store);
this.dispatch = store.dispatch.bind(store);
this.restoreOAuthState();
2016-03-02 02:06:14 +05:30
}
resolve(payload = {}) {
this.state.resolve(this, payload);
}
reject(payload = {}) {
this.state.reject(this, payload);
}
goBack() {
this.state.goBack(this);
}
run(actionId, payload) {
2016-05-30 09:53:27 +05:30
if (actionId === 'redirect') {
location.href = payload;
return;
}
if (!this.actions[actionId]) {
2016-03-02 02:06:14 +05:30
throw new Error(`Action ${actionId} does not exists`);
}
return this.dispatch(this.actions[actionId](payload));
2016-03-02 02:06:14 +05:30
}
setState(state) {
if (!state) {
throw new Error('State is required');
}
this.state && this.state.leave(this);
this.prevState = this.state;
2016-03-02 02:06:14 +05:30
this.state = state;
const resp = this.state.enter(this);
if (resp && resp.then) {
// this is a state with an async enter phase
// block route components from mounting, till promise will be resolved
if (this.onReady) {
const callback = this.onReady;
this.onReady = () => {};
return resp.then(callback);
}
return resp;
}
2016-03-02 02:06:14 +05:30
}
/**
* @return {object} - current request object
*/
getRequest() {
return {
path: '',
query: {},
params: {},
...this.currentRequest
};
}
/**
* This should be called from onEnter prop of react-router Route component
*
* @param {object} request
* @param {string} request.path
* @param {object} request.params
* @param {object} request.query
* @param {function} replace
* @param {function} [callback = function() {}] - an optional callback function to be called, when state will be stabilized
* (state's enter function's promise resolved)
*/
handleRequest(request, replace, callback = function() {}) {
const {path, params = {}, query = {}} = request;
2016-03-02 02:06:14 +05:30
this.replace = replace;
this.onReady = callback;
2016-03-02 02:06:14 +05:30
if (!path) {
throw new Error('The request.path is required');
}
if (this.getRequest().path === path) {
// we are already handling this path
this.onReady();
return;
}
this.currentRequest = request;
2016-07-28 01:15:50 +05:30
switch (path) {
2016-03-02 02:06:14 +05:30
case '/register':
this.setState(new RegisterState());
break;
case '/forgot-password':
this.setState(new ForgotPasswordState());
break;
case '/resend-activation':
this.setState(new ResendActivationState());
break;
case '/':
2016-03-02 02:06:14 +05:30
case '/login':
case '/password':
2016-08-03 00:29:29 +05:30
case '/accept-rules':
2016-03-02 02:06:14 +05:30
case '/oauth/permissions':
case '/oauth/finish':
2016-03-02 02:06:14 +05:30
this.setState(new LoginState());
break;
default:
2016-05-28 03:54:22 +05:30
switch (path.replace(/(.)\/.+/, '$1')) { // use only first part of an url
case '/oauth2':
this.setState(new OAuthState());
break;
case '/activation':
this.setState(new ActivationState());
break;
2016-05-28 03:54:22 +05:30
case '/recover-password':
this.setState(new RecoverPasswordState());
break;
default:
console.log('Unsupported request', {path, query, params});
replace('/404');
break;
2016-05-28 03:54:22 +05:30
}
2016-03-02 02:06:14 +05:30
}
this.onReady();
2016-03-02 02:06:14 +05:30
}
/**
* @api private
*/
restoreOAuthState() {
try {
const data = JSON.parse(localStorage.getItem('oauthData'));
if (Date.now() - data.timestamp < 60 * 60 * 1000) {
this.run('oAuthValidate', data.payload);
}
} catch (err) {/* bad luck :( */}
}
2016-03-02 02:06:14 +05:30
}