<commit_before>import React from 'react'
import $ from 'jquery'

import NavigationBar from './navbar'
import UserList from './userlist'
import PermissionData from './permissiondata'

import config from './config'

import { Grid, Row, Col } from 'react-bootstrap'

export default class App extends React.Component {

  constructor(props) {
    super(props)

    this.state = {
      profile : null
    }

    this.lock = new Auth0Lock(config.auth0ClientID, config.auth0Domain)

    try {
      // Get the user token if we've saved it in localStorage before
      var userToken = localStorage.getItem('userToken');

      if (userToken) {
        this.getProfile(userToken)
      }
    } catch(err) {
      console.log(err)
      console.log('error retrieving user data from localStorage, clearing and starting over')
      this.logOut()
    }

  }

  logIn () {
    this.lock.show((err, profile, token) => {
      if (err) {
        console.log("There was an error :/", err)
        return
      }

      // save token to localStorage
      localStorage.setItem('userToken', token);

      this.userLoggedIn(token, profile)
    })
  }

  logOut () {
    localStorage.removeItem('userToken')
    window.location.replace('https://' + config.auth0Domain + '/v2/logout?returnTo=' + window.location.href)
  }

  getProfile (token) {
    // retreive the user profile from Auth0
    $.post( "https://" + config.auth0Domain + "/tokeninfo", { id_token: token })
      .done( (profile) => {
        this.userLoggedIn(token, profile)
      })
  }

  userLoggedIn (token, profile) {

    this.permissionData = new PermissionData(profile.app_metadata.datatools)

    console.log('getting feed IDs')
    $.ajax({
      url : config.dataManagerURL + "/api/feedsources",
      data : {
        feedcollection: config.projectID
      },
      headers: {
        'Authorization' : 'Bearer ' + token
      },
      success: (data) => {
        console.log('got feed sources', data)

        // initialize the feed listing, including a catch-all entry at the beginning
        this.feeds = data
        this.feeds.unshift({
          id: "*",
          name: "All Agencies"
        })

        this.setState({
          profile: profile,
          token: token
        })
      },
      error: (err) => {
        console.log('error getting feed sources', err)
      }
    })
  }

  isAdmin () {
    return this.permissionData && this.permissionData.isProjectAdmin(config.projectID)
  }

  render () {

    return (
      <div>
        <NavigationBar
          title = "MTC 511 Transit Data Manager"
          logIn = {this.logIn.bind(this)}
          logOut = {this.logOut.bind(this)}
          profile = {this.state.profile}
        />
        {this.isAdmin() ?
          <UserList
            token = {this.state.token}
            feeds={this.feeds}
          /> :
          <Grid><Row><Col xs={12}>You must be logged in as an adminstrator to access this area</Col></Row></Grid> }

      </div>
    )
  }
}
<commit_msg>SSO support<commit_after>import React from 'react'
import $ from 'jquery'

import NavigationBar from './navbar'
import UserList from './userlist'
import PermissionData from './permissiondata'

import config from './config'

import { Grid, Row, Col } from 'react-bootstrap'

export default class App extends React.Component {

  constructor(props) {
    super(props)

    this.state = {
      profile : null
    }

    this.lock = new Auth0Lock(config.auth0ClientID, config.auth0Domain)

    try {
      // Get the user token if we've saved it in localStorage before
      var userToken = localStorage.getItem('userToken');

      if (userToken) {
        this.getProfile(userToken)
      }
      else {
        var hash = this.lock.parseHash(window.location.hash);
        if (hash && hash.id_token) {
          // the user came back from the login (either SSO or regular login),
          // save the token
          localStorage.setItem('userToken', hash.id_token);

          // redirect to "targetUrl" if any
          window.location.href = hash.state || '';
          return;
        }

        // check if logged in elsewhere via SSO
        this.lock.$auth0.getSSOData((err, data) =>  {
          if (!err && data.sso) {
            // there is! redirect to Auth0 for SSO
            this.lock.$auth0.signin({
              callbackOnLocationHash: true
            });
          } else { // assume that we are not logged in
          }
        });

      }
    } catch(err) {
      console.log(err)
      console.log('error retrieving user data from localStorage, clearing and starting over')
      this.logOut()
    }

  }

  logIn () {
    this.lock.show((err, profile, token) => {
      if (err) {
        console.log("There was an error :/", err)
        return
      }

      // save token to localStorage
      localStorage.setItem('userToken', token);

      this.userLoggedIn(token, profile)
    })
  }

  logOut () {
    localStorage.removeItem('userToken')
    window.location.replace('https://' + config.auth0Domain + '/v2/logout?returnTo=' + window.location.href)
  }

  getProfile (token) {
    // retreive the user profile from Auth0
    $.post( "https://" + config.auth0Domain + "/tokeninfo", { id_token: token })
      .done( (profile) => {
        this.userLoggedIn(token, profile)
      })
  }

  userLoggedIn (token, profile) {

    this.permissionData = new PermissionData(profile.app_metadata.datatools)

    console.log('getting feed IDs')
    $.ajax({
      url : config.dataManagerURL + "/api/feedsources",
      data : {
        feedcollection: config.projectID
      },
      headers: {
        'Authorization' : 'Bearer ' + token
      },
      success: (data) => {
        console.log('got feed sources', data)

        // initialize the feed listing, including a catch-all entry at the beginning
        this.feeds = data
        this.feeds.unshift({
          id: "*",
          name: "All Sources"
        })

        this.setState({
          profile: profile,
          token: token
        })
      },
      error: (err) => {
        console.log('error getting feed sources', err)
      }
    })

    // set up single logout
    setInterval(() => {
      // if the token is not in local storage, there is nothing to check (i.e. the user is already logged out)
      if (!localStorage.getItem('userToken')) return;

      this.lock.$auth0.getSSOData((err, data) => {
        // if there is still a session, do nothing
        if (err || (data && data.sso)) return;

        // if we get here, it means there is no session on Auth0,
        // then remove the token and redirect to #login
        localStorage.removeItem('userToken');
        window.location.href = '/'
      });
    }, 5000)
  }

  isAdmin () {
    return this.permissionData && this.permissionData.isProjectAdmin(config.projectID)
  }

  render () {

    return (
      <div>
        <NavigationBar
          title = "MTC 511 Transit Data Manager"
          logIn = {this.logIn.bind(this)}
          logOut = {this.logOut.bind(this)}
          profile = {this.state.profile}
        />
        {this.isAdmin() ?
          <UserList
            token = {this.state.token}
            feeds={this.feeds}
          /> :
          <Grid><Row><Col xs={12}>You must be logged in as an adminstrator to access this area</Col></Row></Grid> }

      </div>
    )
  }
}

{'fraction_non_alphanumeric': 0.12539014791694938, 'fraction_numerical': 0.004478219568462478, 'mean_word_length': 2.0631753948462177, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 34, 'https://': 4, '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': '2786122', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2147, 'n_words': 557}