为什么 express-mysql-session 停止在我的数据库中存储值?

问题描述 投票:0回答:0

我正在学习构建全栈应用程序,我正在尝试构建一个登录表单。目前,我正试图确保会话存储在我的数据库中。我使用 React 作为前端将信息发送到我的 Node/Express 后端。当用户按下登录按钮时,如果用户存在,它应该在我的 MySQL 数据库中创建一个会话(如果用户不存在,它会向客户端返回一些值来解释缺少的内容)。我已经集成了 Passport,它工作了几个小时(使用登录按钮创建会话时存储的值),然后突然停止在数据库中创建任何会话值。

它发生在我从

更改以下行之后
passport.serializeUser(function(user, cb) {
    process.nextTick(function() {
      return cb(null, { id: user.id, username: user.username });
    });
  });
  
passport.deserializeUser(function(user, done) {
    process.nextTick(function() {
      return done(null, user);
    });
});

passport.serializeUser(function(user, cb) {
    return cb(null, { username: user.username });
  });
  
passport.deserializeUser(function(user, cb) {
    return cb(null, user);
});

这些是从 Passport 的教程中提取的。我不明白为什么 express-mysql-session 不再存储值,而且我找不到任何方法来找出问题的根源。这是我的 main.js 文件中的相关代码:

import express from "express";
import cors from "cors";
import users from './routes/users.js'
import session from "express-session";
import path, { dirname } from 'path'
import { fileURLToPath } from 'url';
import passport from "passport";
import expressMySqlSession from "express-mysql-session";

const port = 8080;
const MySQLStore = expressMySqlSession(session);

const sessionStore = new MySQLStore({
    host: 'localhost',
    port: 3306,
    user: ####,
    password: ####,
    database: ####,
})

const app = express(); // Creates Express.js application
app.use(express.json()); // Parses incoming requests containing JSON
app.use(cors()); // Enables CORS

app.use(session({
    key: 'session_cookie_name',
    secret: 'session_cookie_secret',
    store: sessionStore,
    resave: false,
    saveUninitialized: false,
}))

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(express.static(path.join(__dirname, 'public')));

app.use(passport.authenticate('session'))


app.use('/users', users)

从我的用户文件中:

import express from "express";
import passport from "passport";
import LocalStrategy from "passport-local";
import { createHash, randomUUID } from "crypto";
import pool from "../db.js";


const hash = (string) => createHash('sha256').update(string).digest('hex');
const users = express.Router();

passport.use(new LocalStrategy(function verify(username, password, cb) {
    const q = 'SELECT * FROM users WHERE username = ?';
    const values = [username];
    pool.query(q, [values], (err, data) => {
        if (err) {
            console.log('error ran')
            return cb(err);
        }
        if (data.length === 0) {
            console.log('no user ran')
            return cb(null, {errType: 'noUser'})
        }
        if (hash(password) !== data[0].password) {
            console.log('wrong password ran')
            return cb(null, {errType: 'wrongPassword'})
        }
        return cb(null, data)
    })
}))

users.post("/login", (req, res) => {
    passport.authenticate('local', function(err, user) {
        if (err) {
            return res.send(err)
        }
        return res.send(user);
    })(req, res)
})

passport.serializeUser(function(user, cb) {
    return cb(null, { id: user.id, username: user.username });
  });
  
passport.deserializeUser(function(user, cb) {
    return cb(null, user);
});

我已经搜索了大约三个小时,但不知道从哪里开始。任何帮助表示赞赏。

mysql node.js express passport.js
© www.soinside.com 2019 - 2024. All rights reserved.