[Vuejs]-How to link multiple providers on firebase with javascript

0👍

I would try and get rid of your self references if possible. Here’s a somewhat refactored approach:

var googleProvider = new firebase.auth.GoogleAuthProvider();
var facebookProvider = new firebase.auth.FacebookAuthProvider();

var app = {}

app.data = function() {
    return {
        email: '',
        password: '',
        credential: {}
    };
};

app.signup = function(provider) {
    firebase.auth().createUserWithEmailAndPassword(app.data.email, app.data.password).then(
        (user) => {
            self.registerProfile()
            user.sendEmailVerification()
            Materialize.toast('Email verification sent to: ' + app.data.email, 5000)
        },
        (err) => {
            console.log("Error Code: " + err.code)
            if (err.code == 'auth/email-already-in-use') {
                var credential = firebase.auth.EmailAuthProvider.credential(app.data.email, app.data.password)
                app.data.credential = credential
                //check if google-link with facebook or if facebook link with google
                app.googleSignin().then(function() {
                    firebase.auth().currentUser.link(app.data.credential).then(function(user) {
                        console.log("Account Linking success: " + user)
                    }, function(error) {
                        console.log("Error on linking accounts" + error)
                    })
                })
            } else {
                Materialize.toast('Uppss: ' + err.message, 5000)
                user.sendEmailVerification()
            }
        }
    );
};

app.googleSignin = function() {
    firebase.auth().signInWithPopup(googleProvider).then(function(result) {
        var token = result.credential.accessToken;
        var user = result.user;
    }).catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;
        console.log(error.code)
        console.log(error.message)
    });
};

app.facebookSignin = function() {
    firebase.auth().signInWithPopup(facebookProvider).then(function(result) {
        var token = result.credential.accessToken;
        var user = result.user;
    }).catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;
        console.log("error code:" + error.code)
        console.log("error msg:" + error.message)
    });
};

export default {
    name: 'signUp',
    data: app.data,
    methods: {
        signUp: app.signup,
        googleSignin: app.googleSignin,
        facebookSignin: app.facebookSignin,
    }
}

Things I still notice:

  • You have a call to self.registerProfile() however I can’t see this function anywhere in your code?
  • You reference data directly in your signup method however it would be better if this were passed in as a function parameter – is this possible?

Leave a comment