javascript - 如何在 Firebase 中添加 SignOut 选项
问题描述
首先,很抱歉,如果已经问过这个问题,但我搜索了很多,但找不到任何东西。基本上,我有这段代码可以让我登录但我无法注销,我搜索并尝试了不同的代码但它没有用。谁能帮忙。
initApp = function() {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var uid = user.uid;
var phoneNumber = user.phoneNumber;
var providerData = user.providerData;
document.getElementById("name-container").innerHTML=displayName;
} else {
// User is signed out.
}
}, function(error) {
console.log(error);
});
};
window.addEventListener('load', function() {
initApp()
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<style type="text/css">
@import url('https://fonts.googleapis.com/css?family=Dosis');
body{
margin: 0;
font-family: 'Dosis', sans-serif;
}
#header{
width: 100%;
height: 100px;
padding-top: 50px;
text-align: center;
color: black;
font-size: 34px;
box-shadow: 10px 10px 5px #aaaaaa;
}
h2{
margin: 0;
}
.container{
width: 100%;
text-align: center;
margin-top: 90px;
}
.first{
display: table;
margin: 0 auto;
text-align: center;
padding: 20px;
}
a{
text-decoration: none;
font-size: 30px;
color: red;
}
a:hover{
color: black;
}
p{
text-decoration: none;
font-size: 30px;
color: red;
}
p:hover{
color: black;
cursor: pointer;
}
footer{
text-align: center;
border-top: 1px solid black;
padding: 20px;
position: fixed;
bottom: 0;
left: 0
right:0;
width: 100%;
}
</style>
<script src="https://www.gstatic.com/firebasejs/5.5.7/firebase.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body>
<div id="header">
<h2>Welcome <span id="name-container"></span></h2>
</div>
<div class="container">
<div class="first">
<a href="">Account</a>
</div>
<div class="first">
<a href="">My Files</a>
</div>
<div class="first">
<p class="first">LogOut</p>
</div>
</div>
<footer>
Smarter © 2018
</footer>
</body>
</html>
我只想知道如何实现注销选项。
解决方案
要让用户退出 Firebase 身份验证,请调用firebase.auth().signOut()
。请参阅此页面底部的更长示例:https ://firebase.google.com/docs/auth/web/password-auth 。
请注意,这不会让用户退出 Google 身份验证。如果您也想这样做(通常不会),请参阅https://developers.google.com/identity/sign-in/web/sign-in#sign_out_a_user
推荐阅读
- php - 无法移动上传的文件
- database - 工作流数据库结构
- node.js - 如何在没有 dotenv 的情况下运行项目?
- javascript - 如何组合不同对象中的数组对象
- python - 努力理解 Python 输出(Unicode、UTF-8、不同的 Python 版本)
- javascript - 如何在 Eclipse 中从另一个 JavaScript 文件导入 JavaScript 方法
- php - Symfony 5 Form 类实体管理器
- excel - Excel的谷歌翻译功能不起作用
- php - 所有 Drupal 管理页面返回状态 404
- excel - VBA:打开多个excel文件时引用一个excel文件