@mkeen/rxcouch
Version:
Real Time RxJs Based CouchDB Client
178 lines • 8.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var rxhttp_1 = require("@mkeen/rxhttp");
var types_1 = require("./types");
var CouchSession = /** @class */ (function () {
function CouchSession(authorizationBehavior, sessionUrl, credentials) {
var _this = this;
if (sessionUrl === void 0) { sessionUrl = ''; }
this.authorizationBehavior = authorizationBehavior;
this.sessionUrl = sessionUrl;
this.credentials = credentials;
this.authenticated = new rxjs_1.BehaviorSubject(false);
this.loginAttemptMade = new rxjs_1.BehaviorSubject(false);
this.userSession = new rxjs_1.BehaviorSubject(null);
this.cookie = new rxjs_1.BehaviorSubject('');
this.context = new rxjs_1.BehaviorSubject(null);
this.saveCookie = function (httpResponse) {
var headers = httpResponse.headers;
if (typeof process === 'object') {
var cookie = headers.get('set-cookie');
if (cookie) {
_this.cookie.next(cookie);
}
}
};
this.extractResponse = function (httpResponse) {
return httpResponse.response;
};
if (this.credentials) {
this.credentials.subscribe(function (couchDbCreds) {
_this.authenticate(couchDbCreds).pipe(operators_1.take(1)).subscribe(function (_authSuccess) {
_this.authenticated.next(_this.authenticated.value);
}, function (_error) {
if (!!_this.authenticated.value) {
_this.authenticated.next(false);
}
});
});
}
}
CouchSession.prototype.authenticate = function (providedCredentials) {
var _this = this;
return rxjs_1.Observable.create(function (observer) {
if (_this.authorizationBehavior === types_1.AuthorizationBehavior.cookie) {
if (providedCredentials) {
_this.lastCredentials = providedCredentials;
var username = providedCredentials.username, password = providedCredentials.password;
_this.attemptNewAuthentication(username, password).pipe(operators_1.take(1)).subscribe(function (authResponse) {
if (!_this.loginAttemptMade.value) {
_this.loginAttemptMade.next(true);
}
if (_this.authenticated.value !== true) {
_this.authenticated.next(true);
}
delete authResponse.ok;
_this.context.next(authResponse);
observer.next(true);
}, function (_error) {
if (!_this.loginAttemptMade.value) {
_this.loginAttemptMade.next(true);
}
console.warn(_error);
_this.context.next(null);
observer.error(false);
observer.complete();
});
}
else {
if (_this.loginAttemptMade.value === false) {
_this.get().pipe(operators_1.take(1)).subscribe(function (session) {
var ok = session.ok, userCtx = session.userCtx;
var authenticated = !!userCtx.name;
if (_this.authenticated.value !== authenticated) {
_this.authenticated.next(authenticated);
if (ok) {
_this.context.next(session.userCtx);
observer.next(true);
}
else {
_this.context.next(null);
observer.error(false);
}
}
else {
_this.context.next(null);
observer.error(false);
}
});
}
else {
_this.context.next(null);
observer.error(false);
}
}
}
});
};
CouchSession.prototype.reauthenticate = function () {
return this.authenticate(this.lastCredentials);
};
CouchSession.prototype.get = function () {
var _this = this;
return rxjs_1.Observable.create(function (observer) {
_this.httpRequest(_this.sessionUrl, rxhttp_1.FetchBehavior.simpleWithHeaders).fetch().pipe(operators_1.tap(_this.saveCookie), operators_1.map(_this.extractResponse)).subscribe(function (response) {
if (!_this.loginAttemptMade.value) {
_this.loginAttemptMade.next(true);
}
if (response.ok && response.info.authenticated) {
_this.context.next(response.userCtx);
if (!_this.authenticated.value) {
_this.authenticated.next(true);
}
}
else {
_this.context.next(null);
if (!!_this.authenticated.value) {
_this.authenticated.next(false);
}
}
observer.next(response);
}, function (err) {
observer.error(err);
});
});
};
CouchSession.prototype.attemptNewAuthentication = function (username, password) {
var _this = this;
return this.httpRequest(this.sessionUrl, rxhttp_1.FetchBehavior.simpleWithHeaders, 'POST', JSON.stringify({
'username': username,
'password': password
})).fetch().pipe(operators_1.tap(this.saveCookie), operators_1.map(this.extractResponse), operators_1.tap(function (_response) {
_this.loginAttemptMade.next(true);
}));
};
CouchSession.prototype.httpRequest = function (url, behavior, method, body) {
if (behavior === void 0) { behavior = rxhttp_1.FetchBehavior.simpleWithHeaders; }
if (method === void 0) { method = 'GET'; }
if (body === void 0) { body = undefined; }
var _a;
return new rxhttp_1.HttpRequest(url, this.httpRequestOptions((_a = this.cookie) === null || _a === void 0 ? void 0 : _a.value, method, body), behavior);
};
CouchSession.prototype.httpRequestOptions = function (cookie, method, body) {
var httpOptions = {
method: method
};
if (body) {
httpOptions.body = body;
}
if (cookie !== null) {
if (cookie.length && typeof process === 'object') { // Todo: Type hint and length check really necessary?
httpOptions['headers'] = {
'Cookie': this.cookieForRequestHeader(cookie) // Todo: Why is type hint needed when inside the null check?
};
}
}
return httpOptions;
};
CouchSession.prototype.cookieForRequestHeader = function (cookie) {
return cookie.split(';')[0].trim();
};
CouchSession.prototype.destroy = function () {
var _this = this;
return rxjs_1.Observable.create(function (observer) {
_this.httpRequest(_this.sessionUrl, rxhttp_1.FetchBehavior.simple, 'delete').fetch().subscribe(function (response) {
if (response.ok) {
_this.authenticated.next(false);
_this.cookie.next('');
}
observer.next(response);
});
});
};
return CouchSession;
}());
exports.CouchSession = CouchSession;
//# sourceMappingURL=couchsession.js.map