I cannot get my google firebase auth to work, I get the following error Cannot find name 'Persistence'. Did you mean 'setPersistence'?ts(2552)
Here is my code. I tried to import it but still gets an error. This is in TypeScript where do I import from? It appears UserCredential and Persistence are both an interface.
import { initializeApp, FirebaseApp, FirebaseError } from 'firebase/app';
import {getAuth, GoogleAuthProvider, Auth, signInWithPopup, UserCredential, setPersistence } from "firebase/auth";
export interface IUser {
displayName: string | null;
email: string | null;
providerId: string;
uid: string;
}
const firebaseConfig = {
apiKey: "apiKey",
authDomain: "authDomain",
databaseURL: "databaseURL",
projectId: "projectId",
storageBucket: "storeageBucket",
messagingSenderId: "messageSenderId",
appId: "appId",
measurementId: "measurmentId"
};
const _getApp = (): FirebaseApp => {
const app = initializeApp(firebaseConfig, 'MyExample');
return app;
}
const _setPeristence = async (): Promise<void> => {
const app = _getApp();
const auth = getAuth(app);
setPersistence(auth, Persistence.LOCAL);
return Promise.resolve();
}
const _signInWithProvider = async (resolve:any): Promise<void> =>{
const currentUser = await GetGoogleUser()
if (currentUser != null) {
return resolve();
}
else {
const provider = new GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
const auth = getAuth(_getApp());
signInWithPopup(auth, provider)
.then((result: UserCredential) => {
}).catch((error: FirebaseError) => {
});
}
}
const SignIn = async (resolve: any): Promise<void> => {
await _setPeristence();
_signInWithProvider(resolve);
return;
}
export {
SignIn,
}
CodePudding user response:
The definition of Persistence
is
interface Persistence {
/**
* Type of Persistence.
* - 'SESSION' is used for temporary persistence such as `sessionStorage`.
* - 'LOCAL' is used for long term persistence such as `localStorage` or `IndexedDB`.
* - 'NONE' is used for in-memory, or no persistence.
*/
readonly type: 'SESSION' | 'LOCAL' | 'NONE';
}
It's not an enum, but an object interface with a type
property that can be either 'SESSION'
, 'LOCAL'
, or 'NONE'
. You can just pass an object literal with the desired type
to setPersistence
:
setPersistence(auth, {type: 'LOCAL'});
CodePudding user response:
Need to add the following to the import
browserLocalPersistence,
browserSessionPersistence,
inMemoryPersistence
like so
import {
getAuth,
GoogleAuthProvider,
Auth,
signInWithPopup,
UserCredential,
setPersistence,
browserLocalPersistence,
browserSessionPersistence,
inMemoryPersistence
} from "firebase/auth";