1

我猜我的 vuejs 商店正在重置我的授权。我还在标题组件中收到错误消息Cannot read property 'home' of undefined。我不知道为什么这不起作用。我尝试调试。

这就是我所拥有的:在我的 nuxt.config.js 中,我将插件设置为 firebase:

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

然后这是我的 firebase.js 文件:

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

我不知道这是否与我的错误有关,但出于目的,我只是将其放入。然后对于我的注册页面,我使用他们的 gmail 帐户注册用户,我有这样的功能methods

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
            }
        }

这会触发商店注册用户设置cookie并将用户设置在商店中我这样做:

    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
                })
            }
        })
    } 

然后对于我设置的cookie,我这样做:

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
            })
        }
    }
}

请让我知道我做错了什么。如果您需要更多代码,请告诉我。

4

0 回答 0