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

FR: User.fromJSON

Open marcusx2 opened this issue 2 years ago • 2 comments

Operating System

macOS

Browser Version

Safari 16.5.2

Firebase SDK Version

10.1.0

Firebase SDK Product:

Auth

Describe your project's tooling

index.html with source tag.

Describe the problem

I would like the users to a have a fromJSON static method, similar to all the classes that inherit AuthCredential.

Currently, there is no way to JSON.stringify a user and turn it back to a normal user.

Steps and code to reproduce issue

There are no steps.

marcusx2 avatar Sep 05 '23 15:09 marcusx2

I was able to achieve this like so:

onAuthStateChanged(auth,
			async user => {
				if (user) {
					console.log("== USER ==");
					console.log(user);
					let user2 = user.toJSON();
					user2 = JSON.parse(JSON.stringify(user2))
					user2 = user.constructor._fromJSON(auth, user2);

					console.log(user2);
				} else {
					console.log("user signed out and/or deleted");
				}
			}
		);

It is possible to make a user from a stringified version by accessing the hidden fromJSON method of the user constructor. So I'd need to store this constructor to use it later.

Can the fromJSON just be exposed instead? Any particular reason it's not? We have the toJSON method to turn it into a JSON, but not the way back. I need to be able to go back for my use case. Like I said, I found this workaround but it'd be nice if fromJSON was simply exposed.

marcusx2 avatar Sep 05 '23 16:09 marcusx2

I was able to achieve this like so:

onAuthStateChanged(auth,
			async user => {
				if (user) {
					console.log("== USER ==");
					console.log(user);
					let user2 = user.toJSON();
					user2 = JSON.parse(JSON.stringify(user2))
					user2 = user.constructor._fromJSON(auth, user2);

					console.log(user2);
				} else {
					console.log("user signed out and/or deleted");
				}
			}
		);

It is possible to make a user from a stringified version by accessing the hidden fromJSON method of the user constructor. So I'd need to store this constructor to use it later.

Can the fromJSON just be exposed instead? Any particular reason it's not? We have the toJSON method to turn it into a JSON, but not the way back. I need to be able to go back for my use case. Like I said, I found this workaround but it'd be nice if fromJSON was simply exposed.

I've been pulling my hair out trying to figure out how to create a new User from a stringified User object in order to pass it to the function 'updateCurrentUser'. I am working on building a chrome extension using manifest V3 that has strict security policies. The only way to do a federated login is through service-worker and an offscreen page. Once logged in, the user creds are passed to the main context through a message where the Auth needs to be updated using 'updateCurrentUser'. Some other threads have also mentioned this problem but there is no solution offered any where. This workaround is the only viable solution that I have seen. So, thank you for posting it.

@marcusx2 By any chance, did you figure out a better way to create a new User object from JSON?

bashimr avatar Sep 09 '24 13:09 bashimr