Firebase anonymous auth data has reset, when using safari in iOS and reload immediately in Nuxt.js

Viewed 118

I made a simple code like following in Nuxt.js.

<template>
  <div>
    uid:{{ user.uid }}
  </div>
</template>

<script>
import firebase from "firebase";

export default {
  data() {
    return {
      user: {}
    };
  },

  mounted: function() {
    this.initFirebase();
  },

  methods: {
    initFirebase: function() {
      if (!firebase.apps.length) {
        firebase.initializeApp({
          apiKey: process.env.API_KEY,
          authDomain: process.env.AUTH_DOMAIN,
          databaseURL: process.env.DATABASE_URL,
          projectId: process.env.PROJECT_ID,
          storageBucket: process.env.STORAGE_BUCKET,
          messagingSenderId: process.env.MESSAGING_SENDER_ID
        });
      }
      firebase.auth().onAuthStateChanged((user) => {
        if (user) {
          this.user = user
          console.log("authed");
        } else {
          console.log("authing");
          firebase.auth().signInAnonymously()
        }
      });
    },
  }
};
</script>

I expect this code will auth only one time, and UID will never change in later visit.
It works almost perfect, but combination in iOS and Safari, the UID will change when I visit this page and reload immediately after UID displayed.
I cleared the safari's cache and tried it several times, and notice that this doesn't happen if I wait 10 or 20 seconds. And if UID once changed, it will never change.
I never seen this in Android or any other browser in iOS like chrome for example.
I want these to behave same.

Please let me know if you have any good ideas.

0 Answers
Related