diff --git a/src/realm/object-store/sync/app.cpp b/src/realm/object-store/sync/app.cpp index ebb121d286..de4c3aa928 100644 --- a/src/realm/object-store/sync/app.cpp +++ b/src/realm/object-store/sync/app.cpp @@ -835,8 +835,10 @@ void App::log_in_with_credentials(const AppCredentials& credentials, const std:: return completion(nullptr, AppError(ErrorCodes::BadToken, "Could not log in user: received malformed JWT")); } - switch_user(user); - get_profile(user, std::move(completion)); + get_profile(user, [this, &completion](const std::shared_ptr& user, Optional error) { + switch_user(user); + completion(user, error); + }); }, false); } diff --git a/test/object-store/sync/app.cpp b/test/object-store/sync/app.cpp index 680ae6742b..a929cc37ad 100644 --- a/test/object-store/sync/app.cpp +++ b/test/object-store/sync/app.cpp @@ -4168,6 +4168,20 @@ TEST_CASE("app: custom user data integration tests", "[sync][app][user][function TEST_CASE("app: jwt login and metadata tests", "[sync][app][user][metadata][function][baas]") { TestAppSession session; auto app = session.app(); + + bool first_log_in = true; + auto token = app->subscribe([&first_log_in, &app](auto&) { + if (first_log_in) { + // Read the current user to verify that doing so does not deadlock + auto user = app->current_user(); + auto metadata = user->user_profile(); + auto custom_data = *user->custom_data(); + CHECK(custom_data["name"] == "Not Foo Bar"); + CHECK(metadata["name"] == "Foo Bar"); + first_log_in = false; + } + }); + auto jwt = create_jwt(session.app()->app_id()); SECTION("jwt happy path") {