I’m new to Reactjs & Firebase, but I’ve managed to set up a signup/login system- now once the user logs in, I have them redirected back to the home page, but I want the nav bar to display ‘profile’ instead of the original ‘log in’. I know this is something to do with Conditional Rendering but I’m having trouble implementing it with Firebase.
NavBar.js
JavaScript
x
24
24
1
import { Link } from 'react-router-dom'
2
3
// import { AuthProvider, useAuth } from '../contexts/AuthContext'
4
// import firebase from 'firebase/compat/app';
5
6
const NavBar = () => {
7
8
return (
9
<header>
10
<Link to = '/'>
11
<img src = {require('../images/logo.png')} alt = 'logo'/>
12
</Link>
13
<div id = 'nav-pages'>
14
<Link to='/contact'>contact</Link>
15
<Link to='/about'>about</Link>
16
{/* change this to a link to a profile page */}
17
<Link to='/login'>log-in</Link>
18
</div>
19
</header>
20
)
21
}
22
23
export default NavBar
24
LogIn.js
JavaScript
1
68
68
1
import React, { useRef, useState } from 'react'
2
import NavBar from '../components/NavBar'
3
import Footer from '../components/Footer'
4
5
import { Link, useNavigate } from 'react-router-dom';
6
import { AuthProvider, useAuth } from '../contexts/AuthContext'
7
8
// change to login css
9
import '../css/SignUp.css'
10
11
export default function Login(){
12
13
const emailRef = useRef();
14
const passwordRef = useRef();
15
16
const { login } = useAuth();
17
const [error, setError] = useState("");
18
const [loading, setLoading] = useState(false);
19
20
const navigate = useNavigate();
21
22
async function handleSubmit(e){
23
e.preventDefault();
24
25
try {
26
setError("");
27
setLoading(true);
28
await login(emailRef.current.value, passwordRef.current.value);
29
navigate('/', {replace: true});
30
} catch {
31
setError('failed to sign in');
32
}
33
setLoading(false);
34
}
35
36
return (
37
<>
38
<NavBar/>
39
40
<div id = 'signup'>
41
<div id = 'card'>
42
<h2>log in</h2>
43
{error && <p id = 'error'>{error}</p>}
44
45
<form onSubmit={handleSubmit} id='form'>
46
<form id='email'>
47
<p>email</p>
48
<input type='email' ref = {emailRef} required/>
49
</form>
50
51
<form id='password'>
52
<p>password</p>
53
<input type='password' ref = {passwordRef} required/>
54
</form>
55
56
<button type='submit' disabled={loading}>log in</button>
57
</form>
58
</div>
59
60
<p>need an account? <Link to='/signup'>Sign Up</Link></p>
61
62
</div>
63
64
<Footer/>
65
</>
66
)
67
}
68
AuthContext.js
JavaScript
1
45
45
1
import React, { useContext, useState, useEffect } from 'react'
2
import { auth } from '../firebase';
3
4
5
const AuthContext = React.createContext()
6
7
export function useAuth(){
8
return useContext(AuthContext)
9
}
10
11
export function AuthProvider({ children }){
12
13
const [currentUser, setCurrentUser] = useState();
14
const [loading, setLoading] = useState(true);
15
16
function signup(email, password){
17
return auth.createUserWithEmailAndPassword(email, password)
18
}
19
20
function login(email, password){
21
return auth.signInWithEmailAndPassword(email,password)
22
}
23
24
useEffect(() => {
25
const unsubscribe = auth.onAuthStateChanged(user => {
26
setCurrentUser(user)
27
setLoading(false)
28
})
29
30
return unsubscribe
31
}, [])
32
33
const value = {
34
currentUser,
35
login,
36
signup,
37
}
38
39
return (
40
<AuthContext.Provider value={value}>
41
{!loading && children}
42
</AuthContext.Provider>
43
);
44
}
45
Advertisement
Answer
I think you should be able to do this:
JavaScript
1
25
25
1
import { Link } from 'react-router-dom'
2
import { useAuth } from '../contexts/AuthContext'
3
// import firebase from 'firebase/compat/app';
4
5
const NavBar = () => {
6
7
let user = useAuth().currentUser;
8
9
return (
10
<header>
11
<Link to = '/'>
12
<img src = {require('../images/logo.png')} alt = 'logo'/>
13
</Link>
14
<div id = 'nav-pages'>
15
<Link to='/contact'>contact</Link>
16
<Link to='/about'>about</Link>
17
{user && <Link to="/profile" >Profile</Link>}
18
{!user && <Link to='/login'>log-in</Link>}
19
</div>
20
</header>
21
)
22
}
23
24
export default NavBar
25
So the line with useAuth().currentUser
will get the user from the context / provider.
And the line with { user && <xxx /> }
will be rendered when there is a user.
And the line with { !user && <xxx /> }
will be rendered when there is no user.