π§ Email OTP Auth
A simple Flutter package for email-based OTP authentication! This package allows developers to send a 6-digit OTP to a userβs email and verify it for seamless email authentication. Perfect for apps that require email-based verification!
π Features
- π Send OTP: Generate and send a 6-digit OTP to the specified email address.
- β Verify OTP: Verify the OTP input to confirm the email's authenticity.
- β οΈ Expiration Time: OTP will expired after 5 minutes.
π Installation
Add the following to your pubspec.yaml
file:
dependencies:
email_otp_auth: ^1.0.0
Then, run:
flutter pub get
π² Usage
Import the package
import 'package:email_otp_auth/email_otp_auth.dart';
Sending and Verifying OTP
Here's how to use the package to send and verify an OTP:
import 'package:flutter/material.dart';
import 'package:email_otp_auth/email_otp_auth.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final TextEditingController emailController = TextEditingController();
final TextEditingController otpController = TextEditingController();
Future<void> sendOtp() async {
var response = await EmailOtpAuth.sendOTP(email: emailController.text);
if (response["message"] == "Email Sent") {
// Show success message
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("OTP sent successfully! β
")),
);
} else {
// Handle failure
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Failed to send OTP β")),
);
}
}
Future<void> verifyOtp() async {
var response = await EmailOtpAuth.verifyOtp(otp: otpController.text);
if (response["message"] == "OTP Verified") {
// Show success message
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("OTP verified! π")),
);
} else {
// Handle error cases
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Invalid or expired OTP β οΈ")),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Email OTP Authentication"),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: emailController,
decoration: const InputDecoration(
labelText: "Email Address",
border: OutlineInputBorder(),
),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: sendOtp,
child: const Text("Send OTP"),
),
const SizedBox(height: 20),
TextField(
controller: otpController,
decoration: const InputDecoration(
labelText: "Enter OTP",
border: OutlineInputBorder(),
),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: verifyOtp,
child: const Text("Verify OTP"),
),
],
),
),
);
}
}
πΈ Screenshots
OTP Send successfully β | OTP Verified β | OTP Invalid β | OTP Expired β οΈ |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
π Methods
sendOTP({required String email})
- Sends a 6-digit OTP to the provided email address.verifyOtp({required String otp})
- Verifies the provided OTP against the one sent to the email.
π Notes
- Ensure a valid email format to successfully receive OTPs.
- OTP expiration, retries, and error handling are managed within the
verifyOtp
method.
π Devloper Info & License
KAMESH SINGH
Flutter Developer
Copyright Β© 2024 Kamesh Singh Sisodiya. Licensed under the MIT LICENSE