【问题标题】:Failed to serialize user into session with passportjs and cookie-session无法使用 passportjs 和 cookie-session 将用户序列化到会话中
【发布时间】:2019-01-15 03:23:38
【问题描述】:

我正在尝试使用 Google+ OAuth 进行身份验证。为了实现这一点,我将 passportjs 与 Google 策略(passport-google-oauth20 模块)一起使用,但是当 passport 尝试将用户序列化到会话中(使用 cookie-session)时,我遇到了一个错误。

登录 Google 网站后出现错误。

代码:

passport.serializeUser((user, done) => {
    console.log('serialize ' + (user.id == undefined ? null : user.id));
    console.log(user);
    return done(null, (user.id == undefined ? null : user.id));
});

passport.deserializeUser((id, done) => {
console.log('dserialize id ' + id);
   db.connect((err, client, don) => {
        if (err) throw err
        client.query('SELECT * FROM "AppUsers" WHERE "googleId" = $1', [id], (err, res) => {
            don();
            if (err) {
               console.log(err.stack);
            } else {
               console.log(res.rows[0]);
               if (res.rows[0]) {return done(null, res.rows[0]);}
               else {return done(null, null);}
            }
        });
    });
});

编辑:

async function checkGoogle(profile) {
    const client = await db.connect();
    try {
        const { rows } = await client.query('SELECT * FROM "AppUsers" WHERE "googleId" = $1', [profile.id]);
        let currentUser = rows[0];
        console.log(currentUser);
        if (currentUser) {
            console.log('in db ' + currentUser.id);
            console.log(currentUser);
            return currentUser;
        } else {
            const { rows } = await client.query('INSERT INTO "AppUsers" ("googleId") VALUES ($1) RETURNING *', [profile.id]);
            let newUser = rows[0];
            console.log('not in db ' + newUser.id);
            console.log(newUser);
            return newUser;
        }
    } catch (error) {
        alert(error);
    } finally {
        client.release();
    }
}

passport.use(
    new GoogleStrategy({
        // options for google strategy
        clientID: keys.google.clientID,
        clientSecret: keys.google.clientSecret,
        callbackURL: '/auth/google/redirect'
    }, (accessToken, refreshToken, profile, done) => {
        // check if user already exists in our own db
        return done(null, checkGoogle(profile));
    })
);

输出: The error screen

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 您的user 变量是Promise。这意味着您还没有等待 Promise 的响应完成。你能显示passport.use(new GoogleStrategy({})的代码
  • @DatTran 抱歉耽搁了。

标签: node.js express passport.js google-plus-signin cookie-session


【解决方案1】:

您需要等待 checkGoogle 函数使用 async/await 返回数据。

passport.use(
    new GoogleStrategy({
        // options for google strategy
        clientID: keys.google.clientID,
        clientSecret: keys.google.clientSecret,
        callbackURL: '/auth/google/redirect'
    }, async (accessToken, refreshToken, profile, done) => {
        const user = await checkGoogle(profile);
        // check if user already exists in our own db
        return done(null, user);
    })
);

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 1970-01-01
    • 2021-03-14
    • 2013-11-25
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2019-03-26
    相关资源
    最近更新 更多