根据角色显示不同的视图控制器-Firebase,Swift 3 [英] Display different view controllers based on role-Firebase, Swift 3
问题描述
我正在构建一个非常简单的应用程序,该应用程序将用户的报告发送给管理员.到目前为止,我已经完成了整个前端.我有菜单在工作,报告顺序似乎很短.现在是我该承担后端责任的时候了.我是一名新的Swift开发人员,完全是自学成才的(就像您应该是:)一样),我在一些事情上感到困惑.我只需要一些指导,我只是通过阅读在后面的堆栈溢出中获取建议,但从未提出过任何问题.所以!我对您这个敬虔的堆栈社区的问题是!:
I'm building a pretty simple app that sends reports from a user to an admin. So far i have the entire front end done. I have the menu working, and the report sequence is seemless. Now its time for me to take on the back end. I am a new Swift developer, completely self taught(like you should be :) ) and i'm stumped on a few things. I just need some guidance, i've used stack overflow in the back for advice by just reading but have never asked a question. So! my question to you, the godly stack community, is!:
我有两个用户角色.
- 普通用户
- 管理员
根据他们在Firebase中的角色,我希望能够在他们登录时将其重定向到各自的视图控制器.现在!我问了我的一个朋友,他告诉我他们都可以在同一个应用程序中完成,我不需要为管理员制作其他应用程序.我猜这是真的,因为我相信他的判断.我当时在考虑是否要检查
I want to be able to, based on their role in firebase, when they login redirect them to their respective view controller. Now! i asked a friend of mine who told me they can all be done in the same app, i dont need to make a different app for the admins. I'm guessing this is true because i trust his judgement. I was thinking of doing if checks ie
//If role is = to admin
self.performSegue(admin VC)
//else
self.performSegue(homeScreen)
我遇到问题1.分配角色,并且2.从Firebase访问它!
I am having issues 1. assigning the role and 2. accessing it from firebase!
任何见解/技巧都将不胜感激!
Any insight/tips is much appreciated!
谢谢你堆满了神
推荐答案
这将是一个两步过程
第一步是验证用户身份并检索其用户uid
Step one is to authenticate the user and retrieve their user 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)
}
})
假设您有一个标准用户节点,其中包含其他用户数据
Assuming you have a standard users node which contains additional user data
users
uid_0
fav_food: "pizza"
user_role: "admin"
uid_1
fav_food: "tacos"
user_role: "normal_user"
步骤2:然后将调用assignUserRole函数以获取用户信息并设置UI
Step 2: the assignUserRole function will then be called to get the user information and set up the 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
}
})
}
这篇关于根据角色显示不同的视图控制器-Firebase,Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!