gpt4 book ai didi

swift - 如何使用 Swift 在 Firebase 中对密码进行哈希处理?

转载 作者:行者123 更新时间:2023-11-30 12:32:40 25 4
gpt4 key购买 nike

我正在关注 Uber 克隆 tutorial 。我可以登录、注册和注销,但密码似乎没有经过哈希处理;我可以在我的 Firebase 数据库中清楚地看到它们。

这是我的代码。第一个登录/注册/注销功能保存在与 Controller 分开的“插件”文件夹中。

import Foundation
import FirebaseAuth

typealias LoginHandler = (_ msg: String?) -> Void;

struct LoginErrorCode {
static let INVALID_EMAIL = "Invalid email, please provide a real email address";
static let WRONG_PASSWORD = "Wrong Password, Please Try Again";
static let PROBLEM_CONNECTING = "Problem Connecting to Database. Please Try Later";
static let USER_NOT_FOUND = "User Not Found, Please Register";
static let EMAIL_ALREADY_IN_USE = "Email Already In Use, Please Use Different Email";
static let WEAK_PASSWORD = "Password Should Be At Least 6 Characters";
}

class AuthProvider {
private static let _instance = AuthProvider();

static var Instance: AuthProvider {
return _instance;
}

func login(withEmail: String, password: String, loginHandler: LoginHandler?) {
FIRAuth.auth()?.signIn(withEmail: withEmail, password: password, completion: { (user, error) in
if error != nil {
self.handleErrors(err: error as! NSError, loginHandler: loginHandler);
} else {
loginHandler?(nil);
}
})
} //login func

func signUp(withEmail: String, password: String, loginHandler: LoginHandler?) {
FIRAuth.auth()?.createUser(withEmail: withEmail, password: password, completion: { (user, error) in
if error != nil {
self.handleErrors(err: error as! NSError, loginHandler: loginHandler);
} else {
if user?.uid != nil {
// store the user to database
DBProvider.Instance.saveUser(withID: user!.uid, email: withEmail, password: password)
//log in the user
self.login(withEmail: withEmail, password: password, loginHandler: loginHandler)
}
}
})

} //sign up func

func logOut() -> Bool {
if FIRAuth.auth()?.currentUser != nil {
do {
try FIRAuth.auth()?.signOut();
return true;
} catch {
return false;
}
}
return true
}

private func handleErrors(err: NSError, loginHandler: LoginHandler?) {
if let errCode = FIRAuthErrorCode(rawValue: err.code) {
switch errCode {
case .errorCodeWrongPassword:
loginHandler?(LoginErrorCode.WRONG_PASSWORD);
break;
case .errorCodeInvalidEmail:
loginHandler?(LoginErrorCode.INVALID_EMAIL);
break;
case .errorCodeUserNotFound:
loginHandler?(LoginErrorCode.USER_NOT_FOUND);
break;
case .errorCodeEmailAlreadyInUse:
loginHandler?(LoginErrorCode.EMAIL_ALREADY_IN_USE);
break;
case .errorCodeWeakPassword:
loginHandler?(LoginErrorCode.WEAK_PASSWORD);
break;
default:
loginHandler?(LoginErrorCode.PROBLEM_CONNECTING);
break;
}
}
}
} //class

Controller :

import UIKit
import FirebaseAuth

class SignInVC: UIViewController {
private let DRIVER_SEGUE = "DriverVC";

@IBOutlet weak var emailTextField: UITextField!

@IBOutlet weak var passwordTextField: UITextField!


override func viewDidLoad() {
super.viewDidLoad()


// Do any additional setup after loading the view.
}

@IBAction func login(_ sender: Any) {

if emailTextField.text != "" && passwordTextField.text != "" {
AuthProvider.Instance.login(withEmail: emailTextField.text!, password: passwordTextField.text!, loginHandler: { (message) in
if message != nil {
self.alertTheUser(title: "Problem With Authentication", message: message!);
} else {
self.performSegue(withIdentifier: self.DRIVER_SEGUE, sender: nil)
}
});
} else {
alertTheUser(title: "Email And Password Are Required", message: "Please enter email and password");
}
}


@IBAction func signUp(_ sender: Any) {
if emailTextField.text != "" && passwordTextField.text != "" {
AuthProvider.Instance.signUp(withEmail: emailTextField.text!, password: passwordTextField.text!, loginHandler: { (message) in
if message != nil {
self.alertTheUser(title: "Problem With Creating New Account", message: message!)
} else {
self.performSegue(withIdentifier: self.DRIVER_SEGUE, sender: nil)
}
})
} else {
alertTheUser(title: "Email And Password Are Required", message: "Please enter email and password");
}
}


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


} //class

最佳答案

尝试在 FIRUser 上使用 updatePassword(_ password: String,completion: FirebaseAuth.FIRUserProfileChangeCallback? = nil)

关于swift - 如何使用 Swift 在 Firebase 中对密码进行哈希处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43301437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com