+export const toggleSession = (session: Session) =>
+ async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+ let s = { ...session };
+
+ if (session.loggedIn) {
+ s.loggedIn = false;
+ } else {
+ const sessions = getState().auth.sessions;
+ const activeSession = getActiveSession(sessions);
+ if (activeSession) {
+ s = await dispatch<any>(validateSession(s, activeSession)) as Session;
+ }
+ }
+
+ dispatch(authActions.UPDATE_SESSION(s));
+ services.authService.saveSessions(getState().auth.sessions);
+ };
+
+export const initSessions = (authService: AuthService, config: Config, user: User) =>
+ (dispatch: Dispatch<any>) => {
+ const sessions = authService.buildSessions(config, user);
+ authService.saveSessions(sessions);
+ dispatch(authActions.SET_SESSIONS(sessions));
+ };
+