How to use the same Anonymous user on Firebase?









up vote
0
down vote

favorite












I want a guest user to login, and create only 1 new user on my auth db.



Problem-




  • -On Unity editor- A new anonymous user is created with each launch of the app


  • On Android the guest user stays logged in but when the app is uninstalled- then another new anonymous user is created

My db is just full of anonymous users.



Note- I do use- and check monitoring authentication state



How can I use the same Anonymous user? I was even thinking about login with a custom token, and save it on the user's device- but then I need to create my own uid on the admin side and not sure if it's reliable or even a good solution?



It seems like Firebase doesn't give a proper solution for that. You'd think it's basic. Am I missing something?



Code for unity-



void InitializeFirebase()

Debug.Log("Setting up Firebase Auth");

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);


void AuthStateChanged(object sender, System.EventArgs eventArgs)

if (auth.CurrentUser != user)

bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
if (!signedIn && user != null)

Debug.Log("Signed out " + user.UserId);

user = auth.CurrentUser;
if (signedIn)

Debug.Log("Signed in " + user.UserId);




void OnDestroy()

auth.StateChanged -= AuthStateChanged;
auth = null;

public static void SignAnonymous()

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task =>
user = task.Result;

Debug.LogFormat("User signed in successfully: 0 (1)",
user.DisplayName, user.UserId);
);











share|improve this question























  • If you want help figuring out why your approach isn't working, include the minimal code that reproduces the problem in your question. That drastically increases the chances that somebody can spot what's going wrong.
    – Frank van Puffelen
    Nov 12 at 3:01










  • Thanks @FrankvanPuffelen ! but my question is not just code related. It's known that Firebase creates new anonymous users when the app is uninstalled and installed again (and probably some other times also). I can't trust firebase or prevent it from creating new users for a device. It spams my db. Therefore I want to manage it myself and asked how to use the same anonymous user or what is another way to create a single anonymous user per device. Could custom tokens be an option? I added my code now
    – SHAI
    Nov 12 at 4:33










  • Where do you call SignAnonymous? You should only call this when the user is not signed in yet. Although (if I recall correctly) calling it even when the user is signed in already should not create a new UID, but merely reload the existing one. The only time that I know that might not happen is if the SDK is unable to store the user token locally on the device. Ah, I now see that is precisely your first bullet.
    – Frank van Puffelen
    Nov 12 at 4:39










  • Yes I check if the user is logged in- but on Unity-editor, it'll always return false when re-launching the app, and on Android it'd be false as well after uninstalling the app (and I suspect other times as well) therefore anonymous user is not a good option
    – SHAI
    Nov 12 at 4:50











  • Firebase keeps it's user token on the device. But of course it is wiped from there when the user deletes the app. With your own custom sign-in method, where are you going to keep the user ID/credentials so that they don't get wiped, yet don't require the user to sign in again?
    – Frank van Puffelen
    Nov 12 at 15:10














up vote
0
down vote

favorite












I want a guest user to login, and create only 1 new user on my auth db.



Problem-




  • -On Unity editor- A new anonymous user is created with each launch of the app


  • On Android the guest user stays logged in but when the app is uninstalled- then another new anonymous user is created

My db is just full of anonymous users.



Note- I do use- and check monitoring authentication state



How can I use the same Anonymous user? I was even thinking about login with a custom token, and save it on the user's device- but then I need to create my own uid on the admin side and not sure if it's reliable or even a good solution?



It seems like Firebase doesn't give a proper solution for that. You'd think it's basic. Am I missing something?



Code for unity-



void InitializeFirebase()

Debug.Log("Setting up Firebase Auth");

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);


void AuthStateChanged(object sender, System.EventArgs eventArgs)

if (auth.CurrentUser != user)

bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
if (!signedIn && user != null)

Debug.Log("Signed out " + user.UserId);

user = auth.CurrentUser;
if (signedIn)

Debug.Log("Signed in " + user.UserId);




void OnDestroy()

auth.StateChanged -= AuthStateChanged;
auth = null;

public static void SignAnonymous()

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task =>
user = task.Result;

Debug.LogFormat("User signed in successfully: 0 (1)",
user.DisplayName, user.UserId);
);











share|improve this question























  • If you want help figuring out why your approach isn't working, include the minimal code that reproduces the problem in your question. That drastically increases the chances that somebody can spot what's going wrong.
    – Frank van Puffelen
    Nov 12 at 3:01










  • Thanks @FrankvanPuffelen ! but my question is not just code related. It's known that Firebase creates new anonymous users when the app is uninstalled and installed again (and probably some other times also). I can't trust firebase or prevent it from creating new users for a device. It spams my db. Therefore I want to manage it myself and asked how to use the same anonymous user or what is another way to create a single anonymous user per device. Could custom tokens be an option? I added my code now
    – SHAI
    Nov 12 at 4:33










  • Where do you call SignAnonymous? You should only call this when the user is not signed in yet. Although (if I recall correctly) calling it even when the user is signed in already should not create a new UID, but merely reload the existing one. The only time that I know that might not happen is if the SDK is unable to store the user token locally on the device. Ah, I now see that is precisely your first bullet.
    – Frank van Puffelen
    Nov 12 at 4:39










  • Yes I check if the user is logged in- but on Unity-editor, it'll always return false when re-launching the app, and on Android it'd be false as well after uninstalling the app (and I suspect other times as well) therefore anonymous user is not a good option
    – SHAI
    Nov 12 at 4:50











  • Firebase keeps it's user token on the device. But of course it is wiped from there when the user deletes the app. With your own custom sign-in method, where are you going to keep the user ID/credentials so that they don't get wiped, yet don't require the user to sign in again?
    – Frank van Puffelen
    Nov 12 at 15:10












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want a guest user to login, and create only 1 new user on my auth db.



Problem-




  • -On Unity editor- A new anonymous user is created with each launch of the app


  • On Android the guest user stays logged in but when the app is uninstalled- then another new anonymous user is created

My db is just full of anonymous users.



Note- I do use- and check monitoring authentication state



How can I use the same Anonymous user? I was even thinking about login with a custom token, and save it on the user's device- but then I need to create my own uid on the admin side and not sure if it's reliable or even a good solution?



It seems like Firebase doesn't give a proper solution for that. You'd think it's basic. Am I missing something?



Code for unity-



void InitializeFirebase()

Debug.Log("Setting up Firebase Auth");

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);


void AuthStateChanged(object sender, System.EventArgs eventArgs)

if (auth.CurrentUser != user)

bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
if (!signedIn && user != null)

Debug.Log("Signed out " + user.UserId);

user = auth.CurrentUser;
if (signedIn)

Debug.Log("Signed in " + user.UserId);




void OnDestroy()

auth.StateChanged -= AuthStateChanged;
auth = null;

public static void SignAnonymous()

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task =>
user = task.Result;

Debug.LogFormat("User signed in successfully: 0 (1)",
user.DisplayName, user.UserId);
);











share|improve this question















I want a guest user to login, and create only 1 new user on my auth db.



Problem-




  • -On Unity editor- A new anonymous user is created with each launch of the app


  • On Android the guest user stays logged in but when the app is uninstalled- then another new anonymous user is created

My db is just full of anonymous users.



Note- I do use- and check monitoring authentication state



How can I use the same Anonymous user? I was even thinking about login with a custom token, and save it on the user's device- but then I need to create my own uid on the admin side and not sure if it's reliable or even a good solution?



It seems like Firebase doesn't give a proper solution for that. You'd think it's basic. Am I missing something?



Code for unity-



void InitializeFirebase()

Debug.Log("Setting up Firebase Auth");

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);


void AuthStateChanged(object sender, System.EventArgs eventArgs)

if (auth.CurrentUser != user)

bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
if (!signedIn && user != null)

Debug.Log("Signed out " + user.UserId);

user = auth.CurrentUser;
if (signedIn)

Debug.Log("Signed in " + user.UserId);




void OnDestroy()

auth.StateChanged -= AuthStateChanged;
auth = null;

public static void SignAnonymous()

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task =>
user = task.Result;

Debug.LogFormat("User signed in successfully: 0 (1)",
user.DisplayName, user.UserId);
);








firebase unity3d firebase-authentication google-cloud-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 4:32

























asked Nov 12 at 0:33









SHAI

173112




173112











  • If you want help figuring out why your approach isn't working, include the minimal code that reproduces the problem in your question. That drastically increases the chances that somebody can spot what's going wrong.
    – Frank van Puffelen
    Nov 12 at 3:01










  • Thanks @FrankvanPuffelen ! but my question is not just code related. It's known that Firebase creates new anonymous users when the app is uninstalled and installed again (and probably some other times also). I can't trust firebase or prevent it from creating new users for a device. It spams my db. Therefore I want to manage it myself and asked how to use the same anonymous user or what is another way to create a single anonymous user per device. Could custom tokens be an option? I added my code now
    – SHAI
    Nov 12 at 4:33










  • Where do you call SignAnonymous? You should only call this when the user is not signed in yet. Although (if I recall correctly) calling it even when the user is signed in already should not create a new UID, but merely reload the existing one. The only time that I know that might not happen is if the SDK is unable to store the user token locally on the device. Ah, I now see that is precisely your first bullet.
    – Frank van Puffelen
    Nov 12 at 4:39










  • Yes I check if the user is logged in- but on Unity-editor, it'll always return false when re-launching the app, and on Android it'd be false as well after uninstalling the app (and I suspect other times as well) therefore anonymous user is not a good option
    – SHAI
    Nov 12 at 4:50











  • Firebase keeps it's user token on the device. But of course it is wiped from there when the user deletes the app. With your own custom sign-in method, where are you going to keep the user ID/credentials so that they don't get wiped, yet don't require the user to sign in again?
    – Frank van Puffelen
    Nov 12 at 15:10
















  • If you want help figuring out why your approach isn't working, include the minimal code that reproduces the problem in your question. That drastically increases the chances that somebody can spot what's going wrong.
    – Frank van Puffelen
    Nov 12 at 3:01










  • Thanks @FrankvanPuffelen ! but my question is not just code related. It's known that Firebase creates new anonymous users when the app is uninstalled and installed again (and probably some other times also). I can't trust firebase or prevent it from creating new users for a device. It spams my db. Therefore I want to manage it myself and asked how to use the same anonymous user or what is another way to create a single anonymous user per device. Could custom tokens be an option? I added my code now
    – SHAI
    Nov 12 at 4:33










  • Where do you call SignAnonymous? You should only call this when the user is not signed in yet. Although (if I recall correctly) calling it even when the user is signed in already should not create a new UID, but merely reload the existing one. The only time that I know that might not happen is if the SDK is unable to store the user token locally on the device. Ah, I now see that is precisely your first bullet.
    – Frank van Puffelen
    Nov 12 at 4:39










  • Yes I check if the user is logged in- but on Unity-editor, it'll always return false when re-launching the app, and on Android it'd be false as well after uninstalling the app (and I suspect other times as well) therefore anonymous user is not a good option
    – SHAI
    Nov 12 at 4:50











  • Firebase keeps it's user token on the device. But of course it is wiped from there when the user deletes the app. With your own custom sign-in method, where are you going to keep the user ID/credentials so that they don't get wiped, yet don't require the user to sign in again?
    – Frank van Puffelen
    Nov 12 at 15:10















If you want help figuring out why your approach isn't working, include the minimal code that reproduces the problem in your question. That drastically increases the chances that somebody can spot what's going wrong.
– Frank van Puffelen
Nov 12 at 3:01




If you want help figuring out why your approach isn't working, include the minimal code that reproduces the problem in your question. That drastically increases the chances that somebody can spot what's going wrong.
– Frank van Puffelen
Nov 12 at 3:01












Thanks @FrankvanPuffelen ! but my question is not just code related. It's known that Firebase creates new anonymous users when the app is uninstalled and installed again (and probably some other times also). I can't trust firebase or prevent it from creating new users for a device. It spams my db. Therefore I want to manage it myself and asked how to use the same anonymous user or what is another way to create a single anonymous user per device. Could custom tokens be an option? I added my code now
– SHAI
Nov 12 at 4:33




Thanks @FrankvanPuffelen ! but my question is not just code related. It's known that Firebase creates new anonymous users when the app is uninstalled and installed again (and probably some other times also). I can't trust firebase or prevent it from creating new users for a device. It spams my db. Therefore I want to manage it myself and asked how to use the same anonymous user or what is another way to create a single anonymous user per device. Could custom tokens be an option? I added my code now
– SHAI
Nov 12 at 4:33












Where do you call SignAnonymous? You should only call this when the user is not signed in yet. Although (if I recall correctly) calling it even when the user is signed in already should not create a new UID, but merely reload the existing one. The only time that I know that might not happen is if the SDK is unable to store the user token locally on the device. Ah, I now see that is precisely your first bullet.
– Frank van Puffelen
Nov 12 at 4:39




Where do you call SignAnonymous? You should only call this when the user is not signed in yet. Although (if I recall correctly) calling it even when the user is signed in already should not create a new UID, but merely reload the existing one. The only time that I know that might not happen is if the SDK is unable to store the user token locally on the device. Ah, I now see that is precisely your first bullet.
– Frank van Puffelen
Nov 12 at 4:39












Yes I check if the user is logged in- but on Unity-editor, it'll always return false when re-launching the app, and on Android it'd be false as well after uninstalling the app (and I suspect other times as well) therefore anonymous user is not a good option
– SHAI
Nov 12 at 4:50





Yes I check if the user is logged in- but on Unity-editor, it'll always return false when re-launching the app, and on Android it'd be false as well after uninstalling the app (and I suspect other times as well) therefore anonymous user is not a good option
– SHAI
Nov 12 at 4:50













Firebase keeps it's user token on the device. But of course it is wiped from there when the user deletes the app. With your own custom sign-in method, where are you going to keep the user ID/credentials so that they don't get wiped, yet don't require the user to sign in again?
– Frank van Puffelen
Nov 12 at 15:10




Firebase keeps it's user token on the device. But of course it is wiped from there when the user deletes the app. With your own custom sign-in method, where are you going to keep the user ID/credentials so that they don't get wiped, yet don't require the user to sign in again?
– Frank van Puffelen
Nov 12 at 15:10

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254641%2fhow-to-use-the-same-anonymous-user-on-firebase%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254641%2fhow-to-use-the-same-anonymous-user-on-firebase%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党