Adding password provider support to Realm.Sync.User.authenticate

This commit is contained in:
Kenneth Geisshirt 2017-12-21 12:43:38 +01:00
parent 26ed091c77
commit 58311c0087
3 changed files with 26 additions and 6 deletions

View File

@ -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

View File

@ -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,
};

View File

@ -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'));
},