<commit_before>
var Session = React.createClass({
  contextTypes: { main: React.PropTypes.any.isRequired },
  getInitialState: function() { return {
    remoteDB: null,
  }},
  
  logIn: function(username, password, onSuccess, onError) {
    var dbUrl = "http://forages-db.ilvain.com:5984/" + username
    var db = new PouchDB(dbUrl, { skipSetup: true })
    
    db.login(username, password).then(function (user) {
      this.setState({ remoteDB: db })
      this.context.main.setState({ loggedIn: true })
      
      onSuccess()
    }.bind(this)).catch(function (error) {
      console.error(error)
      
      onError(error)
    })
  },
  
  logOut: function() {
    if (this.state.remoteDB)
      this.state.remoteDB.logout()
    
    this.context.main.setState({ loggedIn: false })
  },
  
  render: function() { return null }
})<commit_msg>Get access to user-private application-specific db in Session.<commit_after>
var Session = React.createClass({
  contextTypes: { main: React.PropTypes.any.isRequired },
  getInitialState: function() { return {
    remoteDB: null,
  }},
  
  dbUrl(name)    { return "http://forages-db.ilvain.com:5984/" + name },
  dbCreate(name) { return new PouchDB(this.dbUrl(name), { skipSetup: true }) },
  
  logIn: function(username, password, onSuccess, onError) {
    var appName = "forages"
    
    // Log in to central _users database.
    var usersDB = this.dbCreate("_users")
    usersDB.login(username, password).then(user => {
      // Get user info for this user from the _users database.
      return usersDB.get("org.couchdb.user:" + username).then(userInfo => {
        // Log in to this user's private database for this application.
        var privateDB = this.dbCreate(userInfo[appName].dbname)
        return privateDB.login(username, password).then(user => {
          // Save the database handle and mark as logged in.
          this.setState({ remoteDB: privateDB })
          this.context.main.setState({ loggedIn: true })
          
          onSuccess()
        })
      })
    }).catch(error => {
      console.error(error)
      
      onError(error)
    })
  },
  
  logOut: function() {
    if (this.state.remoteDB)
      this.state.remoteDB.logout()
    
    this.context.main.setState({ loggedIn: false })
  },
  
  render: function() { return null }
})
{'fraction_non_alphanumeric': 0.1366968520914187, 'fraction_numerical': 0.0034497628288055198, 'mean_word_length': 2.694267515923567, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3269181', 'n_tokens_mistral': 728, 'n_tokens_neox': 725, 'n_words': 161}