Monday, 22 May 2017

React NodeJS and PostgreSQL login system

I'm new to React and learning how to use it. I'm trying to make a very simple webpage with a login function. I'm using NodeJS and PostgreSQL for the database. I can't figure out why i cannot access the functions from my database script.

I have Babel, webpack, and pg installed

//index.jsx
import {Test} from "./database.js";

var LoginComp = React.createClass({
  getInitialState: function(){ //constructor
    return{
        showExternalHTML: false,
        user : null,
        password : null,
        newName : null,
        newEmail : null,
        pw1 : null,
        pw2 : null
    };
  },

//Login Handlers
  handleEmailChange: function(e){
    this.setState({
        user: e.target.value
    });
  },

  handlePwChange: function(e){
    this.setState({
        password: e.target.value
    });
  },

  handleLogin: function(){
    Test(this.stateuser, this.state.password);
    console.log("Email: " + this.state.user);
    console.log("Password: " + this.state.password);
  },
//End of LoginHandlers

render(){
  return (
    //html stuff
  )
}

Database.js

const pg = require('pg')  //npm install pg --save
const conString = 'postgres://pi:12345@localhost/postgres'

export function Test(){
  pg.connect(conString, onLogin);
}

function onLogin(username, password){
  client.query(findDataInDatabase(username, 'username'), function(err, result){
    done()
    if(result.rows[0] == username){
      console.log('Username checks out')
      //check password.
    } else {
      console.log('You not in our system');
    }
    if(err){ return console.error('Error: ', err) }
  })
}

function findDataInDatabase(username, dataWeNeed) {
  return 'select ' + dataWeNeed + ' from users where username = \'' + username + '\''
}



via user3307553

No comments:

Post a Comment