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

179
Views
passport.js deserialize user with mysql connection

How can I deserialize a User in passport.js when im connecting it to a db? It worked when I used an array but as soon as it was connected to the database it stopped working. How can I fix that?

Error: Failed to deserialize user out of session
    at pass (/Users/frederik/IdeaProjects/vr/node_modules/passport/lib/authenticator.js:341:19)
    at deserialized (/Users/frederik/IdeaProjects/vr/node_modules/passport/lib/authenticator.js:346:7)
    at /Users/frederik/IdeaProjects/vr/src/login/passport-config.ts:42:54
    at pass (/Users/frederik/IdeaProjects/vr/node_modules/passport/lib/authenticator.js:354:9)
    at Authenticator.deserializeUser (/Users/frederik/IdeaProjects/vr/node_modules/passport/lib/authenticator.js:359:5)
    at SessionStrategy.authenticate (/Users/frederik/IdeaProjects/vr/node_modules/passport/lib/strategies/session.js:61:10)
    at attempt (/Users/frederik/IdeaProjects/vr/node_modules/passport/lib/middleware/authenticate.js:369:16)
    at authenticate (/Users/frederik/IdeaProjects/vr/node_modules/passport/lib/middleware/authenticate.js:370:7)
    at Layer.handle [as handle_request] (/Users/frederik/IdeaProjects/vr/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/frederik/IdeaProjects/vr/node_modules/express/lib/router/index.js:323:13)
const authenticateUserDB = (email: string, password: string, done: any) => {
        sql._getInstance()._getConnection().query('SELECT * FROM `users` WHERE ?',{email:email}, function (error:MysqlError|null, results:any) {
            if (error) return done(error, false, "No database connection");
            if(results.length == 0) return done(null, false, "No User with that email");
            //TODO:encrypt
            if(password == results[0].password){
                return done(null, {
                    id: results[0]['uuid'],
                    name: results[0]['name'],
                    email: results[0]['email'],
                    password: results[0]['password']
                });
            }else{
                return done(null, false, "Password is incorect");
            }
        });
    }

    const getUserByUUID = (uuid:string)=>{
        sql._getInstance()._getConnection().query('SELECT * FROM `users` WHERE ?',{uuid:uuid}, function (error:MysqlError|null, results:any) {
            if (error) throw Error();
            if(results.length == 0) throw Error();
            return {
                id: results[0]['uuid'],
                name: results[0]['name'],
                email: results[0]['email'],
            }

        });
    }

    passport.use(new Strategy({usernameField: 'email'}, authenticateUserDB));
    passport.serializeUser((user: any, done: any) => done(null, user.id));
    passport.deserializeUser((id: any, done: any) => done(null, getUserByUUID(id)));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem was that the deserialize didn't wait for getUserByUUid

const getUserByUUID = (uuid: string) => {
        return new Promise((resolve, reject) => {
            sql._getInstance()._getConnection().query('SELECT * FROM `users` WHERE ?', {uuid: uuid}, function (error: MysqlError | null, results: any) {
                if (error) reject("");
                if (results.length == 0) reject("");
                resolve({
                    id: results[0]['uuid'],
                    name: results[0]['name'],
                    email: results[0]['email'],
                    password: results[0]['password']
                });

            });
        })

    }
passport.deserializeUser(async (id: any, done: any) => done(null, await getUserByUUID(id)));
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!