2013-03-05 1 views
6

私はプロジェクトでMozilla Personaを使用しています。 onloginの後にloggedInUserを更新したいと思います。しかしloggedInUserは、navigator.id.watch()に渡されるオブジェクトの属性です。 navigator.id.watch()が(AngularJSサービスで)1回呼び出されました。 もう一度呼び出すと、オブジェクト全体を渡す必要がありますか?それは正しいとは思わない。私が間違っている?同じように、Mozilla Personaでonlogin後にloggedInUserを更新するには

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(); 
    } 
}; 
}); 
+1

上記の 'app.factory()'コールでは、現在ログインしているユーザの電子メールアドレスを検索し、それを渡すことができますか? 通常、 'navigator.id.watch()'を呼び出すたびに、バックエンドから現在ログインしているユーザの電子メールアドレスを取得できるようにします。 –

答えて

3

はないあなただけのloggedInUserがあなたのnavigator.id.watch方法と同じスコープの下にグローバルまたは少なくとも「ローカルグローバル」になることはできます:ここで

= P

は私のサービスですMDNの例?

その後、電子メールを含むいくつかのデータを含むPersonaサービスからJSON応答を得ることができます。つまり、あなたのAJAX応答にそのデータを渡し、MDNからloggedInUser変数

https://developer.mozilla.org/en-US/docs/Persona/Quick_Setup#Step_3.3A_Watch_for_login_and_logout_actions

var currentUser = '[email protected]'; 

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); } 
    }); 
    } 
}); 

JSON応答のサンプルを埋めることができます:navigator.id.watchコールで

{ 
    "status": "okay", 
    "email": "[email protected]", 
    "audience": "https://example.com:443", 
    "expires": 1308859352261, 
    "issuer": "eyedee.me" 
} 
+1

私は前進しなければならなかったので、私はそれを最善を尽くす方法がまだ分かりませんが、あなたの答え@chambsのようなものでしょう。 私の状況の「ワンページアプリ」なので、混乱しているようです。私は最終的にそれに戻ってきます...ありがとう! – slacktracer

0

を、loggedInUser: localStorage.getItem('persona') || nullnullを設定しました重要です)、Personaログインが成功すると、localStorage.setItem('persona', theUserEmail)を実行し、失敗した場合はlocalStorage.removeItem('persona')を実行します。