aws-amplify / amplify-android

The fastest and easiest way to use AWS from your Android app.
https://docs.amplify.aws/lib/q/platform/android/
Apache License 2.0
249 stars 117 forks source link

Documentation about accessing AWS Cognito on Android WITHOUT Amplify wrapper/NPM/Compose #2826

Closed Orbyt closed 5 months ago

Orbyt commented 5 months ago

Before opening, please confirm:

Language and Async Model

Kotlin or Java

Amplify Categories

Authentication

Gradle script dependencies

Not applicable.

Environment information

Not applicable.

Please include any relevant guides or documentation you're referencing

AWS Cognito SDK docs (https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-integrate-apps.html), Amplify docs, and more.

Describe the feature request

I have spent many hours reading documentation, searching Google, online forums, previous issues, and more, before posting here. Note that the "discussions" link for this project linked in the issues template (https://github.com/aws-amplify/amplify-android/discussions) returns a 404.

Documentation about using AWS Cognito without Amplify seem to be missing. The official AWS Cognito documentation almost exclusively references AWS Amplify. The part of that document referring to AWS Cognito SDKs for Java links to https://sdk.amazonaws.com/java/.../CognitoIdentityProviderClient, which then links to generic SDKs for AWS, none of which seem to be for accessing AWS Cognito in Android (the Java SDK appears to be for Java server applications).

Adding NPM/Typescript/Compose to access Cognito as required by the AWS Amplify for Android documentation is not viable for the project which prompted this question, and considering the multiple StackOverflow and Reddit threads on this topic this seems to be an issue for others as well. Is there any other way to simply authenticate a user in Android with AWS Cognito without making the entire application an "Amplify app"? Answers and documentation for such use-cases would be extremely helpful.

I am more than happy to add additional details to this query if needed. Thank you!

Initialization steps (if applicable)

No response

Code Snippet

No response

amplifyconfiguration.json

No response

GraphQL Schema

No response

Additional information and screenshots

No response

mattcreaser commented 5 months ago

Hi @Orbyt thanks for posting your question!

There seems to be a bit of confusion here, I hope I can clarify. The short answer is that yes, it is definitely possible! The most straightforward way to access Cognito without using Amplify is to directly use the underlying AWS SDK for Kotlin - this is what Amplify itself uses to implement its Cognito functionality. There are some specific examples of using the Kotlin SDK to invoke Cognito APIs available here. The Kotlin SDK GitHub repository may be a good starting point for learning more.

Note that the Kotlin SDK is a lower-level library that is specifically useful for invoking AWS APIs, and Amplify builds on top of this to offer higher-level functionality such as storing credentials for you, or automatically refreshing tokens when required. If you use the Kotlin SDK directly you may need to implement some of this functionality yourself.

Moving on to Amplify, none of NPM/Typescript/Compose are strictly required to use Amplify to access Cognito. Here's what those technologies are used for:

Hope that answers your question, but let me know if something needs further clarification!

BigBen3 commented 5 months ago

Hi don't know if this will answer your question but I found a repo with cognito and android and pretty sure its without amplify: https://github.com/nameisjayant/Aws-Cognito-User-Pool-Authnetication

mattcreaser commented 5 months ago

Hi don't know if this will answer your question but I found a repo with cognito and android and pretty sure its without amplify: https://github.com/nameisjayant/Aws-Cognito-User-Pool-Authnetication

This app is using the AWS SDK for Android, which was the underlying AWS SDK for Amplify v1.x. Although this SDK can still be used, we recommend you either use Amplify (v2.x) or directly access the Kotlin SDK instead.

Orbyt commented 5 months ago

Hi @Orbyt thanks for posting your question!

There seems to be a bit of confusion here, I hope I can clarify. The short answer is that yes, it is definitely possible! The most straightforward way to access Cognito without using Amplify is to directly use the underlying AWS SDK for Kotlin - this is what Amplify itself uses to implement its Cognito functionality. There are some specific examples of using the Kotlin SDK to invoke Cognito APIs available here. The Kotlin SDK GitHub repository may be a good starting point for learning more.

Note that the Kotlin SDK is a lower-level library that is specifically useful for invoking AWS APIs, and Amplify builds on top of this to offer higher-level functionality such as storing credentials for you, or automatically refreshing tokens when required. If you use the Kotlin SDK directly you may need to implement some of this functionality yourself.

Moving on to Amplify, none of NPM/Typescript/Compose are strictly required to use Amplify to access Cognito. Here's what those technologies are used for:

* `npm`/`typescript`: These are used in Amplify Gen2 to define your backend. Gen2 is built on [Amazon CDK](https://docs.aws.amazon.com/cdk/v2/guide/home.html) which is a type of _infrastructure as code_, so you define the AWS resources you need in TypeScript and then the Gen2 CLI can deploy everything in the cloud. It also outputs a configuration file that you add to your Android application (`amplify_outputs.json`), which is what Amplify Android uses to connect to those resources. The only thing that goes into your application is the configuration file, TypeScript/NPM are only used to define the backend and is not part of your app. Although this is certainly the recommended way to set up your backend, you can still use the [Gen1 CLI tool to setup Auth](https://docs.amplify.aws/gen1/android/build-a-backend/auth/set-up-auth/) instead.

* `compose`: Compose is only used by [amplify-ui-android](https://github.com/aws-amplify/amplify-ui-android/), which are UI components that you can use to build apps using Amplify (in the case of Cognito, it's the Authenticator component). Using Authenticator is strictly optional - you can absolutely choose to build your own UI instead and call through to Amplify Auth APIs to handle the Authentication. This is well covered in the [Amplify Auth docs](https://docs.amplify.aws/android/build-a-backend/auth/connect-your-frontend/sign-in/).

Hope that answers your question, but let me know if something needs further clarification!

@mattcreaser I cannot thank you enough for your well thought out response. I am still going through some of the documentation you linked, so I apologize for the delayed reply. You mentioned an amplify_outputs.json file, which is also referenced in the official documentation, but I have not yet found an example of what that file actually looks like (I've also searched this repository) or how to configure it.

For context, the project that prompted this question is using AWS Cognito for user authentication. The React frontend for the project's associated web app is using the <Authenticator /> component, however I do not believe that the project is using an "Amplify backend". There is no amplify_outputs.json file in the web project, and the button used to download that file as referenced in the documentation does not seem to be present.

Alternatively, there appears to be an Amplify.configure function in the SDK, but I am unsure how to use without the aforementioned file. I did find some sort of schema for the amplify_outputs.json in the documentation.

In summary, could you explain how to either go about obtaining an amplify_outputs.json file when the project is not using an "Amplify backend", or how to configure the Amplify SDK in code?

BigBen3 commented 5 months ago

K for Amplify v1.x. Although this SDK can still be used, we recommend you either use Amplify (v2.x) or directly access the Kotlin SDK instead.

Is there any fulll project examples with mvvm that use Kotlin SDK instead?

mattcreaser commented 5 months ago

Is there any fulll project examples with mvvm that use Kotlin SDK instead?

I'm not aware of any such examples. The Kotlin SDK has a basic Android sample app but it's not MVVM. In general however I would recommend implementing your application as per clean architecture guidelines and use the Kotlin SDK at the repository layer if you wanted to go that route.

In summary, could you explain how to either go about obtaining an amplify_outputs.json file when the project is not using an "Amplify backend", or how to configure the Amplify SDK in code?

To connect to existing resources without setting up any Amplify cloud resources it's probably easier to use the older amplifyconfiguration.json file format. You can copy/paste the JSON from the documentation page here and fill in your Cognito information.

You can omit the OAuth object if you're only doing basic SRP auth.

To configure Amplify with this JSON data you have a couple of options:

Hope that helps!

BigBen3 commented 5 months ago

Is there any fulll project examples with mvvm that use Kotlin SDK instead?

I'm not aware of any such examples. The Kotlin SDK has a basic Android sample app but it's not MVVM. In general however I would recommend implementing your application as per clean architecture guidelines and use the Kotlin SDK at the repository layer if you wanted to go that route.

I was looking through the sample project but it was not related to cognito. Do you know where I can find a resource that talks about setting up the user pool and performing operations such as signing in and signing up etc with the kotlin sdk?

mattcreaser commented 5 months ago

There are examples showing how to use the Kotlin SDK to perform various operations with Cognito here. It's for running on the JVM, not an Android application, but the code to use the Kotlin SDK is the same.

github-actions[bot] commented 5 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.