All files / src/data/SingleSignOn loadUser.js

0% Statements 0/27
0% Branches 0/20
0% Functions 0/6
0% Lines 0/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                                       
import log from 'domain/log';
import { userFound, userNotFound, userExpired, loadUserError } from './actions';
 
// stores the redux store here to be accessed by all functions
let reduxStore;
 
// helper function to set the redux store (for testing)
export function setReduxStore(newStore) {
  reduxStore = newStore;
}
 
// helper function to get the redux store (for testing)
export function getReduxStore() {
  return reduxStore;
}
 
// callback function called when the user has been loaded
export function getUserCallback(user) {
  if (user && ! user.expired) {
    reduxStore.dispatch(userFound(user));
  } else if (user && user.expired) {
    reduxStore.dispatch(userExpired());
  } else {
    reduxStore.dispatch(userNotFound());
  }
  return user;
}
 
// error callback called when the userManager's loadUser() function failed
export function errorCallback(error) {
  log.error(`loadUser: Error in loadUser(): ${error.message}`);
  reduxStore.dispatch(loadUserError());
}
 
// function to load the current user into the store
// NOTE: use only when silent renew is configured
export default function loadUser(store, userManager) {
  if (! store || ! store.dispatch) {
    throw new Error('loadUser: You need to pass the redux store into the loadUser helper!');
  }

  if (! userManager || ! userManager.getUser) {
    throw new Error('loadUser: You need to pass the userManager into the loadUser helper!');
  }
 
  reduxStore = store;
 
  return userManager.getUser()
    .then(getUserCallback)
    .catch(errorCallback);
}