cluedin-widget
Version:
This project contains all the pages needed for browsing entities and searching them. The aim is to replace the CluedIn.Webapp project with this one when all the pages ( including the Admin page ) will be ported to REACT.
99 lines (80 loc) • 2.34 kB
JavaScript
import constants from '../constants';
import { login, getTeamDomain, saveToken } from '../data/auth';
import { unauthorized } from './generic';
import jsCookie from 'js-cookie';
import config from '../../core/config';
const requestLogin = () => ({
type: constants.auth.REQUEST_LOGIN,
});
const receiveLogin = (authInfo, clientId) => {
jsCookie.set('token', authInfo.access_token, {
domain: config.url.mainCookieDomain,
});
config.setCurrentToken(authInfo.access_token);
const clientIds = jsCookie.getJSON('clientIds') || [];
if (clientIds.indexOf(clientId) === -1) {
clientIds.push(clientId);
}
jsCookie.set('clientIds', clientIds);
jsCookie.set('clientId', clientId);
return {
type: constants.auth.RECEIVE_LOGIN,
data: {
authInfo,
},
};
};
const invalidLogin = () => ({
type: constants.auth.INVALID_LOGIN,
});
export const fetchLogin = (username, password, clientId) => {
return (dispatch) => {
dispatch(requestLogin());
return login(username, password, clientId).then((authInfo) => {
saveToken(authInfo, clientId).then(() => {
dispatch(receiveLogin(authInfo, clientId));
});
}).catch(unauthorized(dispatch, invalidLogin));
};
};
const requestTeamDomain = (clientId) => ({
type: constants.auth.REQUEST_TEAM_DOMAIN,
data: {
clientId,
},
});
const receiveTeamDomain = (clientId, teamDomainResult) => {
let clientIdResult;
if (!teamDomainResult.isAvailable) {
clientIdResult = clientId;
}
return {
type: constants.auth.RECEIVE_TEAM_DOMAIN,
data: {
teamDomainResult,
clientId: clientIdResult,
},
};
};
const invalidTeamDomain = () => ({
type: constants.auth.INVALID_TEAM_DOMAIN,
});
export const resetClientId = () => ({
type: constants.auth.RESET_TEAM_DOMAIN,
});
export const fetchTeamDomain = (clientId) => {
return (dispatch) => {
dispatch(requestTeamDomain(clientId));
return getTeamDomain(clientId).then((teamDomainResult) => {
dispatch(receiveTeamDomain(clientId, teamDomainResult));
}).catch(unauthorized(dispatch, invalidTeamDomain));
};
};
export const redirectAfterLogin = (mode, clientId) => {
if (mode !== 'chrome') {
window.location = config.location.goToRoot(clientId);
}
return {
type: constants.auth.REDIRECT_AFTER_LOGIN,
};
};