Skip to content

Auth examples #47

@ryanmclaughlin

Description

@ryanmclaughlin

Hi there,

I'm having trouble getting auth to work. I have successfully implemented the ability to create and login to an account, but I'm having trouble getting the currentUser with the function onAuthStateChanged.

Does anyone have any Vue 2.0, vue-router, and Firebase 3+ examples of auth successfully implemented that would steer me in the right direction?

/views/Auth.vue

<script>
  import firebase from '../services/firebase.js'

  export default {
    data() {
      return {
        email: '',
        password: '',
        confirmPassword: '',
        wantsToSignUp: false,
        errorMessage: '',
      };
    },
    methods: {
      signUpWithPassword() {
        if (this.password === this.confirmPassword) {
          firebase.auth.createUserWithEmailAndPassword(this.email, this.password)
          .then(() => this.signInWithPassword())
          .catch((error) => { this.errorMessage = error.message });
        }
      },
      signInWithPassword() {
        return firebase.auth.signInWithEmailAndPassword(this.email, this.password)
        .then((userData) => {
          this.onSignedIn();
          return userData;
        })
        .catch((error) => { this.errorMessage = error.message });
      },
      onSignedIn() {
        this.$router.go('/');
      },
    },
  };
</script>

AppNav always reports unknown user.title after I am logged in.

/components/AppNav.vue

<script>
  import firebase from '../services/firebase.js'

  export default {
    data() {
      return {
        user: null,
        searchQuery: '',
      };
    },
    methods: {
      processUser(authed) {
        console.log(authed);
        if (authed === null) {
          this.user = null;
          return;
        }
        this.user = {
          title: authed.email || '',
        };
      },
      signOut() {
        firebase.auth.signOut();
        this.$router.go('auth');
      },
    },
    ready() {
      firebase.auth.onAuthStateChanged(this.processUser);
      this.processUser(firebase.auth.currentUser);
    },
  };
</script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions