Compare the data submitted in the form with data in json file in reactjs

error screenshotcode for compare a data in json file with data in submitted form in reactjs... My program is a login page..I want to compare the data that is submitted with the data in json file..The datas are username and password... Please please help me..

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { login } from '../../redux/reducer';
import './LoginForm.css';
import logindetls from '../../logindet.json';

class LoginForm extends Component {

  constructor(props) {
    super(props);
    this.state = {};
    this.onSubmit = this.onSubmit.bind(this);
  }

  render() {
    let {username, password} = this.state;
    let {isLoginPending, isLoginSuccess, loginError} = this.props;

    return (

      <form name="loginForm" onSubmit={this.onSubmit}>
        <div className="form-group-collection">
          <div className="form-group">
            <label>Username/User ID:</label>
            <input name="username" onChange={e => this.setState({username: e.target.value})} value={username}/>
          </div>

          <div className="form-group">
            <label>Password:</label>
            <input type="password" name="password" onChange={e => this.setState({password: e.target.value})} value={password}/>
          </div>
        </div>


        <input type="submit" value="Login" />

        <div className="message">
          { isLoginPending && <div className= "loader"></div>
                          }
          { isLoginSuccess && alert("Successfully Logged in...") }
          { loginError && alert(loginError.message) }
        </div>
      </form>
    )
  }






  onSubmit(e) {
    e.preventDefault();
    let { username, password } = this.state;
    this.props.login(username, password);
    this.setState({
      username: '',
      password: ''
    });
  }
}


const mapStateToProps = (state) => {
  return {
    isLoginPending: state.isLoginPending,
    isLoginSuccess: state.isLoginSuccess,
    loginError: state.loginError
  };
}

const mapDispatchToProps = (dispatch) => {
  return {
    login: (username, password) => dispatch(login(username, password))
  };
}

export default connect(mapStateToProps, mapDispatchToProps)(LoginForm);

this is my loginform page

import logindetls from '../../logindet.json';

const SET_LOGIN_PENDING = 'SET_LOGIN_PENDING';
const SET_LOGIN_SUCCESS = 'SET_LOGIN_SUCCESS';
const SET_LOGIN_ERROR = 'SET_LOGIN_ERROR';

export function login(username, password) {
  return dispatch => {
    dispatch(setLoginPending(true));
    dispatch(setLoginSuccess(false));
    dispatch(setLoginError(null));

    callLoginApi(username, password, error => {
      dispatch(setLoginPending(false));
      if (!error) {
        dispatch(setLoginSuccess(true));
      } else {
        dispatch(setLoginError(error));
      }
    });
  }
}

function setLoginPending(isLoginPending) {
  return {
    type: SET_LOGIN_PENDING,
    isLoginPending
  };
}

function setLoginSuccess(isLoginSuccess) {
  return {
    type: SET_LOGIN_SUCCESS,
    isLoginSuccess
  };
}

function setLoginError(loginError) {
  return {
    type: SET_LOGIN_ERROR,
    loginError
  }
}

function callLoginApi(username, password, callback) {
  setTimeout(() => {

    if (username==logindetls.username && password==logindetls.password)
        {
        return callback(null);
    } else {
      return callback(new Error('Invalid username and password'));
    }
  }, 1000);
}


export default function reducer(state = {
  isLoginSuccess: false,
  isLoginPending: false,
  loginError: null
}, action) {
  switch (action.type) {
    case SET_LOGIN_PENDING:
      return Object.assign({}, state, {
        isLoginPending: action.isLoginPending
      });

    case SET_LOGIN_SUCCESS:
      return Object.assign({}, state, {
        isLoginSuccess: action.isLoginSuccess
      });

    case SET_LOGIN_ERROR:
      return Object.assign({}, state, {
        loginError: action.loginError
      });

    default:
      return state;
  }
}

this is my reducer page... What all changes I want to make.. My json page is

const logindetls = [
  {
    "username": "admin",
    "password": "password"
   }
];

2 answers

  • answered 2018-04-17 05:02 Shahid Peerzade

    You can make use of json-server npm package. using json-server you can create fake rest api and watch a json file. They have very clear docs how to make one.

    Edit
    watch your file using json-server like

    json-server --watch <your json file name her> --port <port number>
    

    for example

    json-server --watch db.json --port 8081
    

    it will create a fake rest api server which will listen on localhost:8081 then make call to the link using Fetch api from your redux action like this

    export function selectedNews(id) {
    const URL = "http://localhost:8081"
    const request = fetch(`${URL}/user? 
          username=${username}&password=${password}`, {
          method: 'GET'
          }).then(res => res.json())
    
        return {
            type: <put your reducer type here>,
            payload: request
        }
    }
    

    if username and password found inside db.json , value of request variable will be those username and password. otherwise undefined.

  • answered 2018-04-17 05:02 pritesh

    I am not sure what u want but from my understanding.

    Add export in .json file in LoginForm as

    export const logindetls = [
      {
        "username": "admin",
        "password": "password"
      }
    ];
    

    You can then simply import the .json file

    import { logindetls } from './[filename].json' //Here you need to give the file name.
    

    and in onSubmit function you can compare as

    console.log(this.state.username === logindetls[0].username)