I'm new to React and Firebase, and I've been trying to solve this small User Sign-Up problem all day . Hopefully someone can help me out, I'd be so grateful
I am making a twitter-like blog site with react and Firebase, and am currently using Firebase's auth.createUserWithEmailAndPassword() method to register a new user, with a username. This is how I'm doing it:
import { auth } from '../firebase';
const onSubmit = async (data) => {
auth
.createUserWithEmailAndPassword(data.email, data.password)
.then((result) => {
result.user.updateProfile({
displayName: data.username,
});
setUserError(!userError);
window.localStorage.setItem('emailForSignIn', data.email);
setRedirect(true);
})
.catch((error) => {
setUserError(!userError);
setAlertMsg(error.message);
});
};
After the user submits the form, they are redirected to the main page, which I then set the page to their current username. I'm setting the username by setting a state for it, and then passing it as a prop to all my pages.
const Main = ({SignOut}) => {
const [userName, setUserName] = useState('')
useEffect(() => {
const getUserName = () => {
auth.onAuthStateChanged((user) => {
setUserName(user.displayName)
console.log('Current userName is: ', user.displayName);
});
}
getUserName()
}, [])
return (
<Router>
<Switch>
<Route exact path="/">
<Navbar userName={userName} SignOut={SignOut} />
<Tweets userName={userName} />
</Route>
<Route path="/profile">
<Navbar userName={userName} SignOut={SignOut} />
<Profile setUserName={setUserName} userName={userName} />
</Route>
</Switch>
</Router>
)
}
Almost everything works perfectly, however their username only shows up if they log out and log back in. Any help would be awesome!
EDIT
Here is my App Component. I moved the useEffect event here, however, I am still experiencing the same thing.
function App() { const [user] = useAuthState(auth); const [userName, setUserName] = useState(''); useEffect(() => { const getUserName = () => { auth.onAuthStateChanged((user) => { setUserName(user.displayName); console.log('Current userName is: ', user.displayName); }); }; getUserName(); }, []); return ( <> {user? ( <Main userName={userName} setUserName={setUserName} SignOut={SignOut} /> ): ( <Register /> )} </> ); }
This was a super noob bug that I was experiencing, but instead of using useEffect to set the username, I just passed my setUserName state to my sign up component, and did away with useEffect entirely. This fixed everything.
const onSubmit = async (data) => { auth.createUserWithEmailAndPassword(data.email, data.password).then((result) => { result.user.updateProfile({ displayName: data.username, }); // This is the state i was missing. setUserName(data.username); setUserError(;userError). window.localStorage,setItem('emailForSignIn'. data;email); setRedirect(true); })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.