Need to configure your backend?See Build a Backend →
ユーザーセッションの管理
Amplify Authは、現在のユーザーセッションとトークンへのアクセスを提供し、ユーザー情報を取得して、有効なセッションでサインインしているかどうかを判断し、アプリケーションへのアクセスを制御するのに役立ちます。
Amplify Authの意図的な決定は、認証情報を公開したり操作したりするパブリックメソッドを避けることでした。
Authを使用すると、サインインするだけで、認証情報を最新の状態に保ち、他のカテゴリに提供するために必要なすべてが処理されます。
ただし、Amplify外のAPIを操作する場合や、AWS固有の識別情報 (例: IdentityId) にアクセスしたい場合は、以下のように fetchAuthSession の結果をキャストすることで、これらの実装詳細にアクセスできます。
Amplify.Auth.fetchAuthSession( result -> { AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result; switch(cognitoAuthSession.getIdentityIdResult().getType()) { case SUCCESS: Log.i("AuthQuickStart", "IdentityId: " + cognitoAuthSession.getIdentityIdResult().getValue()); break; case FAILURE: Log.i("AuthQuickStart", "IdentityId not present because: " + cognitoAuthSession.getIdentityIdResult().getError().toString()); } }, error -> Log.e("AuthQuickStart", error.toString()));Amplify.Auth.fetchAuthSession( { val session = it as AWSCognitoAuthSession when (session.identityIdResult.type) { AuthSessionResult.Type.SUCCESS -> Log.i("AuthQuickStart", "IdentityId = ${session.identityIdResult.value}") AuthSessionResult.Type.FAILURE -> Log.w("AuthQuickStart", "IdentityId not found", session.identityIdResult.error) } }, { Log.e("AuthQuickStart", "Failed to fetch session", it) })try { val session = Amplify.Auth.fetchAuthSession() as AWSCognitoAuthSession val id = session.identityIdResult if (id.type == AuthSessionResult.Type.SUCCESS) { Log.i("AuthQuickStart", "IdentityId: ${id.value}") } else if (id.type == AuthSessionResult.Type.FAILURE) { Log.i("AuthQuickStart", "IdentityId not present: ${id.error}") }} catch (error: AuthException) { Log.e("AuthQuickStart", "Failed to fetch session", error)}RxAmplify.Auth.fetchAuthSession() .subscribe( result -> { AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
switch (cognitoAuthSession.getIdentityIdResult().getType()) { case SUCCESS: Log.i("AuthQuickStart", "IdentityId: " + cognitoAuthSession.getIdentityIdResult().getValue()); break; case FAILURE: Log.i("AuthQuickStart", "IdentityId not present because: " + cognitoAuthSession.getIdentityIdResult().getError().toString()); } }, error -> Log.e("AuthQuickStart", error.toString()) );セッションの強制更新
プラグインを通じて、fetchAuthSession APIを呼び出すときに forceRefresh オプションを設定することで、内部セッションを強制的に更新できます。
AuthFetchSessionOptions options = AuthFetchSessionOptions.builder().forceRefresh(true).build();val option = AuthFetchSessionOptions.builder().forceRefresh(true).build()val option = AuthFetchSessionOptions.builder().forceRefresh(true).build()AuthFetchSessionOptions options = AuthFetchSessionOptions.builder().forceRefresh(true).build();