auth_email 0.3.0 copy "auth_email: ^0.3.0" to clipboard
auth_email: ^0.3.0 copied to clipboard

This is an easy way to authenticate user email with OTP using PHP as a backend in your own server.

example/lib/main.dart

import 'package:auth_email/auth_email.dart';
import 'package:flutter/material.dart';

// This only use for testing purposes.
final authEmail = AuthEmail(
  appName: 'Auth Email Example',
  server: 'https://pub.lamnhan.dev/auth-email/api',
  serverKey: 'ohYwh',
  isDebug: true,
);

void main() async {
  runApp(const MaterialApp(home: AuthEmailApp()));
}

class AuthEmailApp extends StatefulWidget {
  const AuthEmailApp({Key? key}) : super(key: key);

  @override
  State<AuthEmailApp> createState() => _AuthEmailAppState();
}

class _AuthEmailAppState extends State<AuthEmailApp> {
  String sendOtpButton = 'Send OTP';
  String verifyOtpButton = 'Verify OTP';

  bool isSent = false;
  String desEmailTextField = '';
  String verifyTextField = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Auth Email'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          if (!isSent) ...[
            const Text('Input your client email:'),
            TextFormField(
              textAlign: TextAlign.center,
              initialValue: desEmailTextField,
              onChanged: (value) {
                desEmailTextField = value;
              },
              validator: (value) {
                if (!AuthEmail.isValidEmail(desEmailTextField)) {
                  return 'This email is not valid!';
                }
                return null;
              },
            ),
            const SizedBox(height: 8),
            ElevatedButton(
              onPressed: () async {
                setState(() {
                  sendOtpButton = 'Sending OTP...';
                });

                final result =
                    await authEmail.sendOTP(email: desEmailTextField);

                if (!result) {
                  setState(() {
                    sendOtpButton = 'Send OTP failed!';
                  });
                }
                setState(() {
                  isSent = result;
                });
              },
              child: Text(sendOtpButton),
            )
          ] else ...[
            const Text('Input your OTP:'),
            TextField(
              textAlign: TextAlign.center,
              onChanged: (value) {
                verifyTextField = value;
              },
            ),
            const SizedBox(height: 8),
            ElevatedButton(
              onPressed: () async {
                final result = authEmail.verifyOTP(
                    email: desEmailTextField, otp: verifyTextField);

                if (result) {
                  setState(() {
                    verifyOtpButton = 'Verified OTP';
                  });
                } else {
                  setState(() {
                    verifyOtpButton = 'Verify OTP failed!';
                  });
                }
              },
              child: Text(verifyOtpButton),
            )
          ]
        ],
      ),
    );
  }
}
0
likes
140
pub points
46%
popularity

Publisher

verified publisherlamnhan.dev

This is an easy way to authenticate user email with OTP using PHP as a backend in your own server.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

http

More

Packages that depend on auth_email