Pass AuthError in Realm.Sync.User methods callbacks (#619)
* Pass AuthError in Realm.Sync.User methods callbacks The new Realm.Sync.AuthError class exposes properties common to the Problem family of classes in the Realm Object Server * extract AuthError in a separate file * whitespace
This commit is contained in:
parent
a66198fe46
commit
622482029a
18
docs/sync.js
18
docs/sync.js
|
@ -48,6 +48,24 @@ Sync.setLogLevel = function(log_level) {};
|
|||
* @type {("error"|"info"|"debug")}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class that describes authentication errors in the Realm Object Server
|
||||
* @memberof Realm.Sync
|
||||
*/
|
||||
class AuthError extends Error {
|
||||
/**
|
||||
* The numerical code for this error.
|
||||
* @type {number}
|
||||
*/
|
||||
get code() {}
|
||||
|
||||
/**
|
||||
* The unique help URI that describes this error.
|
||||
* @type {string}
|
||||
*/
|
||||
get type() {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for logging in and managing Sync users.
|
||||
* @memberof Realm.Sync
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright 2016 Realm Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
'use strict';
|
||||
|
||||
class AuthError extends Error {
|
||||
constructor(problem) {
|
||||
super(problem.title);
|
||||
Object.assign(this, problem);
|
||||
}
|
||||
}
|
||||
|
||||
exports['AuthError'] = AuthError;
|
|
@ -25,6 +25,7 @@ module.exports = function(realmConstructor) {
|
|||
// Add sync methods
|
||||
if (realmConstructor.Sync) {
|
||||
realmConstructor.Sync.User = require('./sync').User;
|
||||
realmConstructor.Sync.AuthError = require('./errors').AuthError;
|
||||
}
|
||||
|
||||
// TODO: Remove this now useless object.
|
||||
|
|
16
lib/sync.js
16
lib/sync.js
|
@ -1,5 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const AuthError = require('./errors').AuthError;
|
||||
|
||||
function node_require(module) {
|
||||
return require(module);
|
||||
}
|
||||
|
@ -39,21 +41,19 @@ const postHeaders = {
|
|||
function _authenticate(server, json, callback) {
|
||||
json.app_id = '';
|
||||
var options = {
|
||||
url: server + 'auth',
|
||||
url: server + '/auth',
|
||||
body: JSON.stringify(json),
|
||||
headers: postHeaders
|
||||
};
|
||||
post(options, function(error, response, body) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
callback(error);
|
||||
}
|
||||
else if (response.statusCode != 200) {
|
||||
console.log('Bad response: ' + response.statusCode);
|
||||
callback(new Error('Bad response: ' + response.statusCode));
|
||||
callback(new AuthError(JSON.parse(body)));
|
||||
}
|
||||
else {
|
||||
var rjson = JSON.parse(body);
|
||||
let rjson = JSON.parse(body);
|
||||
// TODO: validate JSON
|
||||
|
||||
const token = rjson.refresh_token.token;
|
||||
|
@ -109,7 +109,7 @@ User.create = function(server, username, password, callback) {
|
|||
|
||||
User.authenticateRealm = function(fileUrl, realmUrl, callback) {
|
||||
var options = {
|
||||
url: this.server + 'auth',
|
||||
url: this.server + '/auth',
|
||||
body: JSON.stringify({
|
||||
data: this.token,
|
||||
path: url.parse(realmUrl).path,
|
||||
|
@ -120,12 +120,10 @@ User.authenticateRealm = function(fileUrl, realmUrl, callback) {
|
|||
};
|
||||
post(options, function(error, response, body) {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
callback(error);
|
||||
}
|
||||
else if (response.statusCode != 200) {
|
||||
console.log('Bad response: ' + response.statusCode + body);
|
||||
callback(new Error('Bad response: ' + response.statusCode));
|
||||
callback(new AuthError(JSON.parse(body)));
|
||||
}
|
||||
else {
|
||||
var json = JSON.parse(body);
|
||||
|
|
Loading…
Reference in New Issue