【发布时间】: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) => { firebase .auth() .signOut() .then(() => { return res.status(200).json({ message: "Signout successful" }); }) .catch((error) => { return res.status(403).json({ message: error.message }); }); -
这是一个快递应用吗?你不能在你的服务器上使用
signOut方法。服务器上没有用户登录。那应该在前端本身使用。请参考我的回答。
标签: reactjs firebase firebase-authentication google-authentication