获取参数并存储并在变量上使用它们以在我的方法上使用

时间:2017-09-26 14:35:38

标签: javascript firebase vue.js firebase-authentication vue-resource

我想获取一些参数并使用它们从firebase重置密码功能。

这就是我的链接的样子: http://localhost:8080/passwordreset?mode=resetPassword&oobCode=y6FIOAtRUKYf88Rt5OlEwxUuTyEmb3M4gquZSIseX2UAAAFevpj-gw&apiKey=AIzaSyBaCCvq-ZEfQmdrL7fmElXDjZF_J-tku2I

我想获得modeoobCodeapiKey。 这就是我现在所拥有的:

export default {


data: function() {
    return {
        passwordNew: '',
        passwordConfirm: '',
        mode:'',
        actionCode: '',
        continueUrl: '',
    }
},
methods: {
    handleResetPassword: function() {
        var accountEmail;

        firebase.auth().verifyPasswordResetCode(actionCode).then(function(email) {
            var accountEmail = email;
            firebase.auth().confirmPasswordReset(this.actionCode, this.passwordNew).then(function(resp) {

                alert("Password reset success");
                this.$router.push('hello')
            }).catch(function(error) {
                // Error occurred during confirmation. The code might have expired or the
                // password is too weak.
                console.log("error 1")
            });
        }).catch(function(error) {
            // Invalid or expired action code. Ask user to try to reset the password
            // again.
            console.log("error 2")
        });
    },
}

}

1 个答案:

答案 0 :(得分:1)

来自Firebase文档:

  

某些用户管理操作,例如更新用户的电子邮件地址   并重置用户的密码,导致电子邮件被发送到   用户。这些电子邮件包含收件人可以打开以完成的链接   或取消用户管理操作。默认情况下,用户管理   电子邮件链接到默认操作处理程序,这是一个托管的网页   在项目的Firebase托管域中的URL。

link:https://firebase.google.com/docs/auth/custom-email-handler

你需要获取这些参数并将它们存储在变量上,从firebase文档我得到了那些片段并且只写了getParameterByName函数:

function getParameterByName( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
    // Get the action to complete.
    var mode = getParameterByName('mode');
    // Get the one-time code from the query parameter.
    var actionCode = getParameterByName('oobCode');
    // (Optional) Get the continue URL from the query parameter if available.
    var continueUrl = getParameterByName('continueUrl');

您需要先获取这些参数并验证verifyPasswordResetCode方法上的操作码,然后您可以更改密码并将其与操作代码一起存储到方法中。

在您的导出默认值中:

data: function() {
        return {
            passwordNew: '',
            passwordConfirm: '',
            mode: mode,
            actionCode: actionCode,
            continueUrl: continueUrl,
        }
    },
    methods: {
        handleResetPassword: function() {
            var passwordNew = this.passwordNew
            var actionCode = this.actionCode
            firebase.auth().verifyPasswordResetCode(actionCode).then(function(email) {
                console.log("ActionCode: "+ actionCode);

                firebase.auth().confirmPasswordReset(actionCode, passwordNew).then(function(resp) {

                    alert("Password reset success");
                    this.$router.push('hello')
                }).catch(function(error) {
                    console.log("error 1"+ error)
                });
            }).catch(function(error) {
                console.log("Action code is invalid"+ error)
            });

        },
    }