Why does my store reset my authorization?

60 Views Asked by At

I guess that my store from vuejs is resetting my authorization. I also get the error message Cannot read property 'home' of undefined in the header component. I don't know why this is not working. I tried debugging.

This is what i have: In my nuxt.config.js i set plugins to firebase:

 plugins: [
    '~/plugins/firebase.js'
  ],

Then here is my firebase.js file:

import firebase from 'firebase/app'
import 'firebase/auth'

var firebaseConfig = {
    apiKey: "MYAPIKEY",
    authDomain: "AUTHDOMAIN",
    databaseURL: "DBURL",
    projectId: "PROJECTID",
    storageBucket: "STORAGEBUCKET",
    messagingSenderId: "MESSAGINGSENDERID",
    appId: "APPID",
    measurementId: "MEASUREMENTID"
};
!firebase.apps.length ? firebase.initializeApp(firebaseConfig) : ''

export const auth = firebase.auth()
export default firebase

I don't know if it has anything to do with my error but for purpose i just put it in. Then for my registration page i registrate users with their gmail account i have a function in methods like this:

async registersocial() {
            try {
                const provider = new firebase.auth.GoogleAuthProvider();
                firebase.auth().signInWithPopup(provider).then((result) => {
                    this.account.email = result.user.email
                    this.account.naam = result.additionalUserInfo.profile.given_name
                    this.account.achternaam = result.additionalUserInfo.profile.family_name
                    this.$store.dispatch('users/registersocial', this.account).catch(err => {
                        this.errmsg = err
                    })
                });
            } catch(err) {
                this.errmsg = err
            }
        }

And this fires to the store to register a user set a cookie and set the user in the store i do it like so:

    async registersocial({ commit }, account) {
        const token = await auth.currentUser.getIdToken();
        const { email, uid } = auth.currentUser;

        Cookie.set('access_token', token);

        commit('SET_USER', {
            email,
            uid
        });
        this.$router.push('profiel')
        const users = firebase.database().ref('Gebruikers/')
        users.once('value', function(snapshot){
            const naam = account.naam
            const achternaam = account.achternaam
            const email = account.email
            const google = "Google"

            var sameemail = null

            snapshot.forEach(function(childSnapshot) {
                const data = childSnapshot.exportVal()
                if(data.Email == email) {
                    sameemail = email
                }
            })

            if(sameemail != email) {
                users.push({
                    Voornaam: naam,
                    Achternaam: achternaam,
                    Email: email,
                    registerMethod: google
                })
            }
        })
    } 

And then for the cookie i set i do this:

import JWTDecode from 'jwt-decode';
import cookieparser from 'cookieparser';

export const actions = {
    nuxtServerInit({commit}, {req}) {
        if(process.server && process.static) return;
        if(!req.headers.cookie) return;
        const parsed = cookieparser.parse(req.headers.cookie);
        const accessTokenCookie = parsed.access_token;

        if(!accessTokenCookie) return;
        const decoded = JWTDecode(accessTokenCookie);
        
        if(decoded) {
            commit('users/SET_USER', {
                uid: decoded.user_id,
                email: decoded.email
            })
        }
    }
}

Please let me know what i am doing wrong. If u need any more code please let me know.

0

There are 0 best solutions below