gpt4 book ai didi

swift - 如何在特定数据库引用中使用 FIRApp.createUserWithEmail?

转载 作者:搜寻专家 更新时间:2023-11-01 07:23:42 24 4
gpt4 key购买 nike

我一直在将我的 Firebase 2 后代码库转换为 Firebase 3,但遇到了一些麻烦。

所以基本上我在尝试弄清楚如何在我的 Firebase 数据库的特定位置创建新用户。

目标 - 我想保存所有新用户@ var _USER_REF = FIRDatabaseReference().child("\(BASE_URL)/users")

这是到目前为止的代码。

class  DataService {

static let dataService = DataService()

let BASE_URL = "https://project-1321.firebaseio.com"
var _BASE_REF = FIRDatabaseReference().child(BASE_URL)
var _USER_REF = FIRDatabaseReference().child("\(BASE_URL)/users")
var _NEWS_REF = FIRDatabaseReference().child("\(BASE_URL)/news")
var _MARKET_STATS = FIRDatabaseReference().child("\(BASE_URL)/market")

var CURRENT_USER_REF: FIRDatabaseReference {

let userID = NSUserDefaults.standardUserDefaults().valueForKey("uid") as! String
let currentUser = FIRDatabaseReference().child("\(_BASE_REF)").child("users").child(userID)
//let currentUser = Firebase(url: "\(BASE_REF)").childByAppendingPath("users").childByAppendingPath(userID)
return currentUser

}



func createNewAccount(uid: String, user: Dictionary<String, String>) {
_USER_REF.child(uid).setValue(user)
}



}

View Controller

@IBAction func registerAccount(sender: AnyObject) {

guard let email = self.emailRegField.text where !self.emailRegField.text!.isEmpty else {
return
}

guard let username = self.usernameRegField.text where !self.usernameRegField.text!.isEmpty else {
return
}

guard let password = self.passwordRegField.text where !self.passwordRegField.text!.isEmpty else {
return
}

FIRAuth.auth()?.createUserWithEmail(email, password: password) {
(user, error) in

if error != nil {
print(error)
self.signUpErrorAlert("Alert", message: "There was a problem signing up!")
} else {

let user = ["provider": user?.providerID, "email": email, "username": username]

DataService.createNewAccount(user) // Doesnt Work

}



//Store UID in NSDefaults so if user reopen app they automatically log in if UID exsisits.
NSUserDefaults.standardUserDefaults().setValue(result ["uid"], forKey: "uid")

// Segue New User

self.performSegueWithIdentifier("newUserSegue", sender: nil)

}

//登录已将 UID 保存到 NSDefaults 的用户

当用户登录或注册时,我计划将他们的“UID”保存到 NSDefaults。

然后像这样检查:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if NSUserDefaults.standardUserDefaults().valueForKey("uid") != nil {
self.performSegueWithIdentifier("newUserSegue", sender: nil)
} else {
print("User is not registered or has their UID saved to NSDefaults")
}

}

这是一种安全的方法吗?

最佳答案

@brkr 针对您上面的评论,您仍然可以使用 UID 将唯一用户添加到您的 Firebase 数据库。

例如,这是您的用户引用(在 DataService 类中):让 REF_USERS = FIRDatabase.database().referenceFromURL("\(URL_BASE)/users")

现在,在 Firebase 3.0 中创建一个用户:

FIRAuth.auth()!.createUserWithEmail(email, password: pwd, completion: { authData, error  in
if error == nil {
// Log user in
FIRAuth.auth()?.signInWithEmail(email, password: pwd) { authData, error in
// Save user information to Firebase data
let user = // your user Dictionary
DataService.createNewAccount(authData!.uid, user: user)
}
} else {
// Handle login error here
}
})

创建账户方法的代码:

func createNewAccount(uid: String, user: Dictionary<String, String>) {
REF_USERS.child(uid).setValue(user)
}

关于swift - 如何在特定数据库引用中使用 FIRApp.createUserWithEmail?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37365876/

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