From 72c499855cc31bc02274a89a6133a94f1915f68b Mon Sep 17 00:00:00 2001 From: Anon Ray Date: Tue, 4 Feb 2014 13:37:42 +0530 Subject: [PATCH] Testing out Persona code.. --- swtr/static/js/swtmaker.js | 36 +++++++++++++++++++++++++++++++----- swtr/templates/index.html | 3 ++- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/swtr/static/js/swtmaker.js b/swtr/static/js/swtmaker.js index b6b513b..ebc86b0 100644 --- a/swtr/static/js/swtmaker.js +++ b/swtr/static/js/swtmaker.js @@ -7,6 +7,13 @@ swtr.sweets = new ImgAnnoSwts(); swtr.appView = new AppView(); swtr.who = 'Guest'; + + $.ajaxSetup({ + xhrFields: { + withCredentials: true + }, + crossDomain: true + }); }; /* Model for Image Annotation Sweets */ @@ -269,7 +276,8 @@ }, getSignInCredentials: function(event) { event.preventDefault(); - if(swtr.who === 'Guest' && !$('#username').length) { + navigator.id.request(); + /*if(swtr.who === 'Guest' && !$('#username').length) { var template = _.template($('#signin-credentials-template').html()); $('#signin-msg').html(template()); } @@ -277,7 +285,7 @@ var username = $('#username').val(); var password = $('#password').val(); this.signIn(username, password); - } + }*/ return false; }, signIn: function(username, password) { @@ -302,6 +310,15 @@ } } }); + }, + userLoggedIn: function(userData) { + swtr.who = userData.username; + var text = 'You are signed in as ' + swtr.who+ ''; + $('#signinview').html(text); + }, + userLoggedOut: function() { + swtr.who = 'Guest'; + $('#signinview').html('Logged out'); } }); @@ -345,29 +362,38 @@ //swtr.AppView = AppView; - /*navigator.watch({ + // Persona callbacks + navigator.id.watch({ + //when an user logs in onlogin: function(assertion) { + //verify assertion and login the user $.ajax({ type: 'POST', url: swtr.swtstoreURL() + swtr.endpoints.login, data: {assertion: assertion}, - success: function() { + success: function(data) { + console.log('user logged in', data); + swtr.appView.userLoggedIn(data); }, error: function() { navigator.id.logout(); } }); }, + //when an user logs out onlogout: function() { $.ajax({ type: 'POST', + //data: {email: swtr.who}, url: swtr.swtstoreURL() + swtr.endpoints.logout, success: function() { + console.log('user logged out'); + //swtr.appView.userLoggedOut(); }, error: function() { } }); } - });*/ + }); })(swtr); diff --git a/swtr/templates/index.html b/swtr/templates/index.html index 38cf9e8..9e70721 100644 --- a/swtr/templates/index.html +++ b/swtr/templates/index.html @@ -53,7 +53,7 @@ window.swtr = window.swtr || {}; swtr.swtstoreURL = function() { return '{{ conf.swtstoreURL }}'; } swtr.endpoints = {'get': '/sweets/q', 'post': '/sweets', 'auth': - '/authenticate'}; + '/authenticate', 'login': '/auth/login', 'logout': '/auth/logout'}; window.onload = function() { swtr.init(); }; @@ -64,6 +64,7 @@ +