функция очистки useEffect не запускается при выходе из системы с помощью (firebase), и я получаю сообщение об ошибке Uncaught Ошибка в onSnapshot: FirebaseError: отсутствует или недостаточно разрешений. после перенаправления на знак страница входа / регистрации.
Я понимаю, что функция очистки запускается, если компонент отключается, но в моем случае компонент приложения не отключается (и не должен), поэтому слушатель не отключается
function App() {
const [currentUser, setCurrentUser] = useState('')
useEffect(() => {
let unsubscribeFromAuth = null;
unsubscribeFromAuth = auth.onAuthStateChanged(async userAuth => {
if(userAuth) {
const userRef = await createUserProfileDocument(userAuth)
userRef.onSnapshot(snapshot => {
setCurrentUser({
id: snapshot.id,
...snapshot.data()
})
})
}
setCurrentUser(userAuth)
})
// the cleaning function is not being triggered because the App component is not unmounting (it shouldn't unmount anyways)
return () => unsubscribeFromAuth()
},[])
return (
<div className="App">
<Switch>
<Route exact path="/">
{
currentUser
? <Redirect from="/" to="/userpage" />
: <SignInAndSignUpPage currentUser={currentUser} />
}
</Route>
<Route path='/userpage'>
{
!currentUser
? <Redirect from="/userpage" to="/" />
: <UserPage user={currentUser} />
}
</Route>
</Switch>
</div>
);
}
мои правила безопасности firebase:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
моя кнопка выхода, расположенная на странице пользователя:
<button
onClick={() => {
auth.signOut();
}}
>
SIGN OUT
</button>