Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to login with AWS Cognito without using native SDK

I want to authenticate my users using Cognito, for an app built in Flutter and Dart.

Unfortunately native AWS SDKs don't exist for Dart, and I can't use JS interop in Flutter.

I've looked at the OAuth2 flow, but that requires my users to be redirected to a login form, which isn't great for a mobile app.

What are the alternatives?

like image 704
haz Avatar asked Apr 23 '18 13:04

haz


People also ask

Can I use AWS Cognito without amplify?

AWS Cognito integration with amazon-cognito-identity-js The common approach to integrate Cognito into a single-page app is to use Amplify library. However, using the entire Amplify library may be overkill if we only want to use it for implementing Cognito authentication features in our app.

How do I authenticate to Amazon Cognito?

Go to AWS Cognito service and click “Manage Identity Pools”. 2. Enter “Identity pool name”, expand the “Authentication providers” section and select “Cognito” tab. This is where the Cognito authentication provider will be registered with the Identity pool.


2 Answers

You can try the amazon_cognito_identity_dart package which is written purely in Dart.

import 'package:amazon_cognito_identity_dart/cognito.dart';

final userPool = new CognitoUserPool(
    'ap-southeast-1_xxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxx');
final cognitoUser = new CognitoUser(
    '[email protected]', userPool);
final authDetails = new AuthenticationDetails(
    username: '[email protected]', password: 'Password001');

CognitoUserSession session;
try {
  session = await cognitoUser.authenticateUser(authDetails);
} on CognitoUserException catch (e) {
  // handle CognitoUser exceptions
} catch (e) {
  print(e);
}

print(session.isValid());

See the Example Secure Counter App to get ideas on how to use it with Flutter.

Disclaimer: I am the original author of the package.

like image 164
Jon Saw Avatar answered Oct 19 '22 22:10

Jon Saw


You can implement a backend service to validate Cognito credentials.

In this approach, you can implement a login screen in the mobile app which sends the user credentials to the backend. In the backend you can verify the credentials using Cognito SDK and issue a token that will be sent for subsequent API requests from mobile app.

You can use AWS API Gateway and Lambda to implement this.

For example refer this to implement a NodeJS backend.

like image 29
Ashan Avatar answered Oct 19 '22 22:10

Ashan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!