Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6887

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T10:42:08+00:00 2024-11-27T10:42:08+00:00

Register user to firebase

  • 60k

Code for this can be found in the Github Branch

Organise Firebase #

After previously setting up firebase and vuex I want to organise things a bit better and then add login and registration of users. So first I extract the firebase setup to a separate file, this enables me to use it in other parts of the app and then to also import this into main and use it in the app.

firebase.js

import firebase from 'firebase/app'; import 'firebase/auth'; import 'firebase/firestore';  const firebaseConfig = {   apiKey: 'AIzaSyDbYPvxMaECPwjgR06njRfTLFa_skZ9-Qo',   authDomain: 'pantry-fe77c.firebaseapp.com',   databaseURL: 'https://pantry-fe77c-default-rtdb.firebaseio.com',   projectId: 'pantry-fe77c',   storageBucket: 'pantry-fe77c.appspot.com',   messagingSenderId: '235929136377',   appId: '1:235929136377:web:f4687227f50dc7bd76c628',   measurementId: 'G-7J6VBCW3SE', };  firebase.initializeApp(firebaseConfig);  export default firebaseConfig;  
Enter fullscreen mode Exit fullscreen mode

You'll notice that I also added the auth package to the import so that we can use this feature too as well as the firestore so that we can save data later. I like to also setup and user table when registering so that in the future settings on the user can be saved there.

main.js

import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; import store from './store'; import firebase from './firebase.js';  let app;  firebase.authDomain().onAuthStateChanged(() => {   if (!app) {     app = createApp(App);      app.use(firebase);     app.use(store);     app.use(router);      app.mount('#app');   } });  
Enter fullscreen mode Exit fullscreen mode

The main.js file is cleaned up and I import the firebase settings.

Register Action and Mutation #

Let's setup the registration option now and then connect it to a register form. First thing is to add state to the module of the extra user information that we want to save for now.

authIndex.js

import authActions from './authActions.js'; import authMutations from './authMutations'; import authGetters from './authGetters';  export default {   namespaced: true,   state() {     return {       id: 0,       username: '',       email: '',       registrationError: false,       errorMsg: '',     };   },   mutations: authMutations,   actions: authActions,   getters: authGetters, };  
Enter fullscreen mode Exit fullscreen mode

Now for the action that does the asynchronous calls before passing on the data to the mutation that does the work to save the state. This separation of concerns help to keep state valid and consistent.

authActions.js

import firebase from '@/firebase.js';  const userRef = firebase.firestore().collection('/user');  export default {   async register({ commit }, user) {     try {       // Register user using email and password       const registered = await firebase         .auth()         .createUserWithEmailAndPassword(user.email, user.password);       console.log(registered);        // Create userdata       const userData = {         id: registered.user.uid,         username: user.username,         email: user.email,       };        // Save user to DB       const createUser = await userRef.add(userData);       commit('authSuccess', createUser);     } catch (err) {         commit('authFail', err);     }   }, };  
Enter fullscreen mode Exit fullscreen mode

This user is first created using firebase auth so that we can get the user token for future validation. The next step is to then take that user and save it with the extra data. Once done we can send the data to the mutation using commit or if there is an error we again send the error along to the mutation for later showing the error.

authMutations.js

import router from '@/router';  export default {   authSuccess(state, user) {     console.log(state, user);     state.email = user.email;     state.username = user.username;     state.registrationError = false;     state.errorMsg = '';     router.push({ path: 'home' });   },   authFail(state, error) {     state.registrationError = true;     state.errorMsg = error.message;   }, };  
Enter fullscreen mode Exit fullscreen mode

With that we have the registration functionality setup. In the next post I'll create a registration form and hook that up to the vuex store.

firebasejavascriptvuewebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.