我正在构建一个非常简单的应用程序,可以将用户的报告发送给管理员。到目前为止,我已经完成了整个前端。我有菜单工作,报告序列是无缝的。现在是我采取后端的时候了。我是一个新的Swift开发人员,完全自学成才(就像你应该:)但我对一些事情感到难过。我只是需要一些指导,我在后面使用堆栈溢出来获取建议只是阅读但从未问过问题。所以!我的问题,敬虔的堆栈社区,是!:
我有两个用户角色..
我希望能够根据他们在firebase中的角色,在登录时将其重定向到各自的视图控制器。现在!我问我的一位朋友,他告诉我他们都可以在同一个应用程序中完成,我不需要为管理员制作不同的应用程序。我猜这是真的,因为我相信他的判断。我正在考虑如果检查即
//If role is = to admin
self.performSegue(admin VC)
//else
self.performSegue(homeScreen)
我遇到问题1.分配角色,2。从firebase访问角色!
非常感谢任何见解/提示!
谢谢堆叠溢神
答案 0 :(得分:1)
这将是一个两步过程
第一步是验证用户身份并检索他们的用户uid
Auth.auth().signIn(withEmail: "dude@thing.com", password: "password",
completion: { (auth, error) in
if error != nil {
let err = error?.localizedDescription
print(err!)
} else {
print("succesfully authd")
let uid = auth!.uid
assignUserRole(uid)
}
})
假设您有一个包含其他用户数据的标准用户节点
users
uid_0
fav_food: "pizza"
user_role: "admin"
uid_1
fav_food: "tacos"
user_role: "normal_user"
步骤2:然后调用assignUserRole函数以获取用户信息并设置UI
function assignUserRole(theUid: String) {
let thisUserRef = appRef.child("users").child(theUid)
thisUserRef.observeSingleEvent(of: .value, with: { snapshot in
let userDict = snapshot.value as! [String: Any]
let favFood = userDict["fav_food"] as! String
let userRole = userDict["user_role"] as! String
print(" \(favFood) \(userRole")
if userRole == "admin" {
//display admin viewController
} else {
//display normal user viewController
}
})
}
答案 1 :(得分:-1)
It's hard to give a precise answer since your question is rather broad but something along these lines should accomplish what you're going for. Just be sure to switch out the placeholders with the correct information:
//Fetch user role from Firebase DB
FIRDatabase.database().reference().child("PATH_TO_YOUR_USER_OBJECT").observeSingleEvent(of: .value, with: { (snapshot) in
//Obtain value of user role
let snapValue = snapshot.value as! [String:AnyObject]
let userRole = snapValue["KEY_OF_USER_ROLE"] as! String
//Set default vc in case of error
var viewControllerID = "DEFAULT_VC_ID"
//Set vc based on user role
if userRole == "admin" {
viewControllerID = "ADMIN_VC_ID"
} else {
viewControllerID = "USER_VC_ID"
}
//Instantiate correct vc and make it the root vc
self.storyboard = UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: Bundle.main)
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: viewControllerID)
})