当前位置: 首页 > 知识库问答 >
问题:

如何使用超文本传输协议-代理-中间件与反应和passport.js

丁晋
2023-03-14

嗯,我正在开发一个反应应用程序。我有一个快速服务器在localhost:5000和反应应用程序在localhost:3000。我正在通过谷歌oauth流使用Passportjs。现在真正的问题是,我已经使用超文本传输协议-代理-中间件去localhost:5000/auth/google从我的反应应用程序使用登录按钮,它指向 /auth/google.然后认证后,我应该返回到 /auth/google/callback.then从这里我应该被重定向到localhost:3000/surveys,但我被重定向到不存在的localhost:5000/surveys。所以我得到错误:不能得到 /surveys.但是当我用注销按钮注销 /api/logout然后我被重定向到主页在'/'这是在localhost:3000。那么为什么我被重定向到localhost:3000/而不是localhost:5000/。我怎么能去localhost:3000/surveys而不是开始提到的localhost:5000/surveys?

//passport js file
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const mongoose = require("mongoose");
const keys = require("../config/keys");

const User = mongoose.model("users");
passport.serializeUser((user, done) => {
  done(null, user.id);
});
passport.deserializeUser((id, done) => {
  User.findById(id).then((user) => {
    done(null, user);
  });
});
passport.use(
  new GoogleStrategy(
    {
      clientID: keys.googleClientID,
      clientSecret: keys.googleClientSecret,
      callbackURL: "/auth/google/callback",
      proxy: true,
    },
    async (accessToken, refreshToken, profile, done) => {
      const existingUser = await User.findOne({ googleId: profile.id });
      if (existingUser) {
        return done(null, existingUser);
      }
      const user = await new User({ googleId: profile.id }).save();
      done(null, user);
    }
  )
);
//the routes for my express server
const passport = require("passport");

module.exports = (app) => {
  app.get(
    "/auth/google",
    passport.authenticate("google", { scope: ["profile", "email"] })
  );
  app.get(
    "/auth/google/callback",
    passport.authenticate("google"),
    (req, res) => {
      res.redirect("/surveys");
    }
  );
  app.get("/api/current", (req, res) => {
    res.send(req.user);
  });
  app.get("/api/logout", (req, res) => {
    req.logout();
    res.redirect("/");
  });
};
//the proxy setup in react src folder
const { createProxyMiddleware } = require("http-proxy-middleware");

module.exports = function (app) {
  app.use(
    "/auth/google",
    createProxyMiddleware({
      target: "http://localhost:5000",
      changeOrigin: true,
    })
  );
  app.use(
    "/api",
    createProxyMiddleware({
      target: "http://localhost:5000",
      changeOrigin: true,
    })
  );
};
//my react header component
import React from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
class Header extends React.Component {
  renderContent() {
    switch (this.props.auth) {
      case null:
        return;
      case false:
        return (
          <li>
            <a href="/auth/google">Log In</a>
          </li>
        );
      default:
        return (
          <li>
            <a href="/api/logout">Log Out</a>
          </li>
        );
    }
  }
  render() {
    return (
      <nav>
        <div className="nav-wrapper">
          <Link to={this.props.auth ? "/surveys" : "/"} className="brand-logo">
            Emaily
          </Link>
          <ul className="right">{this.renderContent()}</ul>
        </div>
      </nav>
    );
  }
}
const mapStateToProps = (state) => {
  return { auth: state.auth };
};
export default connect(mapStateToProps)(Header);

共有1个答案

上官自明
2023-03-14

好吧,只是从代理中删除变更源就可以了!

 类似资料:
  • 我试图配置一个代理服务器(setupProxy.js)内创建反应应用程序使用HTTP-代理-中间件获得访问天气数据API(api.darksky.net)。 我遵循React文档中的步骤(https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-代理(手动)但我

  • 嗨,我一直在尝试代理解决方案,以避免在myapp中的cors问题,但它似乎不起作用,我重启了我的机器这么多次,这没有什么区别。基本上,myapp使用ftchApi调用另一个域(localhost:8080),这是一个SpringBoot应用程序终结点。我添加了package.json"代理":"http://localhost:8080/",但api仍然返回与localhost:3000调用,我尝

  • 我的反应应用程序是在localhost:3000和节点服务器上运行localhost:5000。当我试图连接到快速API时,路由将localhost:3000/auth/google而不是localhost:5000/auth/google 用户操作。js } setupProxy.js 节点server.js 编辑:反应包。json 我是新手,因此我不知道代理究竟是如何工作的。

  • 我只是有一个关于服务中http请求的结构和处理响应的问题。我正在使用Angular2。alpha46 Typescript(刚刚开始测试-我喜欢它…Ps…。感谢所有一直致力于它并通过github作出贡献的人) 因此,采取以下措施: 登录表单。组成部分ts 从这个组件中,我导入了我的userService,它将容纳我的超文本传输协议请求,以登录用户。 使用者服务ts 我想做的是能够处理http请求之

  • 如何使用React应用程序连接到多个API? 创建快速后端的反应应用程序,使用超文本传输协议-代理-中间件。 演示错误:https://github.com/svgpubs/nodeproxy.git 我正在使用http代理中间件尝试将一个demo-React应用程序连接到两个不同的服务器:一个外部网站和一个内部站点 它适用于外部网站。但是,localhost:3001连接不起作用。 如果我不使用

  • 我正在使用下面的代码登录apiendpoint并从中检索数据,但响应似乎已编码,我无法读取内容。我正在使用request-requestes-0.0.1 当我在控制台上打印相同的响应 有人能告诉我如何解码和读取响应中的数据吗