firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Auth persistence in Electron renderer

Open bumTomica opened this issue 4 years ago • 5 comments

  • Electron 17.1.2
  • Firebase SDK version: 9.6.8
  • Firebase Product: auth

Hello, not sure if I'm doing something wrong, but I'm trying to migrate to SDK 9+, and auth does not persist (out of the box) in the Electron renderer process.

The sign-in process for Google/Apple/Facebook/Twitter via signInWithCredential works and onAuthStateChanged shows the user, but later when the app is reopened (or just flushed from memory) - the auth user / currentUser disappears (is null).

I think the SDK thinks it is a Node env and uses in-memory persistence, but it is not, I'm using it in the renderer process ("in Chrome"). Is there a way to force local or session persistence? I tried setPersistence before signInWithCredential but the result is the same.

Some code if it helps, thanks in advance for any help!


import { initializeApp, FirebaseApp } from "firebase/app";
import { getAuth, Auth } from "firebase/auth";

import {
	setPersistence,
	browserLocalPersistence,
	GoogleAuthProvider,
	FacebookAuthProvider,
	signInWithCredential,
} from "firebase/auth";

this.app = initializeApp(configFirebase);
this.auth = getAuth(this.app);

// ...

setPersistence(
	this.rootStore.firebaseStore.auth,
	browserLocalPersistence
).then(() => {
	signInWithCredential(
		this.rootStore.firebaseStore.auth,
		GoogleAuthProvider.credential(idToken)
	).then(() => {
		console.log("Signed in with Google");
	});
});

//  or...

await signInWithCredential(
	this.rootStore.firebaseStore.auth,
	FacebookAuthProvider.credential(accessToken)
);
	
	
this.rootStore.firebaseStore.auth.onAuthStateChanged((user) => {
	console.log("Firebase auth user", user);
});

bumTomica avatar Mar 13 '22 21:03 bumTomica

Hi @bumTomica, it probably depends on how the app is being built. If you're using the node targets it will always use in-memory, even if the code is itself running in the renderer. If you can build a JS target that uses the web targets, it will work as you expect.

The reason setPersistence doesn't work is that we include shims for the browser-only features like browserLocalPersistence in Node, to enable a more seamless SSR experience.

TL;DR: The fix is likely in your package.json / webpack / rollup / etc configuration

sam-gc avatar Mar 14 '22 16:03 sam-gc

Thanks, @sam-gc for clearing this up. I looked at the webpack build, it uses recommended "electron-renderer" target for the renderer process. Not sure that I can successfully change that because it is needed for ipcRenderer for example to work properly (fs / path errors). So web target is not an option for an easy fix.

Is there any way to force web target only for Firebase or any other best approach to persist auth (besides loading SDK via src links or building separate codebase only for Firebase auth web target logic)?

bumTomica avatar Mar 14 '22 21:03 bumTomica

It persists properly for us in [email protected] with no special effort, despite the numerous things that do not seem to work properly in [email protected]. I wonder what the issue could be.

Nantris avatar Mar 16 '22 00:03 Nantris

Hi @bumTomica, sorry for the delay. So after doing a bit of digging, the only way I can think to resolve this is to set up a webpack alias to force it to use the browser build. I found this post on StackOverflow: https://stackoverflow.com/questions/59022794/how-do-i-convince-webpack-to-use-the-node-imports-over-web-imports-when-using-t

So in this case, for example, adding this to webpack.config.js:

resolve: {
  alias: {
    '@firebase/auth': path.resolve(__dirname, 'node_modules/@firebase/auth/dist/esm2017/index.js')
  }
},

I've verified locally in a test project that at least for me this properly gets webpack to use the browser build when using the electron-renderer target. FWIW we won't be changing the current default behavior since it's correct for the majority of use cases.

sam-gc avatar Mar 21 '22 21:03 sam-gc

@sam-gc Works as proposed, thanks for taking your time to propose a solution!

bumTomica avatar Mar 22 '22 07:03 bumTomica