testera_auth 0.1.0
testera_auth: ^0.1.0 copied to clipboard
A Flutter plugin for code-based authentication with Firebase Cloud Functions integration.
Testera Auth #
A Flutter plugin for code-based authentication with Firebase Cloud Functions integration.
Features #
- Code-based authentication system
- Firebase Cloud Functions integration
- Local storage for authentication state
- Configurable expiration duration
- Rate limiting protection
- Support for both Android and iOS
Getting started #
Add this to your package's pubspec.yaml file:
dependencies:
testera_auth: ^0.1.0
Usage #
- Wrap your app with
TesteraAuth:
import 'package:flutter/material.dart';
import 'package:testera_auth/testera_auth.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return TesteraAuth(
validationUrl: 'YOUR_FIREBASE_FUNCTION_URL',
child: MaterialApp(
title: 'My App',
home: const HomePage(),
),
);
}
}
- Configure your Firebase Cloud Function:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.validateCode = functions.https.onRequest(async (req, res) => {
const { code } = req.body;
try {
const codeRef = admin.firestore().collection("access_codes").doc(code);
const codeDoc = await codeRef.get();
if (!codeDoc.exists) {
return res.status(400).json({ error: "Invalid code" });
}
const codeData = codeDoc.data();
if (codeData.used) {
return res.status(400).json({ error: "Code already used" });
}
const expiresAt = codeData.expiresAt.toDate();
if (expiresAt < new Date()) {
return res.status(400).json({ error: "Code expired" });
}
await codeRef.update({ used: true });
return res.status(200).json({ success: true });
} catch (error) {
console.error("Error validating code:", error);
return res.status(500).json({ error: "Internal server error" });
}
});
Configuration #
The TesteraAuth widget accepts the following parameters:
validationUrl(required): The URL of your Firebase Cloud Function for code validationexpirationDuration(optional): How long the authentication should remain valid (default: 7 days)initialCode(optional): A code to validate immediately when the app starts
Example #
Check out the example directory for a complete working example.
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.