AWS Cognito Authentication in Flutter
In this article we will discuss about how to perform authentication using AWS cognito in our flutter apps.
🎥 Video Tutorial
📚 Summary
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.
🛠️ Dependencies
🔭 Implementation
→ Build up the basic UI layouts
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: buildAppBar(appBarTitle: widget.title),
body: Padding(
padding: kHPadding,
child: Column(children: [
Image.asset(
loginImages[0],
width: 350,
height: 350,
fit: BoxFit.cover,
),
InputField(
controller: emailController,
isPassword: false,
labelTxt: 'Email',
icon: Icons.person),
HeightSpacer(myHeight: kSpacing),
InputField(
controller: passwordController,
isPassword: true,
labelTxt: 'Password',
icon: Icons.lock),
HeightSpacer(myHeight: kSpacing),
PrimaryBtn(
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(
'${(dotenv.env['POOL_ID'])}',
'${(dotenv.env['CLIENT_ID'])}',
);
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) {
print(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:👉 https://youtu.be/jxPjDb9uH1A
Get the complete source code here:👉 https://github.com/vijayinyoutube/aws_cognito_app
Check out all my Flutter related blogs here.,👇
Other articles you may like.,
🔵Setting up your Flutter app for publishing in Play Store.
🔴Confetti Animation in Flutter
🟡 Change App Launcher Icon in Flutter
🔴 AnimatedContainer Widget in Flutter
Most popular
Flutter Tutorials
If you found this article useful and wish to support my work, you can consider buying me a ☕️ coffee.👇
If you want to know more about Flutter and various Widgets in Flutter…?🤓 Then visit my channel 👉🏻 vijaycreations🚩
Thanks.,