【问题标题】:SwiftUI not staying logged outSwiftUI 不会保持注销状态
【发布时间】:2023-01-09 03:49:27
【问题描述】:

在我的 SwiftUI 应用程序中,我合并了一个登录页面。我添加了一个注销按钮,它确实会将您带回到登录页面,但是,一旦我重新打开应用程序,它就不会保持注销状态。

下面是登录页面所在的位置。

**class AppViewModel:ObservableObject{
**    
    let auth = Auth.auth()
    
    @Published var signedIn = false
    
    var isSignedIn: Bool {
        return auth.currentUser != nil //not signed in
    }
    
    func signIn(email: String, password: String){
        auth.signIn(withEmail: email,
                    password: password){ [weak self] result, error in
            guard result != nil, error == nil else{
                return
            }
            DispatchQueue.main.async{
                //Success
                self?.signedIn = true
            }
        }
    }
    
    func signUp(email: String, password: String){
        auth.createUser(withEmail: email, password: password){[weak self] result, error in
            guard result != nil, error == nil else{
                return
            }
         //success
            DispatchQueue.main.async{
                //Success
                self?.signedIn = true
            }
        }
        
    }
    
    func signOut(){
        try? auth.signOut()
        
        self.signedIn = false
    }
    
}



**struct ContentView: View {
**    @EnvironmentObject var viewModel: AppViewModel
    
    var body: some View {
        NavigationView{
            if viewModel.signedIn {
                    MainPage()
            }
            else{                
            }
        }
        .onAppear{
            viewModel.signedIn = viewModel.isSignedIn
        }
    }
}

struct SignInView: View {
    @State var email = ""
    @State var password = ""
    
    @EnvironmentObject var viewModel: AppViewModel
    
    var body: some View {
            VStack{
                Image("crest")
                    .resizable()
                    .scaledToFit()
                
                VStack{
                    TextField("Email Address", text: $email)
                        .disableAutocorrection(true)
                        .autocapitalization(.none)
                        .padding()
                        .background(Color(.secondarySystemBackground))
                    
                    SecureField("Password", text: $password)
                        .padding()
                        .background(Color(.secondarySystemBackground))
                    
                    Button(action: {
                        
                        guard !email.isEmpty, !password.isEmpty else{
                            return
                        }
                        viewModel.signIn(email: email, password: password)
                    }, label: {
                            Text("Sign In")
                        .foregroundColor(Color.white)
                        .frame(width: 200, height: 50)
                        .cornerRadius(8)
                        .background(Color.blue)
                           })
                    
                    NavigationLink("Create Account", destination: SignUpView())
                }
                .padding()
                
                Spacer()
            }
            .navigationTitle("Sign In")
    }
}


struct SignUpView: View {
    @State var email = ""
    @State var password = ""
    
    @EnvironmentObject var viewModel: AppViewModel
    
    var body: some View {
            VStack{
                Image("crest")
                    .resizable()
                    .scaledToFit()
                
                VStack{
                    TextField("Email Address", text: $email)
                        .disableAutocorrection(true)
                        .autocapitalization(.none)
                        .padding()
                        .background(Color(.secondarySystemBackground))
                    
                    SecureField("Password", text: $password)
                        .padding()
                        .background(Color(.secondarySystemBackground))
                    
                    Button(action: {
                        
                        guard !email.isEmpty, !password.isEmpty else{
                            return
                        }
                        viewModel.signUp(email: email, password: password)
                    }, label: {
                            Text("Create Account")
                        .foregroundColor(Color.white)
                        .frame(width: 200, height: 50)
                        .cornerRadius(8)
                        .background(Color.blue)
                           })
                }
                .padding()
                
                Spacer()
            }
            .navigationTitle("Create Account")
        
    }
}

然后在下面是我加入注销按钮的地方(在不同的视图中)

@EnvironmentObject var appViewModel: AppViewModel
....
Button {
                    appViewModel.signedIn = false
                } label: {
                   Text("Sign Out")
                }

这会将当前视图重新加载到登录页面,但不会停留在那里。

提前感谢您的帮助。

尝试弄乱 contentView 以了解为什么“signedIn”变量没有保留并围绕它进行研究。

【问题讨论】:

  • 您正在覆盖并消除从 firebase 获取错误以查看发生了什么的任何可能性。查看授权文档 done use try? use do try catch 并使用提供的侦听器来确定用户是否真正登录。您现在的编码方式 FirebaseAuth 毫无用处。

标签: swift swiftui firebase-authentication


【解决方案1】:

您没有在 AppViewModel 中调用 signOut 方法。

YourView: View {
    @EnvironmentObject var appViewModel: AppViewModel

    var body: some View {
        Button {
            // NOT THIS: appViewModel.signedIn = false
            appViewModel.signOut()
        } label: {
            Text("Sign Out")
        }
    }
}

每次打开应用程序时,您都在检查 Firebase 中是否有用户会话,并在此基础上构建身份验证逻辑。设置 signedIn 属性不会将用户从 Firebase 中注销。它只是你的局部变量。

【讨论】:

    猜你喜欢
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多