Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
ReactJS: Login form with array of users

I want to create a login form with array data in User.js file (without any backend)

My User.js file looks like this:

const users = [
  {
    username: 'admin1',
    password: 'admin1@1'
  },
  {
    username:'admin2',
    password:'admin2@2'
  }
];

and my Login.js looks something like this:

import React, { Component, Fragment } from 'react';

class Login extends React.Component {
  
  constructor(props) {
    super(props);
    this.state = {
      userName: "",
      password: ""
    };
  }

  changeInputValue(e) {
    this.setState({
      [e.target.name]: e.target.value
    });
  }

  validationForm() {
    let returnData = {
      error : false,
      msg: ''
    }
    const {password} = this.state
    //Check password
    if(password.length < 8) {
      returnData = {
        error: true,
        msg: 'Password must be more than 8 characters'
      }
    }
    return returnData;
  }

  submitForm(e) {
    e.preventDefault();
    const validation = this.validationForm()
    var username = e.target.elements.username.value;
    var password = e.target.elements.password.value;
    if (validation.error) {
      alert(validation.msg)
    }else if(username === 'admin2' && password === 'admin1@1') {
      alert("Login successful");
    }else {
      alert("Wrong password or username");
    }
  }
    
  render() {
    return (
      <div className="container" style={{ paddingTop: "5%" }}>
        <form
          onSubmit={e => {
            this.submitForm(e);
          }}
        >
          <div className="form-group">
            <input
              type="text"
              className="form-control"
              name="username"
              placeholder="Username"
              onChange={e => this.changeInputValue(e)}
            />
          </div>
          <div className="form-group">
            <input
              type="password"
              className="form-control"
              name="password"
              placeholder="Password"
              onChange={e => this.changeInputValue(e)}
              
            />
          </div>
          <button value="submit" className="btn btn-primary" onClick={this.postDetails}>
            Submit
          </button>
        </form>
      </div>
    
    );
  }
}

export default Login;

Above, I don't know how to check username and password from the passed array. Please show an instance of how it is done. And after successful login, how do I switch to another page?Sorry, I'm new to ReactJS so I'm a bit confused, hope you can help.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Import the data from user.js after u exported it from there.

export const UsersData = [
{
    username: 'admin1',
    password: 'admin1@1'
  },
  {
    username:'admin2',
    password:'admin2@2'
  }
];

//Login.js

import UsersData from './user.js'

Now do array operation on this array of objects. Now you can convert the data entered by the user to a similar format provided by User.js.

ie: If the user entered username = alphabeta, password = 1234

submitForm(e) {
    e.preventDefault();
    const validation = this.validationForm()
    var inputData = {
     username : e.target.elements.username.value,
     password : e.target.elements.password.value
    };
  if (validation.error) {
      alert(validation.msg)
    }else if(UsersData.findIndex(inputData)!==-1) {
      alert("Login successful");
    }else {
      alert("Wrong password or username");
    }
  }

Here we uses an array operation called findIndex which returns -1 if it doesn't find the object in the array else it returns the index.

To know more about Array Opreration in JS : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

about 4 years ago · Juan Pablo Isaza Report

0

Not sure if I understand correstly your question but here is how you can check if the duo user and password are in your array :

import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';

const App = () => {
  const users = [
    {
      username: "admin1",
      password: "admin1@1",
    },
    {
      username: "admin2",
      password: "admin2@2",
    },
  ];

  const handleSubmit = (e) => {
    e.preventDefault();
    
    const toCompare = {
      username: e.target.elements.username.value,
      password: e.target.elements.password.value,
    };
    
    // Or just compare properties  
    if (users.some(u => JSON.stringify(u) === JSON.stringify(toCompare))) {
      console.log('User exists in array');
    } else {
      console.log('User does not exist in array');
    }
  };
  
  return (
    <form onSubmit={handleSubmit}>
      <input type="text" placeholder="username" name="username" />
      <input type="text" placeholder="password" name="password" />
      <button type="submit">Send</button>
    </form>
  );
};

render(<App />, document.getElementById('root'));

And here is the repro on Stackblitz

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!