-
Notifications
You must be signed in to change notification settings - Fork 118
/
authentication.js
63 lines (54 loc) · 1.65 KB
/
authentication.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
import { AuthError, getBasicAuthHeaders, getSessionUrl, wrapError } from './utils';
import ajaxCore from 'pouchdb-ajax';
import { assign, toPromise } from 'pouchdb-utils';
var logIn = toPromise(function (username, password, opts, callback) {
var db = this;
if (typeof callback === 'undefined') {
callback = opts;
opts = {};
}
if (['http', 'https'].indexOf(db.type()) === -1) {
return callback(new AuthError('this plugin only works for the http/https adapter'));
}
if (!username) {
return callback(new AuthError('you must provide a username'));
} else if (!password) {
return callback(new AuthError('you must provide a password'));
}
var ajaxOpts = assign({
method: 'POST',
url: getSessionUrl(db),
headers: assign({'Content-Type': 'application/json'}, getBasicAuthHeaders(db)),
body: {name: username, password: password},
}, opts.ajax || {});
ajaxCore(ajaxOpts, wrapError(callback));
});
var logOut = toPromise(function (opts, callback) {
var db = this;
if (typeof callback === 'undefined') {
callback = opts;
opts = {};
}
var ajaxOpts = assign({
method: 'DELETE',
url: getSessionUrl(db),
headers: getBasicAuthHeaders(db),
}, opts.ajax || {});
ajaxCore(ajaxOpts, wrapError(callback));
});
var getSession = toPromise(function (opts, callback) {
var db = this;
if (typeof callback === 'undefined') {
callback = opts;
opts = {};
}
var url = getSessionUrl(db);
var ajaxOpts = assign({
method: 'GET',
url: url,
headers: getBasicAuthHeaders(db),
}, opts.ajax || {});
ajaxCore(ajaxOpts, wrapError(callback));
});
export { logIn, logOut, getSession };