Skip to content

Instantly share code, notes, and snippets.

@Barnabas
Last active December 10, 2015 05:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Barnabas/4386183 to your computer and use it in GitHub Desktop.
Save Barnabas/4386183 to your computer and use it in GitHub Desktop.
Persona Express AngularJS module
html(ng-app="persona")
body(ng-controller="PersonaCtrl")
div(ng-hide="verified")
button(ng-click="verify()") Verify Identity
div(ng-show="verified")
| Hello {{email}}
br
button(ng-click="logout()") Log Out
div(ng-show="error")
strong {{error}}
script(src="https://login.persona.org/include.js")
script(src="angular.js")
script(src="persona.js")
/*
Sample authorize routes to support persona.js client library
*/
exports.setup = function(app) {
app.get("/auth", auth);
app.get("/logout", logout);
app.post("/persona/status", personaStatus);
};
function isAuthorized(req) {
var email = req.session.email || false;
var authorized = false;
// todo: verify in DB
if(email == "your-email-here@example.com") {
authorized = true;
}
req.session.authorized = authorized;
return {email: email, authorized: authorized};
}
function personaStatus(req, res) {
res.json(isAuthorized(req));
}
function auth(req, res) {
res.render("auth", isAuthorized(req));
}
function logout(req, res) {
req.session = null;
res.redirect("/auth");
}
/* Persona Express AngularJS module
* ------------------------------------------------------------------
* This is glue for the following:
* Node http://nodejs.org
* Express http://expressjs.com/
* AngularJS http://angularjs.org
* Mozilla Persona http://www.mozilla.org/en-US/persona/
* express-persona http://jbuck.github.com/express-persona/
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The F*** You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
* Please don't sue: Barnabas Kendall (barnabas.kendall@gmail.com)
*
* This library assumes express-persona default routes,
* persona/verify and persona/logout.
* In addition to these, your app should expose another route:
* persona/status, which should return
* {email: "me@example.com"}
* if verified or
* {email: false}
* if not verified.
*/
angular.module('persona', []);
angular.module("persona").factory("personaSvc", ["$http", "$q", function ($http, $q) {
return {
verify:function () {
var deferred = $q.defer();
navigator.id.get(function (assertion) {
$http.post("/persona/verify", {assertion:assertion})
.then(function (response) {
if (response.data.status != "okay") {
deferred.reject(response.data.reason);
} else {
deferred.resolve(response.data.email);
}
});
});
return deferred.promise;
},
logout:function () {
return $http.post("/persona/logout").then(function (response) {
if (response.data.status != "okay") {
$q.reject(response.data.reason);
}
return response.data.email;
});
},
status:function () {
return $http.post("/persona/status").then(function (response) {
return response.data;
});
}
};
}]);
// Persona controller; exposes login and logout methods, set state for UI
function PersonaCtrl($scope, personaSvc) {
// initialize properties
angular.extend($scope, { verified:false, error:false, email:"" });
$scope.verify = function () {
personaSvc.verify().then(function (email) {
angular.extend($scope, { verified:true, error:false, email:email });
$scope.status();
}, function (err) {
angular.extend($scope, { verified:false, error:err});
});
};
$scope.logout = function () {
personaSvc.logout().then(function () {
angular.extend($scope, { verified:false, error:false});
}, function (err) {
$scope.error = err;
});
};
$scope.status = function () {
personaSvc.status().then(function (data) {
// in addition to email, everything else returned by persona/status will be added to the scope
// this could be the chance to expose data from your local DB, for example
angular.extend($scope, data, { error:false, verified:!!data.email, email:data.email });
}, function (err) {
$scope.error = err;
});
};
// setup; check status once on init
$scope.status();
}
PersonaCtrl.$inject = ["$scope", "personaSvc"];
@apple314159
Copy link

The gist didn't work properly for me. The reason is that the $http.post call in the navigator.id.get inside the verify is not part of an angular context. I had to switch to using XMLHttpRequest and when it finishes, use a $broadcast to notify the controller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment