From cd8db46aa79c28e6e457aa895311f19aab07d4fe Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Thu, 11 Jan 2018 07:00:31 -0800 Subject: [PATCH] When refreshing the token, look up the user in a way that doesn't suffer from #1586 (#1587) * When refreshing the token, look up the user in a way that doesn't suffer from #1586 Expose a means of looking up a user by identity and server to avoid problems if the same user identity exists for multiple servers, which can happen when connecting to the same server via different hostnames. * Return undefined if the user doesn't exist rather than returning an object wrapping a null SyncUser. --- CHANGELOG.md | 4 +++- lib/user-methods.js | 2 +- src/js_sync.hpp | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8587539c..007ca090 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,9 @@ X.Y.Z Release notes * [Object Server] Added JWT authenfication (#1548). ### Bug fixes -* Fixed a bug where `Realm.open` could unexpectedly raise a "Realm at path ... already opened with different schema version" error. +* Fix a bug where `Realm.open` could unexpectedly raise a "Realm at path ... already opened with different schema version" error. +* Increased request timeout for token refresh requests to 10 seconds. This + should help with failing token refreshes on a loaded server (#1586). * Increased request timeout for token refresh requests to 10 seconds. This should help with failing token refreshes on a loaded server. ### Internal diff --git a/lib/user-methods.js b/lib/user-methods.js index 3cc6ca61..d5cf4a60 100644 --- a/lib/user-methods.js +++ b/lib/user-methods.js @@ -138,7 +138,7 @@ function refreshAccessToken(user, localRealmPath, realmUrl) { // Look up a fresh instance of the user. // We do this because in React Native Remote Debugging // `Realm.clearTestState()` will have invalidated the user object - const newUser = user.constructor.all[user.identity]; + let newUser = user.constructor._getExistingUser(user.server, user.identity); if (!newUser) { return; } diff --git a/src/js_sync.hpp b/src/js_sync.hpp index 13951839..8a57aaa6 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -59,6 +59,7 @@ class UserClass : public ClassDefinition { using Value = js::Value; using Function = js::Function; using ReturnValue = js::ReturnValue; + using Arguments = js::Arguments; public: std::string const name = "User"; @@ -81,10 +82,12 @@ public: static void create_user(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); static void admin_user(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); + static void get_existing_user(ContextType, ObjectType, Arguments, ReturnValue&); MethodMap const static_methods = { {"createUser", wrap}, - {"_adminUser", wrap} + {"_adminUser", wrap}, + {"_getExistingUser", wrap}, }; /*static void current_user(ContextType ctx, ObjectType object, ReturnValue &return_value);*/ @@ -160,6 +163,16 @@ void UserClass::admin_user(ContextType ctx, FunctionType, ObjectType this_obj return_value.set(create_object>(ctx, user)); } +template +void UserClass::get_existing_user(ContextType ctx, ObjectType, Arguments arguments, ReturnValue& return_value) { + arguments.validate_count(2); + if (auto user = syncManagerShared().get_existing_logged_in_user(SyncUserIdentifier{ + Value::validated_to_string(ctx, arguments[1], "identity"), + Value::validated_to_string(ctx, arguments[0], "authServerUrl")})) { + return_value.set(create_object>(ctx, new SharedUser(std::move(user)))); + } +} + template void UserClass::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) { auto users = Object::create_empty(ctx);