我将我的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的用户中的登录
当用户登录或登记时,我打算保存他们的&#34; UID&#34;到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")
}
}
这是一种安全的方法吗?
答案 0 :(得分:1)
我不认为有必要使用新的Firebase,在显示板中查看,您的所有用户都应位于“验证”标签下,
此新行在新Firebase中没有任何意义,您查询的URL位于您下载的.plist中。
let BASE_URL = "https://project-1321.firebaseio.com" //remove this line
并使用类似
的内容let firebaseRef = FIRDatabase.database().reference()
let newsRef = firebaseRef.child("news")
找到许多有用的信息
答案 1 :(得分:1)
@brkr在回复上述评论时,您仍然可以使用UID向Firebase数据库添加唯一身份用户。
例如,这是您的用户ref(在DataService类中):
char
现在,在Firebase 3.0中创建一个用户:
let REF_USERS = FIRDatabase.database().referenceFromURL("\(URL_BASE)/users")
创建帐户方法的代码:
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
}
})