1
- import { Buffer } from 'buffer' ;
2
-
3
1
const service = '/api' ;
4
2
5
3
function Hyperglosae ( logger ) {
6
4
7
- this . credentials = { } ;
8
-
9
5
this . getView = ( { view, id, options = [ ] } ) =>
10
6
fetch ( `${
11
7
service
@@ -23,18 +19,9 @@ function Hyperglosae(logger) {
23
19
fetch ( `${ service } /${ id } ` )
24
20
. then ( x => x . json ( ) ) ;
25
21
26
- let basicAuthentication = ( { force} ) => {
27
- let { name, password} = this . credentials ;
28
- if ( ! force && ! name && ! password ) return ( { } ) ;
29
- return ( {
30
- 'Authorization' : 'Basic ' + Buffer . from ( `${ name } :${ password } ` ) . toString ( 'base64' )
31
- } ) ;
32
- } ;
33
-
34
22
this . putDocument = ( doc , uri ) =>
35
23
fetch ( `${ service } /${ uri || doc . _id } ` , {
36
24
method : 'PUT' ,
37
- headers : basicAuthentication ( { force : false } ) ,
38
25
body : JSON . stringify ( doc )
39
26
} )
40
27
. then ( x => x . json ( ) )
@@ -49,7 +36,6 @@ function Hyperglosae(logger) {
49
36
this . deleteDocument = ( { _id, _rev} ) =>
50
37
fetch ( `${ service } /${ _id } ?rev=${ _rev } ` , {
51
38
method : 'DELETE' ,
52
- headers : basicAuthentication ( { force : false } )
53
39
} )
54
40
. then ( x => x . json ( ) )
55
41
. then ( x => {
@@ -63,7 +49,6 @@ function Hyperglosae(logger) {
63
49
this . getDocumentMetadata = ( id ) =>
64
50
fetch ( `${ service } /${ id } ` , {
65
51
method : 'HEAD' ,
66
- headers : basicAuthentication ( { force : false } )
67
52
} ) ;
68
53
69
54
this . putAttachment = ( id , attachment , callback ) =>
@@ -76,7 +61,6 @@ function Hyperglosae(logger) {
76
61
fetch ( `${ service } /${ id } /${ attachment . name } ` , {
77
62
method : 'PUT' ,
78
63
headers : {
79
- ...basicAuthentication ( { force : false } ) ,
80
64
// ETag is the header that carries the current rev.
81
65
'If-Match' : x . headers . get ( 'ETag' ) ,
82
66
'Content-Type' : attachment . type
@@ -86,19 +70,34 @@ function Hyperglosae(logger) {
86
70
} ;
87
71
} ) ;
88
72
89
- this . authenticate = ( { name, password} ) => {
90
- this . credentials = { name, password} ;
91
- return fetch ( `${ service } ` , {
92
- method : 'GET' ,
93
- headers : basicAuthentication ( { force : true } )
73
+ this . getSession = ( ) =>
74
+ fetch ( `${ service } /_session` )
75
+ . then ( x => x . json ( ) )
76
+ . then ( x => {
77
+ this . user = x . userCtx ?. name ;
78
+ return this . user ;
79
+ } ) ;
80
+
81
+ this . postSession = ( { name, password} ) =>
82
+ fetch ( `${ service } /_session` , {
83
+ method : 'POST' ,
84
+ headers : {
85
+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
86
+ } ,
87
+ body : `name=${ name } &password=${ password } `
94
88
} )
95
89
. then ( x => x . json ( ) )
96
90
. then ( x => {
97
- if ( x . reason ) {
98
- this . credentials = { } ;
99
- logger ( x . reason ) ;
100
- throw new Error ( x . reason ) ;
101
- }
91
+ if ( ! x . reason ) return this . user = name ;
92
+ logger ( x . reason ) ;
93
+ } ) ;
94
+
95
+ this . deleteSession = ( ) => {
96
+ fetch ( `${ service } /_session` , {
97
+ method : 'DELETE'
98
+ } )
99
+ . then ( ( ) => {
100
+ this . user = null ;
102
101
} ) ;
103
102
} ;
104
103
@@ -125,7 +124,7 @@ function Hyperglosae(logger) {
125
124
} ;
126
125
127
126
this . refreshDocuments = ( callback ) => {
128
- let id = this . credentials . name || 'PUBLIC' ;
127
+ let id = this . user || 'PUBLIC' ;
129
128
this . getView ( { view : 'all_documents' , id, options : [ 'include_docs' ] } )
130
129
. then ( ( rows ) => {
131
130
callback (
0 commit comments