Page updated Nov 3, 2023

Accessing credentials

An intentional decision with Amplify Auth was to avoid any public methods exposing credentials or manipulating them.

With Auth, you simply sign in and it handles everything else needed to keep the credentials up to date and vend them to the other categories.

However, if you need to access them in relation to working with an API outside Amplify or want access to AWS specific identifying information (e.g. IdentityId), you can access these implementation details by casting the result of fetchAuthSession as follows:

1Amplify.Auth.fetchAuthSession(
2 result -> {
3 AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
4 switch(cognitoAuthSession.getIdentityIdResult().getType()) {
5 case SUCCESS:
6 Log.i("AuthQuickStart", "IdentityId: " + cognitoAuthSession.getIdentityIdResult().getValue());
7 break;
8 case FAILURE:
9 Log.i("AuthQuickStart", "IdentityId not present because: " + cognitoAuthSession.getIdentityIdResult().getError().toString());
10 }
11 },
12 error -> Log.e("AuthQuickStart", error.toString())
13);
1Amplify.Auth.fetchAuthSession(
2 {
3 val session = it as AWSCognitoAuthSession
4 when (session.identityIdResult.type) {
5 AuthSessionResult.Type.SUCCESS ->
6 Log.i("AuthQuickStart", "IdentityId = ${session.identityIdResult.value}")
7 AuthSessionResult.Type.FAILURE ->
8 Log.w("AuthQuickStart", "IdentityId not found", session.identityIdResult.error)
9 }
10 },
11 { Log.e("AuthQuickStart", "Failed to fetch session", it) }
12)
1try {
2 val session = Amplify.Auth.fetchAuthSession() as AWSCognitoAuthSession
3 val id = session.identityIdResult
4 if (id.type == AuthSessionResult.Type.SUCCESS) {
5 Log.i("AuthQuickStart", "IdentityId: ${id.value}")
6 } else if (id.type == AuthSessionResult.Type.FAILURE) {
7 Log.i("AuthQuickStart", "IdentityId not present: ${id.error}")
8 }
9} catch (error: AuthException) {
10 Log.e("AuthQuickStart", "Failed to fetch session", error)
11}
1RxAmplify.Auth.fetchAuthSession()
2 .subscribe(
3 result -> {
4 AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
5
6 switch (cognitoAuthSession.getIdentityIdResult().getType()) {
7 case SUCCESS:
8 Log.i("AuthQuickStart", "IdentityId: " + cognitoAuthSession.getIdentityIdResult().getValue());
9 break;
10 case FAILURE:
11 Log.i("AuthQuickStart", "IdentityId not present because: " + cognitoAuthSession.getIdentityIdResult().getError().toString());
12 }
13 },
14 error -> Log.e("AuthQuickStart", error.toString())
15 );

Force refreshing session

You can ask the plugin to force refresh the internal session by setting the forceRefresh option when calling the fetchAuthSession API.

1AuthFetchSessionOptions options = AuthFetchSessionOptions.builder().forceRefresh(true).build();
1val option = AuthFetchSessionOptions.builder().forceRefresh(true).build()
1val option = AuthFetchSessionOptions.builder().forceRefresh(true).build()
1AuthFetchSessionOptions options = AuthFetchSessionOptions.builder().forceRefresh(true).build();