10
10
11
11
var db = new Pouch ( 'todos' ) ;
12
12
var remoteCouch = false ;
13
+ var cookie ;
13
14
14
15
db . info ( function ( err , info ) {
15
16
db . changes ( { since : info . update_seq , onChange : showTodos , continuous : true } ) ;
60
61
// Initialise a sync with the remote server
61
62
function sync ( ) {
62
63
syncDom . setAttribute ( 'data-sync-state' , 'syncing' ) ;
63
- var pushRep = db . replicate . to ( remoteCouch , { continuous : true , complete : syncError } ) ;
64
- var pullRep = db . replicate . from ( remoteCouch , { continuous : true , complete : syncError } ) ;
64
+ var remote = new PouchDB ( remoteCouch , { headers : { 'Cookie' : cookie } } ) ;
65
+ var pushRep = db . replicate . to ( remote , {
66
+ continuous : true ,
67
+ complete : syncError
68
+ } ) ;
69
+ var pullRep = db . replicate . from ( remote , {
70
+ continuous : true ,
71
+ complete : syncError
72
+ } ) ;
65
73
}
66
74
67
75
// EDITING STARTS HERE (you dont need to edit anything below this line)
156
164
sync ( ) ;
157
165
}
158
166
159
- } ) ( ) ;
167
+ // Host that the couch-persona server is running on
168
+ var authHost = 'http://127.0.0.1:3000' ;
169
+
170
+ var loggedIn = function ( result ) {
171
+ console . log ( 'logged in:' , result ) ;
172
+ remoteCouch = result . dbUrl ;
173
+ cookie = result . authToken . replace ( 'HttpOnly' , '' ) ;
174
+ sync ( ) ;
175
+ } ;
176
+
177
+ var loggedOut = function ( ) {
178
+ console . log ( 'logged out!' ) ;
179
+ } ;
180
+
181
+ function simpleXhrSentinel ( xhr ) {
182
+ return function ( ) {
183
+ if ( xhr . readyState !== 4 ) {
184
+ return ;
185
+ }
186
+ if ( xhr . status == 200 ) {
187
+ var result = { } ;
188
+ try {
189
+ result = JSON . parse ( xhr . responseText ) ;
190
+ } catch ( e ) { }
191
+ loggedIn ( result ) ;
192
+ } else {
193
+ navigator . id . logout ( ) ;
194
+ loggedOut ( ) ;
195
+ }
196
+ } ;
197
+ }
198
+
199
+ function verifyAssertion ( assertion ) {
200
+ var xhr = new XMLHttpRequest ( ) ;
201
+ var param = 'assert=' + assertion ;
202
+ xhr . open ( 'POST' , authHost + '/persona/sign-in' , true ) ;
203
+ xhr . setRequestHeader ( "Content-type" , "application/x-www-form-urlencoded" ) ;
204
+ xhr . setRequestHeader ( "Content-length" , param . length ) ;
205
+ xhr . setRequestHeader ( "Connection" , "close" ) ;
206
+ xhr . send ( param ) ;
207
+ xhr . onreadystatechange = simpleXhrSentinel ( xhr ) ;
208
+ }
209
+
210
+ function signoutUser ( ) {
211
+ var xhr = new XMLHttpRequest ( ) ;
212
+ xhr . open ( "POST" , authHost + '/persona/sign-out' , true ) ;
213
+ xhr . send ( null ) ;
214
+ xhr . onreadystatechange = simpleXhrSentinel ( xhr ) ;
215
+ }
216
+
217
+ navigator . id . watch ( {
218
+ onlogin : verifyAssertion ,
219
+ onlogout : signoutUser
220
+ } ) ;
221
+
222
+ var signinLink = document . getElementById ( 'signin' ) ;
223
+ var signoutLink = document . getElementById ( 'signout' ) ;
224
+ signinLink . onclick = function ( ) { navigator . id . request ( ) ; } ;
225
+ signoutLink . onclick = function ( ) { navigator . id . logout ( ) ; } ;
226
+
227
+ } ) ( ) ;
0 commit comments