忘记密码按钮firebase iOS

时间:2017-12-16 17:33:56

标签: ios swift firebase

我正在尝试使用Firebase在我的应用中创建一个忘记密码按钮,但是如果我编写了我要在代码中更改密码的电子邮件,这就是“坏”代码行。

Auth.auth().sendPasswordReset(withEmail: "email@email"

如果不在代码中写入电子邮件,我该怎么做才能修改帐户密码?问题是如果我没有在代码中写下他的电子邮件地址,注册用户无法重置他的密码,它只适用于给定的电子邮件地址 以下是完整的代码

@IBAction func resetPasswordTapped(_ sender: UIButton) {
    var loginTextField: UITextField?
    let alertController = UIAlertController(title: "Password Recovery", message: "Please enter your email address", preferredStyle: .alert)
    let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in

        if loginTextField?.text != "" {
            Auth.auth().sendPasswordReset(withEmail: ("email@email") { (error) in
                if (error == nil) {

                    self.showErrorAlert(title: "Password reset", msg: "Check your inbox to reset your password")

                } else {
                    print(error)
                    self.showErrorAlert(title: "Unidentified email address", msg: "Please re-enter the email you registered with")
                }
            }
        }
        print("textfield is empty")

    })
    let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in

    }
    alertController.addAction(ok)
    alertController.addAction(cancel)
    alertController.addTextField { (textField) -> Void in
        // Enter the textfiled customization code here.
        loginTextField = textField
        loginTextField?.placeholder = "Enter your login ID"
    }
    present(alertController, animated: true, completion: nil)

}


func showErrorAlert(title: String, msg: String) {
    let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
    let action = UIAlertAction(title: "OK", style: .default, handler: nil)
    alert.addAction(action)
    present(alert, animated: true, completion: nil)}

1 个答案:

答案 0 :(得分:0)

如评论中所述,您的问题有点不清楚。您必须提供电子邮件以发送密码重置电子邮件。您不能使用任何其他内容,除非您严格编写函数来查找用户名/ user / userId以查找电子邮件。这将是有点不必要的。

这是我在我的应用程序中使用的一个例子

{this.state.showPasswordReset ? <div>
                <p>Input the email you signed up with, and we will email you directions to reset your password.</p>
                <form >
                    <TextField
                        hintText="email"
                        floatingLabelText="email"
                        multiLine={false}
                        rows={1}
                        name='email'
                        type='text'
                        value={this.state.email}
                        onChange={(email) => this.setState({ email })}
                    />
                </form>
                <RaisedButton label='Send Password Reset Email' onClick={this.sendPasswordResetEmail.bind(this)} primary={true} type='submit' />
                {/* <Button style={{ backgroundColor: '#757575', margin: 5 }} block onPress={this.sendPasswordResetEmail.bind(this)}>Send Password Reset Email</Button> */}
            </div>
                : <div>
                    <p>Forgot your password? Reset it <p style={{ color: 'red', textDecorationLine: "underline", }} onClick={() => this.setState({ showPasswordReset: !this.state.showPasswordReset })}>here</p></p>
                </div>}