Skip to content

Commit 4e6f95d

Browse files
authored
Throw a meaningful error when refreshing a token for an invalid Realm path (#2084)
* Throw a meaningful error when refreshing a token for an invalid Realm path * Changelog * Fix a typo * Fix a test
1 parent bc7b98d commit 4e6f95d

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
x.x.x Release notes (yyyy-MM-dd)
22
=============================================================
33
### Enhancements
4-
* None.
4+
* A more meaningful exception will be thrown when trying to refresh the access token for a Realm with an invalid url. Previously, trying to connect to a Realm with a url that lacks the path component (e.g. `realm://foo.com`) would result in errors like `Cannot read property ‘token_data’ of undefined`. Instead, now we'll print out the Realm url and provide a more meaningful exception message. ([#ROS-1310](https://github.com/realm/realm-object-server-private/issues/1310), since v1.0.2)
55

66
### Fixed
77
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
@@ -21,7 +21,7 @@ x.x.x Release notes (yyyy-MM-dd)
2121
* None.
2222

2323
### Fixed
24-
* Fixed an incorrect property named returned from `Realm.subscriptions()`. (since v2.19.0-rc.2)
24+
* Fixed an incorrect property name returned from `Realm.subscriptions()`. (since v2.19.0-rc.2)
2525
* Fixed opening query-based Realms with a dynamic schema. Previously the schema would always contain only the types present when the Realm was first added and not any types added later. (since v2.3.0)
2626

2727
### Compatibility

lib/user-methods.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ function refreshAccessToken(user, localRealmPath, realmUrl) {
201201
throw new Error("Server for user must be specified");
202202
}
203203

204-
let parsedRealmUrl = url_parse(realmUrl);
204+
const parsedRealmUrl = url_parse(realmUrl);
205+
const path = parsedRealmUrl.pathname;
206+
if (!path) {
207+
throw new Error(`Unexpected Realm path inferred from url '${realmUrl}'. The path section of the url should be a non-empty string.`);
208+
}
209+
205210
if (user.isAdminToken) {
206211
return refreshAdminToken(user, localRealmPath, realmUrl);
207212
}
@@ -211,7 +216,7 @@ function refreshAccessToken(user, localRealmPath, realmUrl) {
211216
method: 'POST',
212217
body: JSON.stringify({
213218
data: user.token,
214-
path: parsedRealmUrl.pathname,
219+
path,
215220
provider: 'realm',
216221
app_id: ''
217222
}),

tests/js/encryption-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module.exports = {
8181
encryptionKey: new Int8Array(64),
8282
sync: {
8383
user: adminUser,
84-
url: 'realm://localhost:9080'
84+
url: 'realm://localhost:9080/~/encryptedRealm'
8585
}
8686
});
8787
adminUser.logout(); // FIXME: clearTestState() doesn't clean up enough and Realm.Sync.User.current might not work

0 commit comments

Comments
 (0)