Changed over to a promise-oriented database, causing my login feature to malfunction completely

Although I can successfully register, when I am redirected to my game route, all I see is a blank Error page with [object Object] on the screen. This message also appears in my console periodically.

Initially, I suspected an issue related to socket.io, but it appears that the problem lies elsewhere since it doesn't even reach that stage. My hunch is that there might be a configuration error with passport and how it aligns with the promise route I'm following. However, pinpointing the exact location of this error is proving challenging for me.

Below is the snippet from the passport file:

/*jshint esversion: 6 */
const LocalStrategy = require('passport-local').Strategy;
const db = require('../config/db');
const bcrypt = require('bcryptjs');
let io = require('./io');

module.exports = (passport) => {

  // Local Strategy login
  passport.use('local-login', 
  new LocalStrategy((username, password, done) => {
    console.log('username');
    // Match Username
    let sql = 'SELECT * FROM users WHERE username = ?';
    db.query(sql, [username]).then(results => {
      if (!results.length) {
        return done(null, false, {
          type: 'loginMessage',
          message: 'Wrong Login',
        });
      }
      console.log('password');
      //  Match Password
      bcrypt.compare(password, results[0].password, (err, isMatch) => {
        if (isMatch) {
          console.log('Password is correct');
          return done(null, results[0]);
        } else {
          return done(null, false, {
            type: 'loginMessage',
            message: 'Wrong Login',
          });
        }
      });
    });
  }));

  // =========================================================================
  // passport session setup ==================================================
  // =========================================================================

  // used to serialize the user for the session
  passport.serializeUser((user, done) => {
    console.log(user.username + ' has been Serialized');
    done(null, user.id);
  });

  // used to deserialize the user
  passport.deserializeUser((id, done) => {
    db.query('SELECT * FROM users WHERE id = ?', [id]).then(results => {
      console.log(results[0].username + ' has been deserialized');
      done(results[0]);
    });
  });
};

Despite numerous attempts to debug, I have been unable to resolve the login issue. If further information is required, please feel free to ask. Any insights or suggestions would be greatly appreciated.

Answer №1

I made a silly mistake and forgot that I handle errors within the query itself since switching to a promise system. This means there was no need for me to pass it through.

Instead of just using done(results[0]) in the passport.deserializeUser..., all I had to do was add null before it like this: done(null, results[0]). It's frustrating to realize I wasted so much time on such a simple issue. I feel like a fool.

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

Is it achievable for a modal popup to automatically move to a specified location?

I need assistance with... Is it feasible to display an external webpage within a modal window and have that page automatically scroll to a specific section (such as an anchor point)? ...

How can I identify when a node/express ajax request is received?

I have set up a node/express server that sends the ajax-start.html file. Within this file, there is a script that enables ajax requests to be made to the server. Everything functions correctly in this setup. However, I am looking to enhance this process by ...

The data retrieved from the web API is not undergoing the necessary conversion process

I am facing an issue with a web API call where the property checkNumber is defined as a double on the API side, but I need it to be treated as a string in my TypeScript model. Despite having the property defined as a string in my model, it is being receive ...

What is the best way to retrieve the value of a textbox in AngularJS?

Trying my hand at creating a basic web page using angular. I've got 2 textboxes and 2 buttons - one to set a predefined value in a textbox, and the other to add some text. Here's the code snippet: <!DOCTYPE html> <html lang="en" ng-app ...

EventEmitter - Unable to listen for an event

Hello all! I am facing an issue with implementing the observer pattern in my application. The problem is that the subscribers are unable to listen to events. When a function (useEvent.js) triggers an event, nothing seems to happen. Below are the snippets ...

What is the best way to extract function bodies from a string with JavaScript?

I am currently searching for a solution to extract the body of a function declaration by its name from a JavaScript code string within a Node.js environment. Let's assume we have a file named spaghetti.js that can be read into a string. const allJs = ...

NuxtJs: Oops! It looks like NuxtError is not defined in this context

Exploring NuxtJs is new to me. I decided to experiment with how nuxt-link functions by purposely setting up a nuxt-link to a non-existent route in order to trigger the default 404 page. Here's the line of code I added to the pages/index.vue file: < ...

Using jQuery and Bootstrap in an ASP.NET Core project

I am encountering an issue with the configuration of bootstrap and jquery within my project, causing these tools to fail to load properly. The problem seems to be that bootstrap is loading before jquery, resulting in error messages appearing when I check ...

What could be the reason for my ajax request coming back with no data?

When I make this ajax request: $.ajax({ type: "POST", url: "/admin/RoutingIndicators/Add", data: { newSecondaryRI: newRi }, success: function () { alert('hit'); document.location.reload(true); }, error: fu ...

Can you explain the significance of "var router = require('./router/main')(app)"?

(I came across a similar question on stackoverflow, but I'm having trouble understanding the comments..) Hello, I am a middle school student from Korea. English is not my strong suit, so please bear with me. Currently, I am learning Node.js and Expr ...

Using requestAnimationFrame to animate several independent objects

I'm currently working on animating two different objects: a square and a circle. Each object has its own button to initiate the animation, which involves moving the object from left to right. However, I've run into an issue where clicking on one ...

How to submit form data with a POST request in Flask using fetch without having to reload

Despite reading numerous similar questions, I am still unable to determine how to achieve my goal. I have multiple forms on a single page and I am trying to submit data from each form without refreshing the page. Below is an example of one of the five form ...

How to identify the character encoding in a node.js request

Did you know that Facebook chat has a feature where it automatically detects and displays messages in a left-to-right format when typing in English, but switches to right-to-left style when adding right-to-left characters? I'm curious about how Faceb ...

Troubleshooting JSON Array Index Problems

I'm having trouble reaching index 3 in the array using the javascript options on my webpage. The final question "are you satisfied with your choice?" is not showing up for me. I'm not sure what I might be missing or doing incorrectly in this sit ...

Forest Admin's route for /forest/authentication cannot be found and is returning a 404

I am struggling to configure Forest Admin in my local environment using Express and MySQL. Whenever I attempt to authenticate, I encounter a 404 error for the /forest/authentication route. https://i.stack.imgur.com/aEXTL.png app.js const createError = re ...

The usage of 'import.meta' is restricted to within modules and cannot be utilized outside of them

I am working on a project that involves Node + Express + Babel + ES6. Within this project, I have the following files: /package.json { "name": "test-backend", "version": "1.0.0", "description": " ...

The essential information for the data confirmation should consist of either the name or value of the selected

I am looking to incorporate dynamic content into a data confirmation message that appears when the user clicks on submit. In my views, I have: <div class="form-check"> <input class="form-check-input" type="radio" na ...

Factorial calculating application on the web using Node.js

My goal is to create a nodejs program that calculates the factorial of numbers less than 30, a common exercise among beginner programmers. fact(0) = 1 fact(i) = i*fact(i-1) This time, I want the nodejs program to display the output directly on the client ...

How can I apply various middlewares to individual Monk's Manager instances?

Currently, I am utilizing monk () for handling mongodb data manipulation tasks. Monk provides a middleware mechanism where we can easily add multiple middleware by invoking addMiddleware (link here: ) Everything seemed to work smoothly until I created tw ...

Place the child DIV at the bottom of its parent DIV

I've searched through open questions but couldn't find a solution. The code snippet in question looks like this: ... <div style="some height"> <div style="some width"> .......and so on.. .. < ...