diff --git a/CHANGELOG.md b/CHANGELOG.md index a58267a7..77a22e11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ X.Y.Z Release notes * None. ### Enhancements +* [Object Server] Added method `Realm.Sync.User.authenticate` to unify authentication of users. * [Object Server] Added JWT authenfication (#1548). ### Bug fixes diff --git a/lib/user-methods.js b/lib/user-methods.js index f605e6e3..04d4c526 100644 --- a/lib/user-methods.js +++ b/lib/user-methods.js @@ -272,19 +272,25 @@ const staticMethods = { authenticate(server, provider, options) { checkTypes(arguments, ['string', 'string', 'object']) - switch (provider) { + var json = {} + switch (provider.toLowerCase()) { case 'jwt': - case 'JWT': - options.provider = 'jwt' + json.provider = 'jwt' + json.token = options.token; + break + case 'password': + json.provider = 'password' + json.user_info = { password: options.password } + json.data = options.username break default: - return Promise.reject(`${provider} is not supported.`) + Object.assign(json, options) + json.provider = provider } - return _authenticate(this, server, options) + return _authenticate(this, server, json) }, - _refreshAccessToken: refreshAccessToken, }; diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index e3f11fd8..a738024e 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -136,6 +136,19 @@ module.exports = { })) }, + testAuthenticateWithPassword() { + const username = uuid(); + return Realm.Sync.User.register('http://localhost:9080', username, 'password').then((user) => { + user.logout(); + return Realm.Sync.User.authenticate('http://localhost:9080', 'password', { username: username, password: 'password' }); + }).then((user => { + assertIsUser(user); + const realm = new Realm({ sync: { user: user, url: 'realm://localhost:9080/~/test' } }); + TestCase.assertInstanceOf(realm, Realm); + realm.close(); + })) + }, + testLoginMissingUsername() { TestCase.assertThrows(() => Realm.Sync.User.login('http://localhost:9080', undefined, 'password')); },