我在一个项目中使用Mozilla Persona。我想在onlogin之后更新loggedInUser。但loggedInUser是传递给navigator.id.watch()的对象的属性。(在AngularJS服务中)只调用了一次navigator.id.watch()。我应该再次调用它,传递完整的对象吗?看起来不太对劲。我说错了吗?
这是我的服务:
app.factory('persona', function ($rootScope, $http) {
navigator.id.watch({
loggedInUser: null,
onlogin: function onlogin(assertion) {
console.log(this);
$http.post('/signIn', { assertion: assertion })
.then(function (data, status, headers, config) {
$rootScope.$broadcast('signIn', data.data);
}, function (data, status, headers, config) {
$rootScope.$broadcast('signInError', data.data);
});
},
onlogout: function onlogout(param) {
$http.get('/signOut')
.then(function (data, status, headers, config) {
$rootScope.$broadcast('signOut', data.data);
}, function (data, status, headers, config) {
$rootScope.$broadcast('signOutError', data.data);
});
}
});
return {
signIn: function signIn() {
navigator.id.request();
},
signOut: function signOut() {
navigator.id.logout();
}
};
});发布于 2013-04-02 08:34:13
您不能像MDN示例一样,在与navigator.id.watch方法相同的作用域下使loggedInUser成为全局的,或者至少是“局部全局的”吗?
之后,您可以从Persona服务获得JSON响应,其中包含一些数据,包括电子邮件。这样您就可以在AJAX响应上传递数据并填充loggedInUser变量
var currentUser = 'bob@example.com';
navigator.id.watch({
loggedInUser: currentUser,
onlogin: function(assertion) {
$.ajax({
type: 'POST',
url: '/auth/login', // This is a URL on your website.
data: {assertion: assertion},
success: function(res, status, xhr) { window.location.reload(); },
error: function(xhr, status, err) {
navigator.id.logout();
alert("Login failure: " + err);
}
});
},
onlogout: function() {
$.ajax({
type: 'POST',
url: '/auth/logout', // This is a URL on your website.
success: function(res, status, xhr) { window.location.reload(); },
error: function(xhr, status, err) { alert("Logout failure: " + err); }
});
}
});来自MDN的JSON响应示例:
{
"status": "okay",
"email": "bob@eyedee.me",
"audience": "https://example.com:443",
"expires": 1308859352261,
"issuer": "eyedee.me"
}发布于 2014-02-06 00:25:00
在navigator.id.watch调用中,设置loggedInUser: localStorage.getItem('persona') || null ( null很重要),然后,当Persona登录成功时,执行localStorage.setItem('persona', theUserEmail),当失败时,执行localStorage.removeItem('persona')。
https://stackoverflow.com/questions/15227451
复制相似问题