auth.currentuser显示为null
当用户被登录时,我正在尝试显示注销按钮。 这是我正在使用的Navbar。
import React,{useEffect, useState} from 'react'
import { Link, useNavigate } from 'react-router-dom';
import { auth } from '../firebase';
{auth.currentUser ?
<>
<Link className="btn btn-primary" to="/profile">Profile</Link>
<Link className="btn btn-primary">Logout</Link>
</>
:
<>
<Link className="btn btn-primary mx-2" to="/register">Register</Link>
<Link className="btn btn-primary" to="/login">Login</Link>
</>
}
这是firebase配置
import { initializeApp } from "firebase/app";
import {getAuth} from 'firebase/auth'; // getting auhtentication from firebase
import {getFirestore } from 'firebase/firestore'
const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_DATABASE_URL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_ID,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_ID
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app); // now we will export this to use for authentication
const db = getFirestore(app);
export {auth, db};
I am trying to show logout button when the user is logged in for that i am using auth.currentUser to check if the user is authenticated or not but each and everytime auth.currrentUser is showing null value.
Here is the Navbar where i am using it.
import React,{useEffect, useState} from 'react'
import { Link, useNavigate } from 'react-router-dom';
import { auth } from '../firebase';
{auth.currentUser ?
<>
<Link className="btn btn-primary" to="/profile">Profile</Link>
<Link className="btn btn-primary">Logout</Link>
</>
:
<>
<Link className="btn btn-primary mx-2" to="/register">Register</Link>
<Link className="btn btn-primary" to="/login">Login</Link>
</>
}
This is the firebase config from where auth is comming
import { initializeApp } from "firebase/app";
import {getAuth} from 'firebase/auth'; // getting auhtentication from firebase
import {getFirestore } from 'firebase/firestore'
const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_DATABASE_URL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_ID,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_ID
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app); // now we will export this to use for authentication
const db = getFirestore(app);
export {auth, db};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Firebase身份验证会自动将用户凭据存储在大多数浏览器中,并在页面/应用重新加载时还原它们。但这要求它拨打服务器(例如,检查帐户是否被禁用),这需要时间。当您的代码检查
CurrentUser
时,对服务器的调用尚未完成时,因此Currentuser
isnull
。为了解决此问题,您需要使用所谓的AUTH状态侦听器获取通知并响应身份验证状态的任何更改。作为获得当前的uner 显示:
Firebase Authentication automatically stores the user credentials in most browsers and restores them when the page/app reloads. But this requires that it makes a call to the server (to check for example if the account was disabled), which takes time. By the time your code checks
currentUser
the call to the server hasn't completed yet, and thuscurrentUser
isnull
.To fix this, you'll want to use a so-called auth state listener to get notified and respond to any changes in authentication state. As the first snippet in the documentation on getting the current user shows: