如何解决如何在同一应用中两次使用passport-google-oauth20
我试图在我的节点应用程序中两次使用passport-google-oauth20,但无法正常工作。仍在学习护照认证。
这就是问题所在:
我为用户提供了一个使用google(/auth.google)登录的按钮,并为用户提供了使用google(/ users / google)注册的按钮。我想要这样做,以便如果用户已经使用Google在我的应用程序上注册,并且他按了“使用Google注册”按钮,则应该将他重定向回注册页面(身份验证失败)。在我的情况下,当我一次添加策略并仅添加一个登录按钮(不注册)时,auth会起作用,并且用户将重定向到仪表板。但是,当我再次在passport.js中添加策略并且“已经注册的用户”尝试使用google登录时,他将被重定向到注册页面(可以找到使用google按钮进行注册),而不是仪表板。我知道没有必要要做这样的事情,而无需再次使用相同的Straat进行注册,因为它与登录时的工作原理相同。只是想知道是否可以工作。
这是我的代码:
Passport.js
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const myClientId = require("./keys").clientID;
const myClientSecret = require("./keys").clientSecret;
// get the user schema
const User = require("../models/User");
// passport google authentication
module.exports = function (passport) {
passport.use(
new GoogleStrategy(
{
clientID: myClientId,clientSecret: myClientSecret,callbackURL: "/auth/google/callback",},(accesstoken,refreshToken,profile,done) => {
const newUser = new User({
googleId: profile.id,displayName: profile.displayName,firstName: profile.name.givenname,lastName: profile.name.familyName,image: profile.photos[0].value,});
User.findOne({ googleId: profile.id })
.then((user) => {
if (user) {
done(null,user);
} else {
newUser.save().then((userd) => {
done(null,userd);
});
}
})
.catch((err) => console.log(err));
}
)
);
// using same strategy again but different callback URL
passport.use(
new GoogleStrategy(
{
clientID: myClientId,callbackURL: "/users/google/callback",false);
} else {
newUser.save().then((userd) => {
done(null,userd);
});
}
})
.catch((err) => console.log(err));
}
)
);
passport.serializeUser((user,done) => {
done(null,user.id);
});
passport.deserializeUser((id,done) => {
User.findById(id,(err,user) => {
done(err,user);
});
});
};
app.js
/* initalized passport,session and other stuff here but just showing you the routes and the passport
function in app.js */
require("./config/passport")(passport);
app.use("/auth",require("./routes/auth"));
app.use("/users",require("./routes/users"));
auth.js
const express = require("express");
const passport = require("passport");
const router = express.Router();
//? register and login handle
router.get(
"/google",passport.authenticate("google",{
scope: ["profile"],})
);
//? google auth callback
router.get(
"/google/callback",{
successRedirect: "/dashboard",failureRedirect: "/",})
);
module.exports = router;
users.js
const express = require("express");
const router = express.Router();
const passport = require("passport");
const { ensureAuthenticated,forwardAuth } = require("../config/checkAuth");
//? register page
router.get("/register",forwardAuth,(req,res) => {
res.render("register");
});
//? login page
router.get("/login",res) => {
res.render("login");
});
//? logout handle
router.get("/logout",res) => {
req.logout();
res.redirect("/");
});
router.get(
"/google",})
);
router.get(
"/google/callback",failureRedirect: "/users/register",})
);
module.exports = router;
因此,如果用户登录/ auth / google ...,他将被重定向到注册页面(/ users / register)!不知道为什么
解决方法
我解决了我的问题。
解决方案实际上很简单。只需使用相同的password.js文件。
在wallet.js中
const GoogleStrategy = require("passport-google-oauth20").Strategy;
module.exports = function (passport) {
passport.use("firstUse",new GoogleStrategy(....))
passport.use("secondUse",new GoogleStrategy(...))
}
所以事情是,您可以使用所需的任何名称注册策略,然后在要处理路由中的授权中间件时执行以下操作:
在用户和auth.js中
app.get("/auth/google/callback",passport.authenticate("firstUse"));
app.get("/users/google/callback",passport.authenticate("secondUse"));
只需添加您注册时使用的名称。我认为只需要使用passport.authenticate(“ google”)即可,除了“ google”以外无需使用其他任何名称。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。