如何使用Strava API在React中进行身份验证

时间:2019-09-06 15:32:06

标签: node.js reactjs authentication passport.js

我有一个基本的SPA,React App在本地计算机上使用节点服务器运行,并且正在尝试使用passport-strava策略通过Strava来对用户进行身份验证。我可以访问Strava授权页面,但是在Strava登录页面上允许授权后,我无法将用户重定向回其个人资料页面。

在网上寻找其他类似的问题,我认为我在某个地方缺少参数,但不知道在哪里?我尝试在Passportjs.org上关注this example,但也无法正常工作。我尝试在其他版本中也包括scopestate,但这也不能解决问题。

这是我的server/index.js文件,具有所有护照配置:

const express = require("express");
const cors = require("cors");
const passport = require("passport");
const StravaStrategy = require("passport-strava").Strategy;
const keys = require("../config");
const chalk = require("chalk");
require('https').globalAgent.options.rejectUnauthorized = false;

let user = {};

// Passport requires these
passport.serializeUser( (user, cb) => {
  cb(null, user);
});

passport.deserializeUser( (user, cb) => {
  cb(null, user);
});

// Strava Strategy
passport.use(new StravaStrategy({
  clientID: keys.STRAVA.clientID,
  clientSecret: keys.STRAVA.clientSecret,
  // What will it do after verifying user with log-in credentials
  callbackURL: "http://127.0.0.1:3000/auth/strava/callback"
  },

  // callback function that will be run right after making request to Strava API
  (accessToken, refreshToken, profile, cb) => {
    console.log(chalk.blue(JSON.stringify(profile)));
    // put all of the key:value pairs from the profile into a 'user' object
    user = { ...profile };
    User.findOrCreate({ stravaId: profile.id }, (err, user) => {
      return cb(err, user);
   });
}));

// setup the server
const app = express();

// cors will alow us to make requests to Strava
app.use(cors());
app.use(passport.initialize());

// call from front-end to login user with Strava. Will call everything up above
app.get("/auth/strava", passport.authenticate("strava", { failureRedirect: '/login' }, { failWithError: true }));

// define callback
app.get("/auth/strava/callback", 
  passport.authenticate("strava", { failureRedirect: '/login' }, { failWithError: true }),
    (req, res) => {
      res.redirect("/profile");
});

app.get("/user", (req, res) => {
  console.log("getting user data");
  res.send(user);
});

// logout a user
app.get("/auth/logout", (req, res) => {
  console.log("logging out");
  user = {};
  res.redirect("/");
});

const PORT = 5000;
app.listen(PORT);

通过身份验证后,应将用户重定向回应用程序中的个人资料页面,但我在浏览器中收到以下错误:

Error occured while trying to proxy to: 127.0.0.1:3000/auth/strava/callback?state=&code=ca0fb3acc0cc0908ca722e229f403d9884571a2c&scope=

这是我正在运行节点服务器的终端上显示的内容:

/home/masonmontero/Desktop/RunningWhileDating/mile37/node_modules/passport/lib/middleware/authenticate.js:353
[1]           return callback(err);
[1]                  ^
[1]
[1] TypeError: callback is not a function
[1]     at Strategy.strategy.error (/home/masonmontero/Desktop/RunningWhileDating/mile37/node_modules/passport/lib/middleware/authenticate.js:353:18)
[1]     at /home/masonmontero/Desktop/RunningWhileDating/mile37/node_modules/passport-oauth/lib/passport-oauth/strategies/oauth2.js:125:32
[1]     at /home/masonmontero/Desktop/RunningWhileDating/mile37/node_modules/oauth/lib/oauth2.js:191:18
[1]     at passBackControl (/home/masonmontero/Desktop/RunningWhileDating/mile37/node_modules/oauth/lib/oauth2.js:132:9)
[1]     at IncomingMessage.<anonymous> (/home/masonmontero/Desktop/RunningWhileDating/mile37/node_modules/oauth/lib/oauth2.js:157:7)
[1]     at IncomingMessage.emit (events.js:203:15)
[1]     at endReadableNT (_stream_readable.js:1145:12)
[1]     at process._tickCallback (internal/process/next_tick.js:63:19)

0 个答案:

没有答案