使用firebase和Xcode将不同的用户发送到单独的视图控制

使用firebase和Xcode将不同的用户发送到单独的视图控制

本文介绍了如何使用firebase和Xcode将不同的用户发送到单独的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码很新,并且开始使用firebase作为我在Xcode中使用swift创建的应用程序的后端服务器。

I am fairly new to coding and have started using firebase as a back end server for an application i am creating in Xcode using swift.

该应用程序本身将具有一个登录页面,但有3种不同类型的用户。管理员将拥有与其他2个用户不同的权限。

The app itself will have one login page but 3 separate types of users. The admin will have different permissions to the other 2 users.

我目前拥有的代码是:

FIRAuth.auth()?.signIn(withEmail: username!, password: password!, completion: { (user, error) in
    if error == nil {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "AdminVC")
        self.present(vc!, animated: true, completion: nil)
    }

代码正在获取身份验证页面的电子邮件和密码。但由于3种不同类型的用户,我不希望他们都去'AdminVC'视图控制器。

The code is getting the email and password for the authentication page. But because of the 3 different types of users I don't want them all going to the 'AdminVC' view controller.

有没有办法让其他2个用户使用这种身份验证方法转到他们自己的视图控制器?

Is there a way of getting the 2 other users to go to their own view controllers using this authentication method?

推荐答案

如果你想为用户存储一个类型,你必须使用数据库。就像这样

If you want to store a type for a user you have to use the database. Like this

当用户登录时,从数据库中获取路径users /< userId> / type的值。然后使用switch语句重定向到正确的视图控制器。

When the user logs in, get the value from the database for the path "users/<userId>/type". Then use a switch statement to redirect to the correct view controller.

这是完整代码

 // Sign in to Firebase
 FIRAuth.auth()?.signIn(withEmail: "[email protected]", password: "Password123", completion: {
     (user, error) in
         // If there's no errors
         if error == nil {
             // Get the type from the database. It's path is users/<userId>/type.
             // Notice "observeSingleEvent", so we don't register for getting an update every time it changes.
             FIRDatabase.database().reference().child("users/\(user!.uid)/type").observeSingleEvent(of: .value, with: {
                 (snapshot) in

                 switch snapshot.value as! String {
                 // If our user is admin...
                 case "admin":
                     // ...redirect to the admin page
                     let vc = self.storyboard?.instantiateViewController(withIdentifier: "adminVC")
                     self.present(vc!, animated: true, completion: nil)
                 // If out user is a regular user...
                 case "user":
                     // ...redirect to the user page
                     let vc = self.storyboard?.instantiateViewController(withIdentifier: "userVC")
                     self.present(vc!, animated: true, completion: nil)
                 // If the type wasn't found...
                 default:
                     // ...print an error
                     print("Error: Couldn't find type for user \(user!.uid)")
                 }
            })
        }
    })

而不是整个开关语句你可以做

Instead of the whole switch statement you can do

let vc = self.storyboard?.instantiateViewController(withIdentifier: "\(snapshot.value)_View")
self.present(vc!, animated: true, completion: nil)

警告!如果找不到类型,这将崩溃。但这是可以解决的:)

Warning! This will crash if the type isn't found. But that's fixable :)

这篇关于如何使用firebase和Xcode将不同的用户发送到单独的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:52