diff --git a/lib/user-methods.js b/lib/user-methods.js index cc3389ab..b9baa3cd 100644 --- a/lib/user-methods.js +++ b/lib/user-methods.js @@ -82,8 +82,8 @@ function refreshAccessToken(user, localRealmPath, realmUrl) { if (newUser) { let session = newUser._sessionForOnDiskPath(localRealmPath); if (session) { + const errorHandler = session.config.error; if (response.status != 200) { - let errorHandler = session.config.error; let error = new AuthError(json); if (errorHandler) { errorHandler(session, error); @@ -94,6 +94,10 @@ function refreshAccessToken(user, localRealmPath, realmUrl) { parsedRealmUrl.set('pathname', json.access_token.token_data.path); session._refreshAccessToken(json.access_token.token, parsedRealmUrl.href); + if (errorHandler && errorHandler._notifyOnAccessTokenRefreshed) { + errorHandler(session, errorHandler._notifyOnAccessTokenRefreshed) + } + const tokenExpirationDate = new Date(json.access_token.token_data.expires * 1000); scheduleAccessTokenRefresh(newUser, localRealmPath, realmUrl, tokenExpirationDate); } diff --git a/tests/js/session-tests.js b/tests/js/session-tests.js index d15dda42..8850c083 100644 --- a/tests/js/session-tests.js +++ b/tests/js/session-tests.js @@ -54,26 +54,30 @@ module.exports = { testProperties() { return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => { - let config = { sync: { user, url: 'realm://localhost:9080/~/myrealm' } }; - let realm = new Realm(config); - let session = realm.syncSession; + return new Promise((resolve, _reject) => { + const accessTokenRefreshed = Symbol(); + let session; - TestCase.assertInstanceOf(session, Realm.Sync.Session); - TestCase.assertEqual(session.user.identity, user.identity); - TestCase.assertEqual(session.config.url, config.sync.url); - TestCase.assertEqual(session.config.user.identity, config.sync.user.identity); - TestCase.assertUndefined(session.url); - TestCase.assertEqual(session.state, 'active'); + let postTokenRefreshChecks = (sender, error) => { + TestCase.assertEqual(error, accessTokenRefreshed); + TestCase.assertEqual(session.url, `realm://localhost:9080/${user.identity}/myrealm`); + resolve(); + }; - /* disable until the sporadic failures are resolved - // give the session enough time to refresh its access token and bind itself - // TODO: Use an event to discover when the session is bound - let timeout = 4000; + // Let the error handler trigger our checks when the access token was refreshed. + postTokenRefreshChecks._notifyOnAccessTokenRefreshed = accessTokenRefreshed; - return wait(timeout).then(() => { - TestCase.assertEqual(session.url, `realm://localhost:9080/${user.identity}/myrealm`); + const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm', error: postTokenRefreshChecks } }; + const realm = new Realm(config); + session = realm.syncSession; + + TestCase.assertInstanceOf(session, Realm.Sync.Session); + TestCase.assertEqual(session.user.identity, user.identity); + TestCase.assertEqual(session.config.url, config.sync.url); + TestCase.assertEqual(session.config.user.identity, config.sync.user.identity); + TestCase.assertUndefined(session.url); + TestCase.assertEqual(session.state, 'active'); }); - */ }); },