Connecting elements within an object using VueJs

Within my object "info_login," I gather account information:

async created() {
    try {
      const res = await axios.get(inscriptionURL);
      this.comptes = res.data;
      this.comptes.forEach(element => {
        const data = {'pseudo': element.pseudo, 'password': element.password};
        this.info_login.push(data)
      });

    } catch (e) {
      console.error(e);
    }
  },

The information in "info_login" is as follows:

info_login: [
  {pseudo: Nifoo, password: koko},
  {pseudo: CassyDie, password: azeaze},
]

In my HTML structure:

<div class="champs">
    <label for="pseudo">Pseudo</label>
    <input v-model="pseudo" placeholder="Pseudo" />
  </div>

  <div class="champs">
    <label for="password">Mot de passe</label>
    <input type="password" v-model="password" placeholder="Mot de passe" />
  </div>

I aim to log a message in the console only if the entered username and password match.

Currently, my methods look like this:

checkPseudo(info) {
  return info.pseudo === this.pseudo;
},

checkPassword(info) {
  return info.password === this.password;
},

login() {
  console.log(this.info_login.find(element => element.pseudo === this.pseudo))
  if (this.info_login.find(this.checkPseudo) && this.info_login.find(this.checkPassword)) {
    console.log('OK OK OK OK OK');
  } else {
    console.log('NOOOOOOOON');
  }
}

A issue arises when entering 'Nifoo' as the username and 'azeaze' as the password, resulting in the console showing 'OK OK OK OK OK.'

I seek a solution to ensure that the username corresponds to the correct password. Thank you for your assistance.

Answer №1

To enhance security, don't forget to include a condition for the password as well:

new Vue({
  el: "#demo",
  data() {
    return {
      pseudo: '',
      password: '',
      info_login: [
          {pseudo: 'Nifoo', password: 'koko'},
          {pseudo: 'CassyDie', password: 'azeaze'},
      ]
    }
  },
  methods: {
    checkPseudo(info) {
      return info.pseudo === this.pseudo;
    },

    checkPassword(info) {
      return info.password === this.password;
    },

    login() {
      if (this.info_login.find(element => element.pseudo === this.pseudo && element.password === this.password)) {
        console.log('OK');
      } else {
        console.log('NOOOOOOOON');
      }
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">

  <div class="champs">
    <label for="pseudo">Pseudo</label>
    <input v-model="pseudo" placeholder="Pseudo" />
  </div>

  <div class="champs">
    <label for="password">Mot de passe</label>
    <input type="password" v-model="password" placeholder="Mot de passe" />
  </div>
  
  <button @click="login">login</button>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for accessing elements using document.getElementsByTagName

Greetings and best wishes for the holiday season! I hope everyone is cozy and safe. I need a little help with some code here, as I am fairly new to JavaScript but not entirely new to programming. Despite finding an answer on this site, I am still encounter ...

Exploring SQL Components with JavaScript

Here is the code I am currently using: //This function handles all games and their attributes function handleGames(){ sql.query('SELECT id FROM games', function (err, rows){ if(err){ console.log(String(err).error.bgWhite) ...

Exploring the power of TypeScript for authenticating sessions with NextJS

Utilizing next-auth's getSession function in API routes looks something like this for me: const mySession = await getSession({ req }); I have confirmed that the type of the mySession is outlined as follows: type SessionType = { user: { email: s ...

Struggling to implement server side processing with Datatables in MVC4?

Greetings, our current setup involves an MVC4 application that is handling a large number of records with thumbnail images in a jQuery Datatables driven view. Unfortunately, the loading time is slow due to all thumbnails being fetched at once during a GET ...

How can we eliminate the modal-open class in Angular when transitioning to a different URL?

Currently, I am facing an issue with a bootstrap modal. There is a button inside the modal which upon clicking should navigate the current component to another component named 'questions'. The problem arises when the new component is loaded, as t ...

Move the modal dialog so it appears closer to the top of the page

I am facing a challenge with my jQuery modal dialog. While it is loading properly, I am restricted to using an older version of jQuery (1.12.4) and cannot upgrade it. My goal is to center the modal close to the top of the page, similar to how it is positio ...

Align the center of table headers in JavaScript

I'm currently in the process of creating a table with the following code snippet: const table = document.createElement('table'); table.style.textAlign = 'center'; table.setAttribute('border', '2'); const thead ...

Having trouble getting sweet alert to work with my PHP script

I’m integrating Sweet Alerts 2 into my webpage in order to prompt the user when trying to delete something. However, I'm encountering an issue where the PHP file is not being triggered when the user presses delete and the Sweet Alert does not appear ...

How can you start a jQuery script following a triumphant Ajax callback?

Having some trouble getting jQuery to execute after a successful Ajax response. Even though I have it in the success callback, it doesn't seem to be working as expected. I came across a potential solution here, but I'm having difficulty understan ...

Unable to load JQuery from a div element

My goal is to create a standard .html file containing the navigation, footer, and other elements that will be used across multiple pages for a small site I'm building. I want to keep it simple and avoid using php or other programming languages. I&apo ...

Transforming HTML code into a structured object

The provided code snippet includes HTML markup that needs to be transformed into a specific format. <html> <head> <title>Hello!</title> </head> <body> <div class=”div1”> <h1>This is a ...

Guide to creating a Vue.js v-for loopLet's dive into

Hey there! I've been working on a Vue.js program but the output isn't turning out as expected. I could really use some help with this, would anyone be able to take a look at my code and suggest some modifications? This is the code I have so far: ...

Issue encountered when setting a background image in Angular

When trying to add a background image and set it to cover the entire browser window, I encountered an issue where the image appeared very small and did not fully cover the background. As someone new to Angular, my understanding of how this process works i ...

My Vuejs single page application features automatic page refreshing functionality

I created a single page application using vuejs, and I am currently facing the following issue: My navigation menu is working correctly with vue-router and vue-resource. When I click on a link in the menu, only a part of the page changes without refreshin ...

What steps can be taken to modify this jQuery code so that any changes made are also saved to localStorage

I have successfully used jQuery to change the background color of all my divs with the same class (userNumber1) when I check its checkbox. However, I am now looking for a way to save these changes to localStorage, so that they persist each time the page is ...

Why doesn't Material-UI seem to understand the theme.spacing function?

Issue with Material-UI's theme.spacing function I've encountered a problem while using Material-UI's theme.spacing function in a React application. It seems that the spacing function is not being recognized. The Javascript error message st ...

Despite implementing CORS, my POST request to expressjs is still not functioning properly

I have a function in my component that is responsible for sending a POST request to an express API: onSubmit (evt) { evt.preventDefault(); //alert(JSON.stringify(this.description)); axios.post('http://localhost:3000/api/v1/addCom ...

Having trouble with VueJS project not updating after creating a new Docker Image?

What's the deal: I'm currently diving into Docker and decided to work on a small project to learn the ropes. I have a straightforward VueJs Frontend and a basic Python API set up. I successfully created a Docker Volume with 2 containers (1 for t ...

Is the variable empty outside of the subscribe block after it's been assigned?

Why is a variable assigned inside subscribe empty outside subscribe? I understand that subscribe is asynchronous, but I'm not sure how to use await to render this variable. Can someone please help me and provide an explanation? I am attempting to retr ...

The structure of a project using a Vue app and a Node API

Seeking your thoughts on combining a Vue App with a Node using API for my upcoming project. I have set up two separate folders for the client (VueJs) and server (Node) locally: - client (VueJs) - server (Node) I am currently running them individually usi ...