2014-12-04 117 views
1

我可能是誤解了該方法的預期的行爲,但是這是我想使用它:在成功 - 用戶關閉CognitoCachingCredentialsProvider getCachedIdentityId是後空的應用程序關閉並重新打開

- 用戶登錄應用完全(在後臺關閉爲好) - 用戶再次打開應用程序,不必再次登錄,因爲CognitoCachingCredentialsProvider可以在設備上本地檢查,看看她仍然登錄

我試圖做到這一點是這樣的在被提示登錄之前檢查getCachedIdentityId()返回的內容。如果返回值不爲空,那意味着她仍然登錄,因爲沒有任何東西從設備中清除她的憑據。這是我的框架的樣子。我使用的開發者認證方法:

import java.util.HashMap; 
import java.util.Map; 
import java.util.concurrent.ExecutionException; 

import org.json.JSONException; 
import org.json.JSONObject; 

import android.content.Context; 
import android.util.Log; 

import com.amazonaws.auth.AWSAbstractCognitoIdentityProvider; 
import com.amazonaws.auth.CognitoCachingCredentialsProvider; 
import com.amazonaws.mobileconnectors.cognito.*; 
import com.amazonaws.regions.Regions; 

public class Util { 
    private final static String TAG = "Util"; 

    private static final String AWS_ACCOUNT_ID = {acct id}; 
    private static final String COGNITO_POOL_ID = {pool id}; 
    private static final String COGNITO_ROLE_AUTH = {auth arn}; 
    private static final String COGNITO_ROLE_UNAUTH = {unauth arn} 

    private static CognitoCachingCredentialsProvider sCredProvider; 
    private static UserIdentityProvider sIdProvider; 
    private static CognitoSyncManager sSyncManager; 

    private Util() { 
    } 

    public static CognitoCachingCredentialsProvider getCredProvider(
      Context context) { 
    if (sCredProvider == null) { 
     if (sIdProvider == null) { 
      CognitoCachingCredentialsProvider tmpProvider = new CognitoCachingCredentialsProvider(
        context.getApplicationContext(), AWS_ACCOUNT_ID, 
        COGNITO_POOL_ID, COGNITO_ROLE_UNAUTH, 
        COGNITO_ROLE_AUTH, Regions.US_EAST_1); 
      if (tmpProvider.getCachedIdentityId() != null) { 
       sCredProvider = tmpProvider; 
      } else { 
       sCredProvider = null; 
      } 
     } else { 
      sCredProvider = new CognitoCachingCredentialsProvider(
        context.getApplicationContext(), sIdProvider, 
        COGNITO_ROLE_UNAUTH, COGNITO_ROLE_AUTH); 
      Map logins = new HashMap(); 
      logins.put({Developer Provider Name}, sIdProvider.getToken()); 
      sCredProvider.setLogins(logins); 
     } 
    } 
    return sCredProvider; 
    } 

    public static UserIdentityProvider getIdentityProvider(Context context, 
      String email, String pwd) { 
     if (sIdProvider == null) { 
      sIdProvider = new UserIdentityProvider(AWS_ACCOUNT_ID, 
       COGNITO_POOL_ID, context.getApplicationContext()); 
     } 
     return sIdProvider; 
    } 

    public static boolean isLoggedIn(Context context) { 
     if (getCredProvider(context) == null) { 
      return false; 
     } 
     return true; 
    } 

    protected static class UserIdentityProvider extends 
      AWSAbstractCognitoIdentityProvider { 

     private Context context; 
     private String email; 
     private String password; 

     public UserIdentityProvider(String accountId, String identityPoolId, 
       Context c) { 
      super(accountId, identityPoolId); 
      context = c; 
      email = em; 
      password = pwd; 
     } 

     @Override 
     public String refresh() { 
      try { 
       ServerCommunicator server = new ServerCommunicator(context); 
       if (email != null && password != null) { 
    //this is a server call, which makes the call GetOpenIdTokenForDeveloperIdentityRequest after I authenticate the user and send AWS my user's token 
        String response = server.initUserLoginAsyncTask() 
          .execute(email, password).get(); 
        prefs.setAllUserSharedPrefs(response); 
        JSONObject responseJSON = new JSONObject(response); 
        String identityId = responseJSON.getString("id"); 
        String token = responseJSON.getString("token"); 
        if (token != null && identityId != null) { 
         this.setToken(token); 
         this.setIdentityId(identityId); 
         update(identityId, token); 
         return token; 
       } 
      } 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } catch (ExecutionException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     public String getProviderName() { 
      return {Developer Provider Name}; 
     } 

    } 

} 

我只需調用從這個類的isLoggedIn方法,看看是否有本地存儲的IdentityId。但是,這不符合預期。我可以從調試的過程中getCachedIdentityId總是空,我總是提示每當我打開應用程序已被關閉後重新登錄(初始化CognitoCachingCredentialsProvider並添加標記到地圖登錄後甚至直接)看到。 IdentityId何時實際存儲在本地,並且一般情況下我的邏輯是正確的?

附加代碼

import java.util.concurrent.ExecutionException; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
public class LoginActivity extends Activity { 

private final String TAG = "LoginActivity"; 

private EditText etEmail, etPwd; 
private Button bLogin, bGoToRegister; 
private ServerCommunicator server; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.i(TAG, "onCreate"); 
    server = new ServerCommunicator(this); 
    if (Util.isLoggedIn(this)) { 
     Intent intent = new Intent(this, MainActivity.class); 
     startActivity(intent); 
     return; 
    } 
    this.setContentView(R.layout.activity_login); 
    etEmail = (EditText) findViewById(R.id.etEmail); 
    etPwd = (EditText) findViewById(R.id.etPassword); 
    bLogin = (Button) findViewById(R.id.bLogin); 
    bGoToRegister = (Button) findViewById(R.id.bGoToRegister); 
    bLogin.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      String email = etEmail.getText().toString(); 
      String pwd = etPwd.getText().toString(); 
      Util.getIdentityProvider(v.getContext()).setEmail(email); 
      Util.getIdentityProvider(v.getContext()).setPassword(pwd); 
      String token = Util.getIdentityProvider(v.getContext()).refresh(); 
      if (token != null) { 
       Intent intent = new Intent(v.getContext(), MainActivity.class); 
       startActivity(intent); 
      } else { 
       Toast.makeText(v.getContext(), "Invalid username/password", 
       Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
} 
} 

以上是我LoginActivity。當應用程序啓動在MainActivity,我有下面的代碼片段在我的onCreate方法的開頭:

if (!Util.isLoggedIn(this)) { 
     Intent intent = new Intent(this, LoginActivity.class); 
     startActivity(intent); 
} 

這個調用初始化CognitoCachingCredentialsProvider。我認爲這將是IdentityId獲取緩存時的情況,但我的調試發現即使在此塊之後,getCachedIdentityId()仍然返回null。我的基礎是如何嘗試使用這個課程?

+0

你可以提供更多的代碼上下文嗎?具體來說,刷新的位置是由您的身份提供者在此生命週期中調用的? – 2014-12-04 23:28:17

+0

@JeffBailey您好,感謝您的關注。看到我的編輯與額外的代碼。讓我知道如果還有什麼我應該提供的,以幫助進一步澄清這一點。 – ethan123 2014-12-04 23:41:38

回答

1

我有一個建議。 CognitoCachingCredentialsProvider是在身份標識更改時保存的標識。但是,它不會開始監聽,直到它被初始化,並且更新發生在對您的身份提供者的刷新調用中。

你可以嘗試刷新調用之前CognitoCachingCredentialsProvider的初始化移動(但你的身份提供初始化之後)?

編輯:

更新將設置identityId並提出只是事前的道理,但是明確的調用可能會導致它認爲沒有變化正在取得進展。你能否嘗試消除setter電話?

+0

我覺得這是你的意思: Util.getIdentityProvider(v.getContext())setEmail(電子郵件); \t \t \t \t Util.getIdentityProvider(v.getContext())setPassword(PWD)。 \t \t \t \t Util.getCredProvider(V。的getContext()); \t \t \t \t String token = Util.getIdentityProvider(v.getContext())。refresh(); ...但沒有奏效。儘管如此,我認爲這不重要。如果我設置了登錄名,那麼我認爲這需要緩存id。任何其他想法,我如何能夠完成我想要完成的事情? – ethan123 2014-12-04 23:53:51

+0

是的,我之前評論中描述的編輯應該執行您的建議。前兩行靜態初始化我的身份提供者。然後我初始化憑證提供程序,然後在身份提供程序上調用刷新。這是你的意思嗎?這也沒有做我想做的事情。 – ethan123 2014-12-04 23:59:48

+0

你是什麼意思,這不是你想要做的?此外,只有在捕獲到身份更改事件時纔會對該ID進行高速緩存,並且只有在初始化憑據提供程序後纔會初始化該事件的偵聽器。基本上,它應該很重要。 – 2014-12-05 00:09:27

相關問題