第一目的
Android アプリで Cognito ユーザー プールと Cognito Federated Identities を使用してログインを永続化します。アプリはトークンの更新を処理する必要があります (sdk 経由?)。メールがユーザー名として使用されます。アプリケーション全体で 1 つの一意のユーザー名 (電子メール) のみを許可します。例: ユーザーが test@gmail.com の電子メール/ユーザー名で Facebook にログインする場合、ユーザープールを介してtest@gmail.comでユーザーを登録することはできません。
設定
Cognito ユーザープール、Facebook、および Google である認証プロバイダーで作成された ID プールがあります。Facebook と Google のネイティブ SDK を使用してサインインしています。
ユーザーがログインしているかどうかを確認し、それに応じてリダイレクトする SplashScreenActivity があります (LoginActivity または MainActivity)
currentUser.getSession(authenticationHandler)
ユーザープール経由でログインしている場合、ユーザープールを使用しても問題なく動作します。
初めて
- ログインしていない
- AuthenticationHandler:getAuthenticationDetails:userId が null です
- ログイン アクティビティを表示
ログイン
- ユーザープール経由でログイン
- 成功 -> MainActivity
アプリを再起動
- AuthenticationHandler:onSuccess
- MainActivity を表示
Facebook 経由でログインし、CognitoCachingCredentialsProvider
更新しないでログインを設定するとcurrentUser
、getSession()
通話が機能しません。ただし、私ができることは、 を呼び出すことだけですgetCachedIdentityId()
。私の有効期限はありませんCognitoCachingCredentialsProvider
か?
さらに、ユーザープールから作成されたユーザーでログインしてログアウトcurrentUser.getUserId()
すると、以前にログインしたユーザープールユーザーで戻りますが、getCachedIdentityId()
null です
private void logout() {
EasyPrefs.clear(this); //clear shared preferences for local variables
authHelper.getCredentialsProvider().clear(); //clear cached CognitoCredentialsProvider
CognitoUser currentUser = AuthHelper.getInstance().getUserPool().getCurrentUser();
currentUser.signOut(); //this is pointless if I login with federated identity
//TODO: Logout of Google?
//TODO: Logout of Facebook?
//assume logout was successful?
startActivity(new Intent(this, SplashScreenActivity.class));
finish();
}
質問
- ユーザープール、Facebook、Google を使用してネイティブ ログインを処理しているため、アクセス トークンとリフレッシュ トークンを手動で管理する必要がありますか? または
CognitoCachingCredentialsProvider
、私のためにそれを処理できますか? - ユーザーを (ユーザープール、Facebook、または Google 経由で) ログインして、ログインしたままにすることはできますか? また、どのように確認すればよいですか?
- ユーザーが facebook/google 経由でログインするときに、ユーザー プールにユーザーをネイティブに作成できますか?
最終暴言
Firebase Auth を使用したことがある場合は、このようにアプリケーションを機能させたいと考えています。Firebase Users、Facebook ログイン、Google ログインを使用して Firebase Auth を設定するのは非常に簡単です。
- ID プロバイダーでログイン
- トークンを Firebase に渡す
- Firebase ユーザーを取得する
- 終わり。
ログアウトしますか?firebaseUser.logout()。そのような単純な。
コード
スプラッシュスクリーン アクティビティ
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
authHelper = AuthHelper.getInstance();
authHelper.init(this);
//Check if a cached identity exists?
//This will return an identity if user is user pool, facebook, google
String cachedIdentityId = authHelper.getCredentialsProvider().getCachedIdentityId();
Logger.d(TAG, cachedIdentityId == null ? "cachedIdentityId is null" : cachedIdentityId);
//This is never null unless app data has been cleared?
CognitoUser currentUser = authHelper.getUserPool().getCurrentUser();
//Even if I call currentUser.signOut(), this still returns a userId
String cachedUserId = currentUser.getUserId(); //because aws sucks
Logger.d(TAG, cachedUserId == null ? "cachedUserId is null" : cachedUserId);
//if user pool user is signed in, this will goto MainActivity
currentUser.getSession(authenticationHandler);
//not doing anything with cachedIdentityId because....?
}
private AuthenticationHandler authenticationHandler = new AuthenticationHandler() {
@Override
public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
//wait a few seconds, then goto LoginActivity
handler.postDelayed(timerTask, SECONDS * 2);
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
}
@Override
public void authenticationChallenge(ChallengeContinuation continuation) {
}
@Override
public void onFailure(Exception exception) {
Logger.e(TAG, AuthHelper.formatException(exception));
//wait a few seconds, then goto LoginActivity
handler.postDelayed(timerTask, SECONDS * 2);
}
};
ログイン アクティビティ
各ログイン シナリオをネイティブに処理します (ユーザー プール、Facebook、Google)
private void setLogins(String key, String token) {
Map<String, String> logins = new HashMap<>();
logins.put(key, token);
authHelper.getCredentialsProvider().setLogins(logins);
new RefreshCognitoCredentials().execute();
}
private class RefreshCognitoCredentials extends AsyncTask<Void, Void, String> {
@SuppressLint("LongLogTag")
@Override
protected String doInBackground(Void... voids) {
Map<String, String> logins = authHelper.getCredentialsProvider().getLogins();
for(String key : logins.keySet()) {
Logger.d(TAG, key + " - " + logins.get(key));
}
try {
authHelper.getCredentialsProvider().refresh();
} catch(NotAuthorizedException exception) {
authHelper.getCredentialsProvider().clear();
return null;
}
return authHelper.getCredentialsProvider().getIdentityId();
}
@SuppressLint("LongLogTag")
@Override
protected void onPostExecute(String response) {
if(response == null) {
Toast.makeText(getApplicationContext(), "Logins don't match. Please include at least one valid login for this identity or identity pool.", Toast.LENGTH_LONG).show();
return;
}
Logger.d(TAG, response);
startActivity(new Intent(AuthActivity.this, MainActivity.class));
finish();
}
}