Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RCORE-2200: Fix switch_user and get_profile during log_in_with_credentials #7894

Merged
merged 13 commits into from
Jul 24, 2024
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* None.
* App subscription callback was getting fired before the user profile was retrieved on login, leading to an empty user profile when using the callback. ([#7889](https://github.com/realm/realm-core/issues/7889), since v14.7.0)

### Breaking changes
* None.
Expand Down
10 changes: 8 additions & 2 deletions src/realm/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,14 @@ 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 = std::move(completion)](const std::shared_ptr<User>& user,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't worked with C++ in a little while, tried to get it to be equivalent but let me know if this move, etc. doesn't make sense

Optional<AppError> error) {
if (!error) {
switch_user(user);
}
completion(user, error);
});
},
false);
}
Expand Down
15 changes: 15 additions & 0 deletions test/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4172,6 +4172,17 @@ TEST_CASE("app: jwt login and metadata tests", "[sync][app][user][metadata][func

SECTION("jwt happy path") {
bool processed = false;
bool logged_in_once = false;

auto token = app->subscribe([&logged_in_once, &app](auto&) {
REQUIRE(!logged_in_once);
auto user = app->current_user();
auto metadata = user->user_profile();

// Ensure that the JWT metadata fields are available when the callback is fired on login.
CHECK(metadata["name"] == "Foo Bar");
logged_in_once = true;
});

std::shared_ptr<User> user = log_in(app, AppCredentials::custom(jwt));

Expand All @@ -4192,6 +4203,10 @@ TEST_CASE("app: jwt login and metadata tests", "[sync][app][user][metadata][func
auto custom_data = *user->custom_data();
CHECK(custom_data["name"] == "Not Foo Bar");
CHECK(metadata["name"] == "Foo Bar");

REQUIRE(logged_in_once);

app->unsubscribe(token);
}
}

Expand Down
Loading