@Environment(\.dismiss) not working after user login

I want to dismiss a sheet after a user successfully logs in.

struct LoginAnonymous: View {
    
    @EnvironmentObject var app: RealmSwift.App

    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        Button {
            anonymousLogin()
        } label: {
            Text("Login anonymously")
        }
    }
    
    func anonymousLogin(){
        Task {
            do {
                let user = try await app.login(credentials: .anonymous)
                print("Logged in as user with id: \(user.id)")
                DispatchQueue.main.async {
                    print("DISMISS")
                    dismiss()
                }
            } catch {
                print("Failed to log in: \(error.localizedDescription)")
            }
        }
    }
}

I tried wrapping the dismiss call inside of DispatchQueue.main.async, but the sheet still isn’t dismissed. I also tried passing the isPresented binding directly and calling isPresented = false, but it didn’t make a difference.

Do I need to do anything special when calling dismiss after some asynchronous work is completed?

(Maybe the app environment object coming from @ObservedObject var app = Realm.app changing after a user logs in refreshes the view and interferes with the dismiss call)

Full code