J'ai une page de connexion, une page de tableau de bord et quelques autres. J'utilise Firebase comme backend. J'ai effectué un rendu conditionnel sur le composant de connexion pour vérifier si l'utilisateur est connecté ou non. (Si l'utilisateur est connecté, il est redirigé vers le tableau de bord). J'ai utilisé ProtectedRoutes pour le tableau de bord et d'autres pour rediriger vers la page de connexion si l'utilisateur n'est pas connecté.
ce qui se passe, c'est que lorsque je recharge la page du tableau de bord ou toute autre page alors que la connexion est vraie, je vois brièvement le composant de connexion puis il exécute le firebase " auth.onAuthStateChanged La méthode " " définit l'état de connexion à true et me redirige vers le tableau de bord. Je comprends pourquoi je vois la page de connexion, mais je ne sais pas comment y remédier.
import React, { useState } from 'react'
import { connect } from "react-redux"
import { signin, autoSignin } from "../Redux/Actions/AuthActions"
import { Redirect } from 'react-router-dom'
const Login = (props) => {
const [form, setForm] = useState({})
const [disable, setDisable] = useState(false)
const handleChange = (e) => {
setForm({
...form,
[e.target.name]: e.target.value
})
}
const handleSubmit = (e) => {
setDisable(true)
e.preventDefault()
props.signin(form.email, form.password)
setDisable(false)
}
return (
props.auth.loggedin ?
<Redirect to="/dashboard" /> :
<div className="container-fluid login_screen">
<div className="row justify-content-center">
<div className="col-md-4 col-xs-12">
<img className="img-fluid login_screen_logo mt-4" src="/images/logo.png" alt="logo" />
<h1 className="mt-4 fs-3 heading">LOGIN</h1>
<p className="fs-6 mb-3">to access dashboard</p>
<form onSubmit={handleSubmit}>
<div className="form-floating mb-4 mt-4">
<input name="email" onChange={handleChange} required type="email" className="form-control shadow-sm" id="floatingInput" placeholder="room@adil.tower" />
<label htmlFor="floatingInput">Email id</label>
</div>
<div className="form-floating mt-4">
<input name="password" onChange={handleChange} required type="password" className="form-control shadow-sm" id="floatingPassword" placeholder="Password" />
<label htmlFor="floatingPassword">Password</label>
</div>
<div className="d-grid gap-2">
<button type="submit" className={`btn btn-custom mt-4 btn-lg shadow ${disable ? "disabled" : ""}`}>LOGIN</button>
</div>
</form>
</div>
</div>
</div>
)
}
const mapStateToProps = (state) => {
return {
auth: state.auth
}
}
const mapDispatchToProps = (dispatch) => {
return {
signin: (email, pass) => dispatch(signin(email, pass)),
autoSignin: (uid) => dispatch(autoSignin(uid))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Login)
import React from 'react'
const Dashboard = (props) => {
return (
<h1>
dashboard
</h1>
)
}
export default Dashboard
import React from 'react'
import { Redirect, Route } from "react-router-dom"
const ProtectedRoute = ({ loggedin, component: Component, ...rest }) => {
return (
<Route
{...rest}
render={
(props) => {
if (loggedin) {
return <Component {...props} />
} else {
return <Redirect to="/" />
}
}
}
/>
)
}
export default ProtectedRoute
import React, { useEffect } from 'react'
import { BrowserRouter as Router, Switch, Route } from "react-router-dom"
import ProtectedRoute from './ProtectedRoute'
import { auth } from "./FirebaseConfig"
// SCREENS
import Dashboard from './Dashboard/Dashboard'
import Login from "./Login/Login"
import Profile from './Profile/Profile'
import Emergency from './Emergency/Emergency'
import Notice from './Notice/Notice'
import Complains from "./Complains/Complains"
import Meetings from "./Meetings/Meetings"
// REDUX STORE & ACTIONS
import { connect } from "react-redux"
import { autoSignin } from "./Redux/Actions/AuthActions"
const App = (props) => {
useEffect(
() => {
auth.onAuthStateChanged(user => {
if (user) {
props.autoSignin(user.uid)
}
})
}, []
)
return (
<Router>
<Switch>
<ProtectedRoute loggedin={props.auth.loggedin} exact path="/dashboard" component={Dashboard}></ProtectedRoute>
<Route exact path="/"><Login /></Route>
<ProtectedRoute loggedin={props.auth.loggedin} exact path="/profile" component={Profile}></ProtectedRoute>
<ProtectedRoute loggedin={props.auth.loggedin} exact path="/emergency" component={Emergency}></ProtectedRoute>
<ProtectedRoute loggedin={props.auth.loggedin} exact path="/notices" component={Notice}></ProtectedRoute>
<ProtectedRoute loggedin={props.auth.loggedin} exact path="/complains" component={Complains}><Complains /> </ProtectedRoute>
<ProtectedRoute loggedin={props.auth.loggedin} exact path="/meetings" component={Meetings} ></ProtectedRoute>
</Switch>
</Router>
)
}
const mapStateToProps = (state) => {
return {
auth: state.auth
}
}
const mapDispatchToProps = (dispatch) => {
return {
autoSignin: (uid) => dispatch(autoSignin(uid))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App)