Published in


AWS Cognito Authentication in Flutter

In this article we will discuss about how to perform authentication using AWS cognito in our flutter apps.

With the help of AWS cognito service we can authenticate the user using their email id, send verification codes to their email id, add forgot password feature, perform change password and many more…

In this article we shall look into how to perform login using aws cognito.

→ Build up the basic UI layouts

Widget build(BuildContext context) {
return Scaffold(
appBar: buildAppBar(appBarTitle: widget.title),
body: Padding(
padding: kHPadding,
child: Column(children: [
width: 350,
height: 350,
fit: BoxFit.cover,
controller: emailController,
isPassword: false,
labelTxt: 'Email',
icon: Icons.person),
HeightSpacer(myHeight: kSpacing),
controller: passwordController,
isPassword: true,
labelTxt: 'Password',
icon: Icons.lock),
HeightSpacer(myHeight: kSpacing),
btnText: 'Login',
btnFun: () =>
login(emailController.text, passwordController.text))

The UI contains two textformfields, for accepting email id and password from user and a button, which when clicked will call the login function.

The Login function is as follows.,

login(String email, String password) =>
AWSServices().createInitialRecord(email, password);

In the login function, we will call the createInitialRecord() method written inside the AWSServices class by passing the email id and password we get from the textformfield.

→ AWSServices Code

The AWSServices class contains the core logic of making the call to AWS for authenticating the user.

class AWSServices {
final userPool = CognitoUserPool(

Future createInitialRecord(email, password) async {
debugPrint('Authenticating User...');
final cognitoUser = CognitoUser(email, userPool);
final authDetails = AuthenticationDetails(
username: email,
password: password,
CognitoUserSession? session;
try {
session = await cognitoUser.authenticateUser(authDetails);
debugPrint('Login Success...');
} on CognitoUserNewPasswordRequiredException catch (e) {
debugPrint('CognitoUserNewPasswordRequiredException $e');
} on CognitoUserMfaRequiredException catch (e) {
debugPrint('CognitoUserMfaRequiredException $e');
} on CognitoUserSelectMfaTypeException catch (e) {
debugPrint('CognitoUserMfaRequiredException $e');
} on CognitoUserMfaSetupException catch (e) {
debugPrint('CognitoUserMfaSetupException $e');
} on CognitoUserTotpRequiredException catch (e) {
debugPrint('CognitoUserTotpRequiredException $e');
} on CognitoUserCustomChallengeException catch (e) {
debugPrint('CognitoUserCustomChallengeException $e');
} on CognitoUserConfirmationNecessaryException catch (e) {
debugPrint('CognitoUserConfirmationNecessaryException $e');
} on CognitoClientException catch (e) {
debugPrint('CognitoClientException $e');
} catch (e) {

The above function will help us authenticate the user based on their email id and password.

There are other function like creating initial user-pool, forgot password, change password etc., You can get the sample code snippet for all these use-cases from the package itself (👉 refer).

Well that’s it. 🎉 Run the code to see it in action.🥳

Refer my video tutorial for complete guide:👉

Get the complete source code here:👉



A list of Flutter Tutorials and app templates

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vijay R

Hai👋 I’m a flutter developer experienced in designing and developing stunning mobile apps. Reach out for freelance projects: