我创建了一个 MERN 项目,使用 passport.js 实现了 google 登录。 在本地一切正常,但部署到 render.com 后出现问题。 对于第一次使用 passport.js 登录我的部署网站的 google 帐户,它将永远加载直到超时但是如果我在加载时刷新 google 登录页面并尝试再次登录,那将有效,所以我想知道如何修复这个问题。谢谢!
这是我的设置,
import "dotenv/config.js"
import passport from "passport"
import { Strategy as GoogleStrategy } from "passport-google-oauth20"
import User from "./models/userModel.js"
import uploadPhoto from "./utils/uploadPhoto.js"
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK_URL,
},
async function (accessToken, refreshToken, profile, done) {
const {
sub: id,
name,
given_name,
family_name,
picture,
email,
} = profile._json
const user = await User.findOne({ email })
if (!user) {
// UPLOAD PICTURE TO CLOUDINARY
const photo = await uploadPhoto(null, "user", picture)
const newUser = await User.create({
googleId: id,
username: name,
lastName: family_name,
firstName: given_name,
photo: {
publicId: photo.public_id,
url: photo.secure_url,
},
email,
role: "user",
isVerified: true,
verified: new Date(Date.now()),
// FIXME WHAT IS THE BETTER WAY TO DO
password: "test1234",
passwordConfirm: "test1234",
})
return done(null, newUser)
} else {
if (!user.googleId) {
user.googleId = id
await user.save()
}
return done(null, user)
}
}
)
)
passport.serializeUser((user, done) => {
console.log("serialize")
const { id, email, role } = user
done(null, { id, email, role })
})
passport.deserializeUser(async (user, done) => {
console.log("deserializeUser")
done(null, user)
})