Reputation: 13
I've build an SwiftUI App that uses MongoDB Realm (or now App Services) as a backend. Users can authenticate using email/password, AppleID or Google.
In the docs and by mongoDB it says that realm automatically manages the login and the session in the sdk. However, after closing the app (without logout ofc) and reopening, the currentUser obj of my realm object is nil.
Is there any configuration necessary to the realm instance in the app or the console?
My code is quite simple i assume. After declaring my realm on on app Top Level with
let watchlistRealmApp = RealmSwift.App(id: "<my-app-ID>")
i check in my LoginViewModel if there is a user:
class LoginViewModel: ObservableObject {
// UserInteraction
@Published var inProgress = false
@Published var errorMessage = ""
@Published var isLoggedIn: Bool = false
var email: String?
var password: String?
// UserMetaData
private var contactEmail: String?
init() {
if watchlistRealmApp.currentUser?.isLoggedIn != nil || watchlistRealmApp.currentUser != nil {
print("loggedIn = true")
self.isLoggedIn = true
}
}
so i can show or hide my LoginView in my ContentView
if loginVm.isLoggedIn == false {
LoginView()
} else {
HomeView()
}
As explained, the expected behaviour would be that the SDK persists the information from the user and auto-logins to the users session. However the currentUser object is nil after closing and re-opening the app.
I saw that in the simulator, without closing but just building new it works. But in TestFlight or after "real" close of the app, i need to login, again.
Upvotes: 0
Views: 80