Javascript Passport Google Oauth在登录时不会重定向到成功路由

Javascript Passport Google Oauth在登录时不会重定向到成功路由,javascript,express,passport.js,google-oauth,Javascript,Express,Passport.js,Google Oauth,我正在尝试按照教程使用Passport.js在我的Express应用程序中设置GoogleOAuth 以下是我在server.js文件中定义的路由: const passport=require(“passport”); const express=要求(“express”); 常量app=express(); const cors=要求(“cors”); const bodyParser=require(“body parser”); const cookieSession=require(“

我正在尝试按照教程使用Passport.js在我的Express应用程序中设置GoogleOAuth

以下是我在
server.js
文件中定义的路由:

const passport=require(“passport”);
const express=要求(“express”);
常量app=express();
const cors=要求(“cors”);
const bodyParser=require(“body parser”);
const cookieSession=require(“cookie会话”);
要求(“/护照设置”);
应用程序使用(cors());
//解析应用程序/x-www-form-urlencoded
use(bodyParser.urlencoded({extended:false}));
use(bodyParser.json());
应用程序使用(
库克会议({
名称:“图托节”,
键:[“键1”、“键2”],
})
);
app.use(passport.initialize())//初始化passport js和身份验证过程
app.use(passport.session())//使用会话进行管理
常量isLoggedIn=(请求、恢复、下一步)=>{
如果(请求用户){
next();
}否则{
res.sendStatus(401);
}
};
app.get(“/”,(req,res)=>res.send(“你好,世界!你没有登录”);
// 1. 您将用户发送到/google。这将在passport-setup.js中打开一个google页面。
app.get(
“/谷歌”,
passport.authenticate(“谷歌”{
范围:[“个人资料”、“电子邮件”],
})
);
app.get(“auth/google/callback”,
passport.authenticate(“谷歌”{
successRedirect:“/good”,
failureRedirect:“/失败”
}),
功能(req、res){
res.redirect(“http://localhost:19006/good");
}
);
app.get(“/failed”,(req,res)=>res.send(“您登录失败!”);
app.get(“/good”,isLoggedIn,(请求,回复)=>
res.send(`您已登录!欢迎${req.user.email}`)
);
app.get(“/logout)”,(请求、回复)=>{
req.session=null;//销毁会话
req.logout();//从passport注销
res.redirect(“/”;//重定向到主目录。
});
app.listen(19006,()=>console.log(“监听端口”);
server.js
还导入了
passport setup.js
,我在其中存储了以下配置:

const passport = require('passport')
const GoogleStrategy = require('passport-google-oauth20').Strategy
// it will take the user, and take user.id. This makes the user smaller. Each time I'm directed to a route, will take the cookie in the session, go to deserializeUser 
passport.serializeUser(function (user, done) {
  console.log("serialise user")
  done(null, user)
})

// this function takes the id, returns the user object.
passport.deserializeUser(function (user, done) {
  console.log("deserialise user")
  //User.findById(id, function (err, user) {
  done(null, user)
  //})
  // so we will have the user in req.user. But for the purpose of this tutorial, no db.
})
// this /google page will take clientID, client secret to authenticate.
passport.use(new GoogleStrategy({
  clientID: 'MY_CLIENT_ID',
  clientSecret: 'MY_CLIENT_SECRET',
  callbackURL: "http://localhost:19006/auth/google/callback",
  passReqToCallback: true,
},
  function (accessToken, refreshToken, profile, done) {
      return done(null, profile)
  }
))
启动应用程序时,我会转到
localhost:19006/google
,这会将我重定向到google Oauth的登录页面。但是,我被重定向到此链接:http://localhost:19006/auth/google/callback?code=4%2F3AGyrRpoCivQNr2-7sxzdqetvzni9jvxao或htmaazoqjihnpaf8nwqgbfkzrvprwhuf4qmo40ljxigzlsbln7u&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=consent#

我还得到一个404错误代码,页面显示“cannotget/auth/google/callback”

我检查了快速路线,并定义了该路线,因此我不明白为什么应用程序不显示此页面。 我尝试将console.log消息添加到以下函数:

// this /google page will take clientID, client secret to authenticate.
passport.use(new GoogleStrategy({
  clientID: 'MY_CLIENT_ID',
  clientSecret: 'MY_CLIENT_SECRET',
  callbackURL: "http://localhost:19006/auth/google/callback",
  passReqToCallback: true,
},
  function (accessToken, refreshToken, profile, done) {
   //added a console.log here
      return done(null, profile)
  }

app.get("/", (req, res) => res.send("Hello World! You're not logged in "));
// 1. You send your users to /google. This opens a google page, in passport-setup.js.
app.get(
  "/google",
  passport.authenticate("google", {
    scope: ["profile", "email"],
  })
);

app.get("auth/google/callback",
  passport.authenticate("google", { 
    successRedirect: '/good', 
    failureRedirect: "/failed" 
  }),
  function (req, res) {
// added console.log here
    res.redirect("http://localhost:19006/good");
  }
);
但是我的控制台没有记录任何东西

我试过的 在Google开发者控制台中,我使用以下字段设置web应用程序的客户端ID:

在我的代码和Google开发者界面中,我尝试用
127.0.0.1
替换
localhost
,但都不起作用。

你错过了“/”

你错过了“/”


在googleconsole.dev。。授权重定向URI添加此
http://localhost:19006/good
您好,谢谢您的回复!不幸的是,这对我来说不起作用,我仍然面临相同的错误。您已成功重定向到google auth,但问题是在您登录后,您收到404错误。我是否正确?是的,我可以看到页面,在该页面中,我必须选择要使用我的应用程序登录的google帐户。在我点击google帐户后,我被重定向到这个链接:页面上会显示“Cannot/get/auth/google/callback”,你可以从路由中删除
successRedirect:“/good”
,只在google console.dev中留下
failureRedirect
。。授权重定向URI添加此
http://localhost:19006/good
您好,谢谢您的回复!不幸的是,这对我来说不起作用,我仍然面临相同的错误。您已成功重定向到google auth,但问题是在您登录后,您收到404错误。我是否正确?是的,我可以看到页面,在该页面中,我必须选择要使用我的应用程序登录的google帐户。在我点击google帐户后,我被重定向到这个链接:页面上会显示“Cannot/get/auth/google/callback”你能从路由中删除
successRedirect:“/good”
,只留下
failureRedirect
app.get("/auth/google/callback",
  passport.authenticate("google", { 
    successRedirect: '/good', 
    failureRedirect: "/failed" 
  }),