<commit_before>import auth0 from 'auth0-js'
import Auth0Lock from 'auth0-lock'
import decode from 'jwt-decode'
import moment from 'moment'
import uuidv4 from 'uuid/v4'

import { getConfigProperty, getComponentMessages, getMessage } from '../util/config'

const clientID = process.env.AUTH0_CLIENT_ID
const domain = process.env.AUTH0_DOMAIN
const auth0client = new auth0.WebAuth({ domain, clientID })

class Auth0Manager {
  /**
   * Get an Auth0Lock instance.  If one isn't available, create it.
   * I put this here because this will get called after all the config is loaded.
   * The `this.lockOptions` variable is meant to be injected in a testing environment
   *
   * @return {Object} An auth0 lock instance
   */
  getLock () {
    if (!this.lock) {
      this.lock = new Auth0Lock(
        clientID,
        domain,
        this.lockOptions || {
          allowSignUp: false,
          auth: {
            params: {
              scope: 'app_metadata openid email user_metadata'
            },
            redirect: false
          },
          autoclose: true,
          closable: true,
          languageDictionary: {
            title: getMessage(getComponentMessages('Login'), 'title')
          },
          theme: {
            logo: 'https://s3-eu-west-1.amazonaws.com/analyst-logos/conveyal-128x128.png',
            primaryColor: '#2389c9'
          }
        }
      )
    }

    return this.lock
  }

  getProfileFromToken (idToken) {
    return new Promise((resolve, reject) => {
      this.getLock().getProfile(idToken, (err, profile) => {
        if (err) reject(err)
        else resolve(profile)
      })
    })
  }

  getToken () {
    // Retrieves the user token from localStorage
    return window.localStorage.getItem('userToken')
  }

  hideLock () {
    this.getLock().hide()
  }

  /**
   * Show the lock screenn and potentially do stuff after a successful login
   *
   * @param  {Function} [onHide=noop function]
   *   A function to perform if a user closes the lock without logging in
   * @param  {Function} [push]                  A dispatch-aware action function
   * @param  {Function} receiveTokenAndProfile  A dispatch-aware action function
   * @param  {Mixed} [redirectOnSuccess=false]
   *   The string of the route to redirect to upon login success
   */
  loginWithLock ({ onHide = () => {}, push, receiveTokenAndProfile, redirectOnSuccess = false }) {
    const lock = this.getLock()

    function clearOnHideFn () {
      lock.on('hide', () => {})
    }

    function registerOnHideFn () {
      if (typeof onHide === 'function') {
        lock.on('hide', onHide)
      }
    }

    lock.on('authenticated', (authResult) => {
      this.receiveAuthResult({
        authResult,
        push,
        receiveTokenAndProfile,
        redirectOnSuccess
      })
    })

    // register these listeners to make sure the onHide function is not called
    // after a successful login
    lock.on('signin submit', () => { clearOnHideFn() })
    lock.on('signup submit', () => { clearOnHideFn() })
    lock.on('authorization_error', (e) => { registerOnHideFn() })

    registerOnHideFn()<commit_msg>refactor(auth): fix root redirect for realz<commit_after>import auth0 from 'auth0-js'
import Auth0Lock from 'auth0-lock'
import decode from 'jwt-decode'
import moment from 'moment'
import uuidv4 from 'uuid/v4'

import { getConfigProperty, getComponentMessages, getMessage } from '../util/config'

const clientID = process.env.AUTH0_CLIENT_ID
const domain = process.env.AUTH0_DOMAIN
const auth0client = new auth0.WebAuth({ domain, clientID })

class Auth0Manager {
  /**
   * Get an Auth0Lock instance.  If one isn't available, create it.
   * I put this here because this will get called after all the config is loaded.
   * The `this.lockOptions` variable is meant to be injected in a testing environment
   *
   * @return {Object} An auth0 lock instance
   */
  getLock () {
    if (!this.lock) {
      this.lock = new Auth0Lock(
        clientID,
        domain,
        this.lockOptions || {
          allowSignUp: false,
          auth: {
            params: {
              scope: 'app_metadata openid email user_metadata'
            },
            redirect: false
          },
          autoclose: true,
          closable: true,
          languageDictionary: {
            title: getMessage(getComponentMessages('Login'), 'title')
          },
          theme: {
            logo: 'https://s3-eu-west-1.amazonaws.com/analyst-logos/conveyal-128x128.png',
            primaryColor: '#2389c9'
          }
        }
      )
    }

    return this.lock
  }

  getProfileFromToken (idToken) {
    return new Promise((resolve, reject) => {
      this.getLock().getProfile(idToken, (err, profile) => {
        if (err) reject(err)
        else resolve(profile)
      })
    })
  }

  getToken () {
    // Retrieves the user token from localStorage
    return window.localStorage.getItem('userToken')
  }

  hideLock () {
    this.getLock().hide()
  }

  /**
   * Show the lock screenn and potentially do stuff after a successful login
   *
   * @param  {Function} [onHide=noop function]
   *   A function to perform if a user closes the lock without logging in
   * @param  {Function} [push]                  A dispatch-aware action function
   * @param  {Function} receiveTokenAndProfile  A dispatch-aware action function
   * @param  {Mixed} [redirectOnSuccess=false]
   *   The string of the route to redirect to upon login success
   */
  loginWithLock ({ onHide = () => {}, push, receiveTokenAndProfile, redirectOnSuccess = false }) {
    const lock = this.getLock()

    let hideFn = onHide

    function clearOnHideFn () {
      hideFn = () => {}
    }

    function registerOnHideFn () {
      hideFn = onHide
    }

    lock.on('hide', () => {
      hideFn()
    })

    lock.on('authenticated', (authResult) => {
      this.receiveAuthResult({
        authResult,
        push,
        receiveTokenAndProfile,
        redirectOnSuccess
      })
    })

    // register these listeners to make sure the onHide function is not called
    // after a successful login
    lock.on('signin submit', () => { clearOnHideFn() })
    lock.on('signup submit', () => { clearOnHideFn() })
    lock.on('authorization_error', (e) => { registerOnHideFn() })
{'fraction_non_alphanumeric': 0.11176470588235295, 'fraction_numerical': 0.008585055643879173, 'mean_word_length': 2.419021739130435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 2, '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': '2990671', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1821, 'n_words': 510}