gpt4 book ai didi

ios - RealmSwift : Implementing one time login with MongoDB Realm in swift

转载 作者:行者123 更新时间:2023-12-01 21:28:08 25 4
gpt4 key购买 nike

我希望用户登录一次,而不必在每次打开应用程序时重新输入登录信息,除非他们在上次 session 中注销。
当前每次打开应用程序时都会显示登录屏幕。这是我的 Root View

struct AppRootView: View {

var body: some View {
AnyView {

// check if user has already logged in here and then route them accordingly

if auth.token != nil {
homeMainView()
} else {
LoginController()
}
}
}
}

目前这是我用来登录用户的

 @objc func signUp() {
setLoading(true);
app.usernamePasswordProviderClient().registerEmail(username!, password: password!, completion: {[weak self](error) in
// Completion handlers are not necessarily called on the UI thread.
// This call to DispatchQueue.main.sync ensures that any changes to the UI,
// namely disabling the loading indicator and navigating to the next page,
// are handled on the UI thread:
DispatchQueue.main.sync {
self!.setLoading(false);
guard error == nil else {
print("Signup failed: \(error!)")
self!.errorLabel.text = "Signup failed: \(error!.localizedDescription)"
return
}
print("Signup successful!")

// Registering just registers. Now we need to sign in, but we can reuse the existing username and password.
self!.errorLabel.text = "Signup successful! Signing in..."
self!.signIn()
}
})
}

@objc func signIn() {
print("Log in as user: \(username!)");
setLoading(true);

app.login(withCredential: AppCredentials(username: username!, password: password!)) { [weak self](maybeUser, error) in

DispatchQueue.main.sync {
self!.setLoading(false);
guard error == nil else {
// Auth error: user already exists? Try logging in as that user.
print("Login failed: \(error!)");
self!.errorLabel.text = "Login failed: \(error!.localizedDescription)"
return
}

guard let user = maybeUser else {
fatalError("Invalid user object?")
}

print("Login succeeded!");


//
let hostingController = UIHostingController(rootView: ContentView())
self?.navigationController?.pushViewController(hostingController, animated: true)
}

如何实现一次登录,以便用户每次打开应用程序时都必须登录?

最佳答案

一个正确配置和初始化的RealmApp类将在应用程序重新启动之间为您保留 session 信息,您可以使用 .currentUser() 检查现有 session 这个类的方法。所以在你的情况下是这样的:

if app.currentUser() != nil {
homeMainView()
} else {
LoginController()
}

关于ios - RealmSwift : Implementing one time login with MongoDB Realm in swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63253814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com