【问题标题】:FIrebase Google Auth Logout Error Connection refusedFIrebase Google Auth 注销错误连接被拒绝
【发布时间】:2021-10-05 21:23:52
【问题描述】:

我正在尝试将 google auth 应用到我的 firebase 项目中。我可以成功登录应用程序,但问题发生在注销时。

我收到错误 GET http://localhost:4000/auth/logout net::ERR_CONNECTION_REFUSED

这似乎是我在代码中定义的注销路线的问题

export default function Dashboard() {
  const [error, setError] = useState("");
  const history = useHistory();

  async function handleLogout() {
    setError("");
    axios({
      method: "GET",
      withCredentials: true,
      url: "/auth/logout",
  
    })
      .then((res) => {
        Auth.deauthenticateUser();
        history.push("/login");

      })
      .catch((err) => {
        console.log(err.response.data.message);
      });
  }

  return (
    <div>
      {error && <p>{error}</p>}
      <h2>Home</h2>
      <p>Signed In</p>
      <button variant="link" onClick={handleLogout}>
        Log Out
      </button>
    </div>
  );
}

我认为我的注销路径存在问题,我们将不胜感激。

登录代码

  function onGoogleSubmit(e) {
    e.preventDefault();
    var provider = new firebase.auth.GoogleAuthProvider();
    provider.setCustomParameters({
      prompt: 'select_account'

    })
    firebase
      .auth()
      .signInWithPopup(provider)
      .then((result) => {
        setError("");
        console.log(result.user);
        Auth.authenticateUser();
        history.push("/");
      })
      .catch((err) => {
        setError(err.message);
      });
  }

  return (
    <div className="login-container">
      <div className="login-shadow-box">
       
      
   
          <button onClick={onGoogleSubmit}>Google</button>
        </div>
      </div>
    </div>
  );

【问题讨论】:

  • 您是否尝试从 Firebase 身份验证中退出?还有什么在 http://localhost:4000 上运行? /auth/logout 是你的 React App 还是你的服务器上的路由?
  • 是的,我正在尝试从 Firebase 身份验证中注销。该 url 是导致从 firebase 注销路由的原因
  • 为什么不使用signOut 方法?如果那是一个页面,你能分享/auth/logout页面的代码吗?
  • 这是用于将其放入注销路由中的注销代码,以便更轻松地从多个登录选项注销。 router.get("/logout", (req, res) =&gt; { firebase .auth() .signOut() .then(() =&gt; { return res.status(200).json({ message: "Signout successful" }); }) .catch((error) =&gt; { return res.status(403).json({ message: error.message }); });
  • 这是一个快递应用吗?你不能在你的服务器上使用signOut 方法。服务器上没有用户登录。那应该在前端本身使用。请参考我的回答。

标签: reactjs firebase firebase-authentication google-authentication


【解决方案1】:
axios({
  method: "GET",
  withCredentials: true,
  url: "/auth/logout",
})

Axios 将在https://domain.tld/auth/logout 发出 GET 请求。我不确定那是不是服务器。但是您可以简单地使用signOut 方法注销。

async function handleLogout() {
  setError("");
  firebase.auth().signOut()
    .then((res) => {
      Auth.deauthenticateUser();
      history.push("/login");
    })
    .catch((err) => {
      console.log(err.response.data.message);
    });
}

【讨论】:

    猜你喜欢
    • 2015-08-25
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2016-12-09
    • 2018-04-21
    相关资源
    最近更新 更多