gpt4 book ai didi

node.js - 内部 OAuth 错误 : Failed to fetch user profile NodeJs+passport-google-oAuth2

转载 作者:行者123 更新时间:2023-12-03 08:46:13 24 4
gpt4 key购买 nike

我正在学习使用 Passport-google-oauth20 进行身份验证。当我尝试使用 Google+ 注册用户时,在提交请求失败后,我不断收到此错误消息(InternalOAuthError:无法获取用户个人资料)。

我尝试了这个解决方案:

  1. 我启用了 Google+ API 并等待。

2.已弃用的范围

//jshint esversion:6
require('dotenv').config()
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require('passport-local-mongoose');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const findOrCreate = require('mongoose-findorcreate');

const app = express();

app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
// save user sessionusing cookies
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
}));

//initializa passport and use it to manage sessions
app.use(passport.initialize());
app.use(passport.session());

mongoose.connect('mongodb://localhost:27017/userDB', {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
mongoose.set("useCreateIndex", true);

const userSchema = new mongoose.Schema({
email: String,
password: String
});

userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);

const user = mongoose.model("User", userSchema);

// use static authenticate method of model in LocalStrategy
passport.use(user.createStrategy());

passport.serializeUser(function(user, done) {
done(null, user.id);
});

passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});

passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/secrets",
userProfileURL: "https: //www.googleapis.com/oauth2/v3/userinfo"

},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
user.findOrCreate({ googleId: profile.id }, function(err, user) {
return cb(err, user);
});
}
));


app.get("/", function(req, res) {
res.render("home");
});
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile'] }));

app.get('/auth/google/secrets',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect secrets.
res.redirect('/secrets');
});

app.get("/login", function(req, res) {
res.render("login");
});
app.get("/register", function(req, res) {
res.render("register");
});
app.get("/secrets", function(req, res) {
if (req.isAuthenticated) {
res.render("secrets");
} else {
res.redirect("/login");
}
});
app.get("/logout", function(req, res) {
req.logout();
res.redirect("/");
});


app.post("/register", function(req, res) {
user.register({
username: req.body.username
}, req.body.password, function(err, user) {
if (err) {
console.log(err);
res.redirect("/register");
} else {
passport.authenticate("local")(req, res, function() {
res.redirect("/secrets");
});
}
});

});


app.post("/login", function(req, res) {

const user = new user({
name: req.body.username,
password: req.body.passwword
});
req.login(user, function(err) {
if (err) {
console.log(err);
} else {
passport.authenticate("local")(req, res, function() {
res.redirect("/secrets");
});
}
});

});


app.listen(3000, function() {
console.log("Server started on port 3000");
});ode here

最佳答案

在我绞尽脑汁两天后发现,问题出在node-auth包上,但node-oauth似乎是一个死项目(最后一次提交是在2017年),这个错误将影响passport的用户-google-oauth2 快速连接。请查找下面解释的问题:

https://github.com/jaredhanson/passport-google-oauth2/issues/87

我觉得在没有passport js的情况下实现google-Oauth会更好。

关于node.js - 内部 OAuth 错误 : Failed to fetch user profile NodeJs+passport-google-oAuth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61360413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com