Skip to content

Commit

Permalink
chore(next-release/main): update next-release/main with latest from m…
Browse files Browse the repository at this point in the history
…ain (#6956)

* Minor edits to Vite + React quickstart (#6926)

* Updates to Vite + React quickstart

* Update src/fragments/gen2/quickstart/build-a-backend.mdx

Co-authored-by: Kevin Old <kevold@amazon.com>

* Update build-a-backend.mdx

* Update build-a-backend.mdx

* Update build-a-backend.mdx

---------

Co-authored-by: Kevin Old <kevold@amazon.com>

* fix: Fix incorrect auth import path in migration guide. (#6934)

* fix: Update incorrect `updateMFAPreference` parameter in JS v6 migration guide. (#6935)

* Amplify Android Release 2.14.11 (#6933)

* chore: refactor .layout-header into separate component (#6826)

* platform typed as optional

* refactor init

* remove commented code

* remove angry useEffect

* fix typing

* added layoutcontext so mobile menu closes on navigation

* move import

---------

Co-authored-by: katiegoines <katiegoines@gmail.com>

* fix: Nesting of auth cli templates (#6932)

* fix: change config variable name (#6923)

* Guide to support Amplify v2 with AWS Android SDK (#6927)

* Update puppeteer (#6949)

* Update index.mdx (#6950)

* Update CODEOWNERS with correct PM alignment (#6951)

* fix(flutter, js): scope "connect existing cdk" guide to respective platform (#6947)

Co-authored-by: Tim Nguyen <54393192+timngyn@users.noreply.github.com>

* Revert "fix(flutter, js): scope "connect existing cdk" guide to respective pl…" (#6955)

This reverts commit 5998b22.

* chore: remove duplicated logic between Layout and LayoutHeader (#6954)

* chore: more layout refactoring

* remove unused prop, alphabetize props

---------

Co-authored-by: Jim Eagan <84857865+hibler13@users.noreply.github.com>
Co-authored-by: Kevin Old <kevold@amazon.com>
Co-authored-by: Jim Blanchard <jim.l.blanchard@gmail.com>
Co-authored-by: Ankit Shah <22114629+ankpshah@users.noreply.github.com>
Co-authored-by: Katie Goines <30757403+katiegoines@users.noreply.github.com>
Co-authored-by: katiegoines <katiegoines@gmail.com>
Co-authored-by: Dan Kiuna <dankiuna@gmail.com>
Co-authored-by: Kihara, Takuya <gray@tacck.net>
Co-authored-by: Tyler Roach <tjroach@amazon.com>
Co-authored-by: Tim Nguyen <54393192+timngyn@users.noreply.github.com>
Co-authored-by: Rene Brandel <4989523+renebrandel@users.noreply.github.com>
Co-authored-by: Elijah Quartey <Equartey@users.noreply.github.com>
  • Loading branch information
13 people committed Mar 8, 2024
1 parent ce84173 commit 912ff10
Show file tree
Hide file tree
Showing 2 changed files with 281 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/directory/directory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,9 @@ export const directory = {
children: [
{
path: 'src/pages/gen1/[platform]/sdk/configuration/setup-options/index.mdx'
},
{
path: 'src/pages/gen1/[platform]/sdk/configuration/amplify-compatibility/index.mdx'
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
import { getCustomStaticPath } from '@/utils/getCustomStaticPath';

export const meta = {
title: 'Amplify v2 Compatibility',
description:
'Learn how to use the AWS SDK with Amplify v2.',
platforms: ['android']
};

export const getStaticPaths = async () => {
return getCustomStaticPath(meta.platforms);
};

export function getStaticProps(context) {
return {
props: {
platform: context.params.platform,
meta
}
};
}

<InlineFilter filters={["android"]}>
<Callout warning>
The AWS Mobile Client (com.amazonaws:aws-android-sdk-mobile-client) and Amplify Android v2 are not compatible with each other. Amplify v2 migrates the credentials from AWS Mobile Client into a different format, leaving AWS Mobile Client unable to read the credentials. If AWS Mobile Client is launched after this migration has taken place, the Amplify v2 credentials will also be cleared.
</Callout>

## Using Amplify V2 Auth with AWS Android SDK Plugin

We recommend using Amplify v2 with the [AWS Kotlin SDK](https://aws.amazon.com/sdk-for-kotlin/), rather than the AWS Android SDK. In order to better support existing implementations, this guide demonstrates how to continue using AWS Android SDK plugins with Amplify v2.

### Creating an AmplifyCredentialsProvider

Many of the AWS Android SDK plugins accept a custom `AWSCredentialsProvider` implementation. You can implement your own `AWSCredentialsProvider` that uses Amplify Android v2 to provide credentials.

<BlockSwitcher>
<Block name="Java">

```java
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amplifyframework.auth.AWSTemporaryCredentials;
import com.amplifyframework.auth.cognito.AWSCognitoAuthSession;
import com.amplifyframework.auth.options.AuthFetchSessionOptions;
import com.amplifyframework.core.Amplify;

import java.util.concurrent.CompletableFuture;

class AmplifyCredentialsProvider implements AWSCredentialsProvider {

@Override
public AWSCredentials getCredentials() {
CompletableFuture<AWSCredentials> sdkCredentials = new CompletableFuture<>();

Amplify.Auth.fetchAuthSession((authSession) -> {
BasicSessionCredentials credentials = null;
if (authSession instanceof AWSCognitoAuthSession) {
AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) authSession;
com.amplifyframework.auth.AWSCredentials awsCredentials =
cognitoAuthSession.getAwsCredentialsResult().getValue();
if (awsCredentials instanceof AWSTemporaryCredentials) {
AWSTemporaryCredentials temporaryAwsCredentials =
(AWSTemporaryCredentials) awsCredentials;
credentials = new BasicSessionCredentials(
temporaryAwsCredentials.getAccessKeyId(),
temporaryAwsCredentials.getSecretAccessKey(),
temporaryAwsCredentials.getSessionToken()
);
}
}

if (credentials != null) {
sdkCredentials.complete(credentials);
} else {
sdkCredentials.completeExceptionally(
new RuntimeException("Failed to get credentials")
);
}
}, (exception) -> sdkCredentials.completeExceptionally(
new RuntimeException("Failed to get credentials", exception)
));

return sdkCredentials.join();
}

@Override
public void refresh() {
CompletableFuture<Void> result = new CompletableFuture<>();
Amplify.Auth.fetchAuthSession(
AuthFetchSessionOptions.builder().forceRefresh(true).build(),
// We do not need to capture value if refresh succeeds
(authSession) -> result.complete(null),
// We do not need to throw if refresh fails
(exception) -> result.complete(null)
);

result.join();
}
}
```

</Block>
<Block name="Kotlin">

```kotlin
import com.amazonaws.auth.AWSCredentials
import com.amazonaws.auth.AWSCredentialsProvider
import com.amazonaws.auth.BasicSessionCredentials
import com.amplifyframework.auth.AWSTemporaryCredentials
import com.amplifyframework.auth.cognito.AWSCognitoAuthSession
import com.amplifyframework.auth.options.AuthFetchSessionOptions
import com.amplifyframework.core.Amplify
import java.lang.RuntimeException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.runBlocking

class AmplifyCredentialsProvider : AWSCredentialsProvider {

override fun getCredentials(): AWSCredentials = runBlocking {
suspendCoroutine { continuation ->
Amplify.Auth.fetchAuthSession(
{ authSession ->
val awsTemporaryCredentials = (authSession as? AWSCognitoAuthSession)
?.awsCredentialsResult?.value as? AWSTemporaryCredentials

val sdkCredentials = awsTemporaryCredentials?.let {
BasicSessionCredentials(it.accessKeyId, it.secretAccessKey, it.sessionToken)
}

if (sdkCredentials != null) {
continuation.resume(sdkCredentials)
} else {
val authException = RuntimeException("Failed to get credentials")
continuation.resumeWithException(authException)
}
},
{
continuation.resumeWithException(
RuntimeException("Failed to get credentials. See exception.", it)
)
}
)
}
}

override fun refresh() = runBlocking {
suspendCoroutine { continuation ->
Amplify.Auth.fetchAuthSession(
AuthFetchSessionOptions.builder().forceRefresh(true).build(),
// We do not need to capture value if refresh succeeds
{ continuation.resume(Unit) },
// We do not need to throw if refresh fails
{ continuation.resume(Unit) }
)
}
}
}
```

</Block>
</BlockSwitcher>

You can now use your `AmplifyCredentialsProvider` in any plugins that accept an `AWSCredentialsProvider`, instead of using `AWSMobileClient.getInstance()` as your AWSCredentialsProvider.


## Providing AWS Configuration Information

Amplify v2 uses the `amplifyconfiguration.json` file where AWS Android SDK uses the `awsconfiguration.json` file. If you are using both Amplify v2 and AWS Android SDK in your project, it is important to ensure the resources are in sync. The Amplify CLI still generates and updates both of these file types, but any manual customizations should be applied to both files.

For AWS Android SDK plugins that require configuration information, you can continue to use the `AWSConfiguration` class.

<BlockSwitcher>
<Block name="Java">

```java
AWSConfiguration awsConfiguration = new AWSConfiguration(context);
```

</Block>
<Block name="Kotlin">

```kotlin
val awsConfiguration = AWSConfiguration(context)
```

</Block>
</BlockSwitcher>

## Example Usage of AWS Android SDK Plugins with Amplify v2

This is not an exhaustive list of supported plugins. Any plugins that accept an AWSCredentialsProvider and do not rely on AWS Mobile Client should work.

### S3 Storage (com.amazonaws:aws-android-sdk-s3)

<BlockSwitcher>
<Block name="Java">

```java
AWSConfiguration awsConfiguration = new AWSConfiguration(context);
TransferUtility transferUtility = TransferUtility.builder()
.context(context)
.awsConfiguration(awsConfig)
.s3Client(
new AmazonS3Client(
new AmplifyCredentialsProvider(),
Region.getRegion(Regions.US_EAST_1)
)
)
.build();
```

</Block>
<Block name="Kotlin">

```kotlin
val awsConfiguration = AWSConfiguration(context)
val transferUtility = TransferUtility.builder()
.context(context)
.awsConfiguration(awsConfiguration)
.s3Client(
AmazonS3Client(
AmplifyCredentialsProvider(),
Region.getRegion(Regions.US_EAST_1)
)
)
.build()
```

</Block>
</BlockSwitcher>

### IoT (com.amazonaws:aws-android-sdk-iot)

<BlockSwitcher>
<Block name="Java">

```java
AWSIotClient client = new AWSIotClient(new AmplifyCredentialsProvider());

```

</Block>
<Block name="Kotlin">

```kotlin
val client = AWSIotClient(AmplifyCredentialsProvider())
```

</Block>
</BlockSwitcher>



### Android SDK Generated by API Gateway (aws-android-sdk-apigateway-core)

<BlockSwitcher>
<Block name="Java">

```java
ApiClientFactory clientFactory = new ApiClientFactory();
clientFactory.credentialsProvider(new AmplifyCredentialsProvider());
```

</Block>
<Block name="Kotlin">

```kotlin
val clientFactory = ApiClientFactory()
clientFactory.credentialsProvider(AmplifyCredentialsProvider())
```

</Block>
</BlockSwitcher>

</InlineFilter>

0 comments on commit 912ff10

Please sign in to comment.