From b2da39855992a0c15422a7c4363154293a600acb Mon Sep 17 00:00:00 2001 From: Yavor Georgiev Date: Wed, 1 Feb 2017 23:44:56 +0100 Subject: [PATCH] Fix token refresh and session bugs in chrome debugging (#831) --- lib/browser/rpc.js | 2 +- lib/browser/session.js | 8 +++++--- lib/user-methods.js | 16 ++++++++++++---- tests/js/session-tests.js | 6 +++++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/browser/rpc.js b/lib/browser/rpc.js index 3bcd7650..a8044b2a 100644 --- a/lib/browser/rpc.js +++ b/lib/browser/rpc.js @@ -141,7 +141,7 @@ function serialize(realmId, value) { return {type: objectTypes.DICT, keys, values}; } -function deserialize(realmId, info) { +export function deserialize(realmId, info) { let type = info.type; let handler = type && typeConverters[type]; if (handler) { diff --git a/lib/browser/session.js b/lib/browser/session.js index ffe0a1d1..34b51261 100644 --- a/lib/browser/session.js +++ b/lib/browser/session.js @@ -20,12 +20,13 @@ import { keys, objectTypes } from './constants'; import { getterForProperty, createMethods } from './util'; +import { deserialize } from './rpc'; export default class Session { } Object.defineProperties(Session.prototype, { - url: getterForProperty('url'), - state: getterForProperty('state') + url: { get: getterForProperty('url') }, + state: { get: getterForProperty('state') } }); createMethods(Session.prototype, objectTypes.SESSION, [ @@ -42,7 +43,8 @@ export function createSession(realmId, info) { sessionProxy[keys.id] = info.id; sessionProxy[keys.type] = objectTypes.SESSION; - Object.assign(sessionProxy, info.data); + sessionProxy.user = deserialize(realmId, info.data.user); + sessionProxy.config = deserialize(realmId, info.data.config) return sessionProxy; } \ No newline at end of file diff --git a/lib/user-methods.js b/lib/user-methods.js index b17e4fb5..626a7f81 100644 --- a/lib/user-methods.js +++ b/lib/user-methods.js @@ -58,10 +58,18 @@ function authenticateRealm(user, fileUrl, realmUrl) { if (response.status != 200) { //FIXME: propagate error to session error handler } else { - return response.json().then((body) => { - parsedRealmUrl.set('pathname', body.access_token.token_data.path); - let session = user._sessionForOnDiskPath(fileUrl); - session._refreshAccessToken(body.access_token.token, parsedRealmUrl.href); + return response.json().then(function(body) { + // 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 + let newUser = user.constructor.all[user.identity]; + if (newUser) { + let session = newUser._sessionForOnDiskPath(fileUrl); + if (session) { + parsedRealmUrl.set('pathname', body.access_token.token_data.path); + session._refreshAccessToken(body.access_token.token, parsedRealmUrl.href); + } + } }); } }); diff --git a/tests/js/session-tests.js b/tests/js/session-tests.js index 4a763610..c95b6c35 100644 --- a/tests/js/session-tests.js +++ b/tests/js/session-tests.js @@ -66,7 +66,11 @@ module.exports = { TestCase.assertEqual(session.state, 'active'); // give the session enough time to refresh its access token and bind itself - return wait(500).then(() => { + let timeout = 500; + if (typeof window !== 'undefined') { + timeout = 2500; // need a longer timeout under React Native because remote debugging + } + return wait(timeout).then(() => { TestCase.assertEqual(session.url, `realm://localhost:9080/${user.identity}/myrealm`); }); });