Skip to content

Commit 9b8ff3d

Browse files
committed
Started persona instegration
1 parent bd6cf2c commit 9b8ff3d

File tree

4 files changed

+324
-182
lines changed

4 files changed

+324
-182
lines changed

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ <h1>todos</h1>
3232
<p>Created by <a href="http://twitter.com/ffesseler">Florian Fesseler</a></p>
3333
<p>Cleanup, edits by <a href="http://github.com/boushley">Aaron Boushley</a></p>
3434
</footer>
35+
<img src="style/plain_sign_in_blue.png" id="signin" />
36+
<img src="style/plain_sign_in_blue.png" id="signout" style="display:none" />
37+
<script src="https://login.persona.org/include.js"></script>
3538
<script src="js/pouchdb-nightly.js"></script>
3639
<script src="js/base.js"></script>
3740
<script src="js/app.js"></script>

js/app.js

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var db = new Pouch('todos');
1212
var remoteCouch = false;
13+
var cookie;
1314

1415
db.info(function(err, info) {
1516
db.changes({since: info.update_seq, onChange: showTodos, continuous: true});
@@ -60,8 +61,15 @@
6061
// Initialise a sync with the remote server
6162
function sync() {
6263
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+
});
6573
}
6674

6775
// EDITING STARTS HERE (you dont need to edit anything below this line)
@@ -156,4 +164,64 @@
156164
sync();
157165
}
158166

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

Comments
 (0)