πŸ“§ 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 ⚠️
OTP Send 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

KAMESH SINGH
Flutter Developer

GitHub LinkedIn

Copyright Β© 2024 Kamesh Singh Sisodiya. Licensed under the MIT LICENSE


Libraries

email_otp_auth