我在下面执行以下代码,我从另一个来源复制了这些代码。如前所述,我只想使用护照对用户进行身份验证,并将 accessToken 存储在 cookie 中,以便稍后在发出进一步的 API 请求时使用。然后在成功时返回 200 状态代码(或在失败时返回其他内容),以便我的前端接收并执行某些步骤。
我的主要问题是代码的执行顺序是什么? 我不明白 passport.use 中的回调函数何时运行,也不明白 router.get("/auth/google/回调”)将要运行。
我只是想调用类似“localhost:8080/auth/google”的东西,存储 accessToken,并向前端返回响应。
谢谢!
const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy;
// Creating express Router
const router = express.Router();
const GOOGLE_CLIENT_ID = "our-google-client-id";
const GOOGLE_CLIENT_SECRET = "our-google-client-secret";
passport.use(
new GoogleStrategy(
{
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:8080/auth/google/callback",
},
function (accessToken, refreshToken, profile, done) {
//store accessToken in cookies here
}
)
);
router.get("/auth/google", passport.authenticate("google", { scope: ["profile"] }));
router.get(
"/auth/google/callback",
passport.authenticate("google", { failureRedirect: "/error" }),
function (req, res) {
// Successful authentication, redirect success.
res.redirect("/success");
}
);