<commit_before>import React from 'react';
import { render } from 'react-dom';

import { App } from './containers/App';
import { Home, SinglePost, Login, NotFound } from './containers';
import { Router, Route } from './components/router';
import { history } from './history';
import { firebase } from './backend';

import './styles/styles.scss';

// Function that wraps ReactDOM.render and renders the app w/ current location
const renderApp = (state) => {
  render(
    <Router {...state}>
      <Route path="/" index={Home} component={App}>
        <Route path="posts/:post" component={SinglePost} />
        <Route path="login" component={Login} />
        <Route path="*" component={NotFound} />
      </Route>
    </Router>,
    document.getElementById('app'),
  );
};

const initialState = {
  location: window.location.pathname,
};

// When there's a history change, re-render the app
function activateHistoryListener() {
  history.listen((location) => {
    const user = firebase.auth().currentUser;
    const newState = Object.assign(initialState, {
      location: user
                ? location.pathname
                : '/login',
    });

    renderApp(newState);
  });
}

function activateAuthListener() {
  firebase.auth().onAuthStateChanged((user) => {
    const currentLocation = window.location.pathname;
    if (currentLocation === '/') {
      history.push(user
                ? window.location.pathname
                : '/login');
    }

        // Ensure user gets redirected after they log in
    if (user && currentLocation === '/login') {
      history.push('/');
    }
  });
}


// Render the app initially
renderApp(initialState);
activateHistoryListener();
activateAuthListener();<commit_msg>simplify auth stuff in backend<commit_after>import React from 'react';
import { render } from 'react-dom';

import { App } from './containers/App';
import { Home, SinglePost, Login, NotFound } from './containers';
import { Router, Route } from './components/router';
import { history } from './history';
import { firebase } from './backend';

import './styles/styles.scss';

// Function that wraps ReactDOM.render and renders the app w/ current location
const renderApp = (state) => {
  render(
    <Router {...state}>
      <Route path="/" index={Home} component={App}>
        <Route path="posts/:post" component={SinglePost} />
        <Route path="login" component={Login} />
        <Route path="*" component={NotFound} />
      </Route>
    </Router>,
    document.getElementById('app'),
  );
};

// Create an intial state object to use
const initialState = {
  location: window.location.pathname,
};

// When there's a history change, re-render the app
function activateHistoryListener() {
  history.listen((location) => {
    const user = firebase.auth().currentUser;
    const newState = Object.assign(initialState, {
      location: user
                ? location.pathname
                : '/login',
    });

    renderApp(newState);
  });
}

// Set up the auth listener
function activateAuthListener() {
  firebase.auth().onAuthStateChanged(((user) => {
    if (user && window.location.pathname === '/login') {
      return history.push('/');
    }
    return history.push(user ? window.location.pathname : '/login');
  }));
}


// Render the app initially
renderApp(initialState);
activateHistoryListener();
activateAuthListener();
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.0, 'mean_word_length': 3.127450980392157, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7004966', 'n_tokens_mistral': 941, 'n_tokens_neox': 928, 'n_words': 252}