错误:未知的身份验证策略“ facebook”

时间:2019-04-28 08:08:33

标签: node.js facebook express passport.js

我尝试在后端添加Facebook身份验证API。我正在向邮递员查询。 错误消息为:错误:未知身份验证策略“ facebook”

名为http://localhost:3000/users/auth/facebook

的请求

server / index.js

const express = require("express");
const app = express();
const PORT = require("../lib/config/config");
const db = require("../lib/database/db");
const products = require("../lib/router/product");
const passport = require("passport");
const facebook = require("../lib/router/login"); //(passport);

app.use(passport.initialize());
app.use(passport.session());
app.use(express.json());
app.use("/Movie/v1.0/Add", products);
app.use("/users", facebook);

app.listen(PORT.app.PORT, () => {
  console.log(`Conntected on ${PORT.app.PORT}`);
});

server / passport.js

const config = require("../lib/config/config");
const User = require("../lib/models/User");
var passport = require("passport"),
  FacebookStrategy = require("passport-facebook").Strategy;
passport.use(
  new FacebookStrategy(
    {
      clientID: config.oauth.facebook.clientID,
      clientSecret: config.oauth.facebook.clientSecret,
      callbackURL: "http://localhost:3000/users/oauth/facebook"
    },
    function(accessToken, refreshToken, profile, done) {

      process.nextTick(function() {
        User.findOne({ "facebook.id": profile.id }, function(err, user) {
          if (err) {
            return done(err);
          }
          if (user) {
            return done(null, user);
          } else {
            var newUser = new User();
            newUser.facebook.id = profile.id;
            newUser.facebook.token = accessToken;
            newUser.facebook.name =
              profile.name.givenName + " " + profile.name.familyName;
            newUser.facebook.email = profile.emails[0].value;
          }
          newUser.save(function(err) {
            if (err) return err;
            return done(null, newUser);
          });
        });
      });
    }
  )
);

router / login.js

const express = require("express");
const passport = require("passport");
const app = express();


app.get(
  "/auth/facebook",
  passport.authenticate("facebook", { scope: ["email"] })
);

app.get(
  "/auth/facebook/callback",
  passport.authenticate("facebook", {
    successRedirect: "/Movie/v1.0/Add",
    failureRedirect: "/"
  })
);
module.exports = app;

问题编号02: 如果一切正常,如何将这个API添加到React?

0 个答案:

没有答案