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.
This commit is contained in:
Mark Rowe 2017-12-22 10:23:50 -08:00
parent 9505b750f9
commit 7b176167c4
2 changed files with 17 additions and 2 deletions

View File

@ -91,7 +91,7 @@ function refreshAccessToken(user, localRealmPath, realmUrl) {
// Look up a fresh instance of the user. // Look up a fresh instance of the user.
// We do this because in React Native Remote Debugging // We do this because in React Native Remote Debugging
// `Realm.clearTestState()` will have invalidated the user object // `Realm.clearTestState()` will have invalidated the user object
let newUser = user.constructor.all[user.identity]; let newUser = user.constructor._getExistingUser(user.server, user.identity);
if (!newUser) { if (!newUser) {
return; return;
} }

View File

@ -59,6 +59,7 @@ class UserClass : public ClassDefinition<T, SharedUser> {
using Value = js::Value<T>; using Value = js::Value<T>;
using Function = js::Function<T>; using Function = js::Function<T>;
using ReturnValue = js::ReturnValue<T>; using ReturnValue = js::ReturnValue<T>;
using Arguments = js::Arguments<T>;
public: public:
std::string const name = "User"; std::string const name = "User";
@ -79,10 +80,12 @@ public:
static void create_user(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); 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 admin_user(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void get_existing_user(ContextType, ObjectType, Arguments, ReturnValue&);
MethodMap<T> const static_methods = { MethodMap<T> const static_methods = {
{"createUser", wrap<create_user>}, {"createUser", wrap<create_user>},
{"_adminUser", wrap<admin_user>} {"_adminUser", wrap<admin_user>},
{"_getExistingUser", wrap<get_existing_user>},
}; };
/*static void current_user(ContextType ctx, ObjectType object, ReturnValue &return_value);*/ /*static void current_user(ContextType ctx, ObjectType object, ReturnValue &return_value);*/
@ -153,6 +156,18 @@ void UserClass<T>::admin_user(ContextType ctx, FunctionType, ObjectType this_obj
return_value.set(create_object<T, UserClass<T>>(ctx, user)); return_value.set(create_object<T, UserClass<T>>(ctx, user));
} }
template<typename T>
void UserClass<T>::get_existing_user(ContextType ctx, ObjectType, Arguments arguments, ReturnValue& return_value) {
arguments.validate_count(2);
SharedUser *user = new SharedUser(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<T, UserClass<T>>(ctx, user));
}
template<typename T> template<typename T>
void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) { void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) {
auto users = Object::create_empty(ctx); auto users = Object::create_empty(ctx);