我正试图实现谷歌认证到我的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>
);
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);
});
}