Benaiah Mischenko edf0a3afdc feat(backend-github): Open Authoring (#2430)
* Make filterPromises resolve entries before filtering

* Add filterPromisesWith & onlySuccessfulPromises to utility library

* Memoize user method in GitHub API

* Make storeMetadata safe to call concurrently in GitHub API

* Fork workflow: startup and authentication

* Fork workflow: backend support

* Fork workflow: disable unused UI elements

* Fork workflow: docs

* Fork workflow: fix deploy previews

* Suggested edits for fork workflow doc

* Change future tense to present

* Fork workflow: add beta status to docs

* remove debug statement

* rename fork workflow to Open Authoring
2019-07-24 18:20:41 -04:00

117 lines
2.5 KiB
JavaScript

import { actions as notifActions } from 'redux-notifications';
import { currentBackend } from 'coreSrc/backend';
const { notifSend } = notifActions;
export const AUTH_REQUEST = 'AUTH_REQUEST';
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
export const AUTH_FAILURE = 'AUTH_FAILURE';
export const AUTH_REQUEST_DONE = 'AUTH_REQUEST_DONE';
export const USE_FORK_WORKFLOW = 'USE_FORK_WORKFLOW';
export const LOGOUT = 'LOGOUT';
export function authenticating() {
return {
type: AUTH_REQUEST,
};
}
export function authenticate(userData) {
return {
type: AUTH_SUCCESS,
payload: userData,
};
}
export function authError(error) {
return {
type: AUTH_FAILURE,
error: 'Failed to authenticate',
payload: error,
};
}
export function doneAuthenticating() {
return {
type: AUTH_REQUEST_DONE,
};
}
export function useForkWorkflow() {
return {
type: USE_FORK_WORKFLOW,
};
}
export function logout() {
return {
type: LOGOUT,
};
}
// Check if user data token is cached and is valid
export function authenticateUser() {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
dispatch(authenticating());
return backend
.currentUser()
.then(user => {
if (user) {
if (user.useForkWorkflow) {
dispatch(useForkWorkflow());
}
dispatch(authenticate(user));
} else {
dispatch(doneAuthenticating());
}
})
.catch(error => {
dispatch(authError(error));
dispatch(logoutUser());
});
};
}
export function loginUser(credentials) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
dispatch(authenticating());
return backend
.authenticate(credentials)
.then(user => {
if (user.useForkWorkflow) {
dispatch(useForkWorkflow());
}
dispatch(authenticate(user));
})
.catch(error => {
console.error(error);
dispatch(
notifSend({
message: {
details: error.message,
key: 'ui.toast.onFailToAuth',
},
kind: 'warning',
dismissAfter: 8000,
}),
);
dispatch(authError(error));
});
};
}
export function logoutUser() {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
Promise.resolve(backend.logout()).then(() => {
dispatch(logout());
});
};
}