skip to Main Content

I’m trying to set the persistence to ‘Session’ using typescript/react, but getting type errors.

Here is some of code:

import { auth } from "../auth/firebase";
import {
  createUserWithEmailAndPassword,
  sendPasswordResetEmail,
  setPersistence,
  signInWithCustomToken,
  signInWithEmailAndPassword,
  updateProfile,
} from "firebase/auth";

This is my useEffect:

  useEffect(() => {
    console.log("here useEffect");
  
    auth.setPersistence(auth.Auth.SESSION.NONE); //Property 'Auth' does not exist on type 'Auth'.

    const unsubscribe = auth.onAuthStateChanged((user: any) => //this works fine

I tried a different solution I found here, that looked like this:

setPersistence(auth, {type: 'LOCAL'});

This didn’t work either.

2

Answers


  1. See the documentation for an example.

    import { getAuth, setPersistence, browserSessionPersistence } from "firebase/auth";
    
    const auth = getAuth();
    setPersistence(auth, browserSessionPersistence);
    // it returns a promise...
    

    Also see the API documentation for setPersistence.

    Login or Signup to reply.
  2. This table expands on the documentation Supported types of Auth state persistence. The TypeScript constants (TS const) are new values used with the modular API (in lieu of the old enum values used in the namespaced API).

    Enum Persistence (TS const) Description
    firebase.auth.Auth.Persistence.LOCAL browserLocalPersistence State will be persisted even when the browser window is closed. An explicit sign out is needed to clear that state.
    firebase.auth.Auth.Persistence.SESSION browserSessionPersistence State will only persist in the current session or tab, and will be cleared when the tab or window in which the user authenticated is closed. Applies only to web apps.
    firebase.auth.Auth.Persistence.NONE inMemoryPersistence State will be stored in memory and will be cleared when the window or activity is refreshed.

    Example using Web modular API

    import { getAuth, setPersistence, inMemoryPersistence } from "firebase/auth";
    
    const auth = getAuth();
    setPersistence(auth, inMemoryPersistence);
    

    Same example using Web namespaced API

    import firebase from "firebase/compat/app"
    import "firebase/compat/auth"
    
    const firebaseConfig = {
      // ...
    }
    
    firebase.initializeApp(firebaseConfig)
    const auth = firebase.auth()
    
    auth.setPersistence(firebase.auth.Auth.Persistence.NONE)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search