auth_module 0.0.1 copy "auth_module: ^0.0.1" to clipboard
auth_module: ^0.0.1 copied to clipboard

A wrapper to manage authentication in an application.

Auth Module README #

Overview #

The auth_module is a Flutter package designed to handle authentication-related functionalities seamlessly. This package includes features for requesting OTPs, verifying OTPs, managing authentication tokens, and providing authentication guards.

Features #

  • Request OTP
  • Verify OTP
  • Manage authentication tokens
  • Authentication guards

Installation #

To use this package, add the following dependency to your pubspec.yaml file:

dependencies:
auth_module:
path: ../ # Adjust the path as necessary

Run flutter pub get to install the package.

Usage #

Initializing AuthBloc #

First, initialize the AuthBloc in your Flutter application. Here is an example of setting up AuthBloc using MultiBlocProvider:

import 'package:auth_module/auth_module.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<AuthBloc>(
          create: (context) => AuthBloc(authService: MyAuthService()),
        ),
      ],
      child: MaterialApp(
        home: HomeScreen(),
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(child: Text('Home Screen')),
    );
  }
}

Request OTP #

To request an OTP, dispatch the RequestOtpEvent to the AuthBloc. Here is an example with a TextField for inputting the phone number:

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:auth_module/auth_module.dart';

class RequestOtpScreen extends StatelessWidget {
  final TextEditingController _phoneController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    final authBloc = BlocProvider.of<AuthBloc>(context);

    return Scaffold(
      appBar: AppBar(title: Text('Request OTP')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: _phoneController,
              decoration: InputDecoration(labelText: 'Phone Number'),
            ),
            ElevatedButton(
              onPressed: () {
                authBloc.add(RequestOtpEvent(phoneNumber: _phoneController.text));
              },
              child: Text('Request OTP'),
            ),
          ],
        ),
      ),
    );
  }
}

Verify OTP #

To verify an OTP, dispatch the VerifyOtpEvent to the AuthBloc. Here is an example with TextFields for inputting the phone number and OTP:

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:auth_module/auth_module.dart';

class VerifyOtpScreen extends StatelessWidget {
  final TextEditingController _phoneController = TextEditingController();
  final TextEditingController _otpController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    final authBloc = BlocProvider.of<AuthBloc>(context);

    return Scaffold(
      appBar: AppBar(title: Text('Verify OTP')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: _phoneController,
              decoration: InputDecoration(labelText: 'Phone Number'),
            ),
            TextField(
              controller: _otpController,
              decoration: InputDecoration(labelText: 'OTP'),
            ),
            ElevatedButton(
              onPressed: () {
                authBloc.add(
                    VerifyOtpEvent(phoneNumber: _phoneController.text, otp: _otpController.text));
              },
              child: Text('Verify OTP'),
            ),
          ],
        ),
      ),
    );
  }
}

Implementing the Abstract AuthService Class #

To implement the abstract AuthService class, extend it and provide concrete implementations for its methods:

import 'package:auth_module/auth_module.dart';

class MyAuthService extends AuthService {
  @override
  Future<AuthResponse> requestOtp(String phoneNumber) async {
// Implement your OTP request logic here
    return AuthResponse(isSuccessful: true, message: 'OTP requested successfully');
  }

  @override
  Future<AuthResponse> verifyOtp(String phoneNumber, String otp) async {
// Implement your OTP verification logic here
    return AuthResponse(isSuccessful: true, message: 'OTP verified successfully');
  }

  @override
  Future<AuthResponse> login(String username, String password) async {
// Implement your login logic here
    return AuthResponse(isSuccessful: true, message: 'Logged in successfully');
  }

  @override
  Future<void> logout() async {
// Implement your logout logic here
  }
}

Example Implementation #

Refer to the example implementation in the ./example/ directory for a comprehensive demonstration of how to use the auth_module package.

License #

This project is licensed under the MIT License - see the LICENSE file for details.

0
likes
90
points
35
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A wrapper to manage authentication in an application.

License

MIT (license)

Dependencies

auto_route, flutter, flutter_bloc, flutter_secure_storage, http

More

Packages that depend on auth_module