auth_flutter 0.0.1-alpha.1 copy "auth_flutter: ^0.0.1-alpha.1" to clipboard
auth_flutter: ^0.0.1-alpha.1 copied to clipboard

outdated

A flutter plug-in for the native Auth library (Android and iOS).

example/lib/main.dart

import 'dart:async';

import 'package:auth_flutter/provider/phone_otp_provider.dart';
import 'package:auth_flutter/provider/username_password_provider.dart';
import 'package:auth_flutter/terra_auth.dart';
import 'package:flutter/material.dart';

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static const _appName = "Test";
  late TerraAuth _terraAuth;
  final _phoneController = TextEditingController();
  final _otpController = TextEditingController();
  final _messangerKey = GlobalKey<ScaffoldMessengerState>();

  @override
  void initState() {
    super.initState();
    _phoneController.text = "0374264438";
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    _terraAuth = await TerraAuth.getInstance(_appName);
  }

  @override
  void dispose() {
    _phoneController.dispose();
    _otpController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      scaffoldMessengerKey: _messangerKey,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              ElevatedButton(
                  onPressed: () async {
                    final result = await _terraAuth.loginWithGoogle();
                    showToast("Login Result: ${result.isSuccess}");
                  },
                  child: const Text("Login With Google")),
              ElevatedButton(
                  onPressed: () async {
                    final result = await _terraAuth.loginWithFacebook();
                    showToast("Login Result: ${result.isSuccess}");
                  },
                  child: const Text("Login With Facebook")),
              ElevatedButton(
                  onPressed: () async {
                    final result = await _terraAuth.loginWithApple();
                    showToast("Login Result: ${result.isSuccess}");
                  },
                  child: const Text("Login With Apple")),
              ElevatedButton(
                  onPressed: () async {
                    final credential = UsernamePasswordProvider.createCredential(
                        "trung.cs@teko.vn", "trung123456");
                    final result = await _terraAuth.loginWithCredential(credential);
                    showToast("Login Result: ${result.isSuccess}");
                  },
                  child: const Text("Login With Username")),
              TextFormField(
                controller: _phoneController,
                decoration: const InputDecoration(
                  hintText: "Phone number",
                ),
              ),
              TextFormField(
                controller: _otpController,
                decoration: const InputDecoration(
                  hintText: "Otp",
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ElevatedButton(
                      onPressed: () async {
                        final result = await _terraAuth.requestOtp(_phoneController.text.trim());
                        showToast("OTP Result: ${result.isSuccess}");
                      },
                      child: const Text("Request Otp")),
                  ElevatedButton(
                      onPressed: () async {
                        final credential = PhoneOtpProvider.createCredential(
                            _phoneController.text.trim(), _otpController.text.trim());
                        final result = await _terraAuth.loginWithCredential(credential);
                        showToast("Login Result: ${result.isSuccess}");
                      },
                      child: const Text("Login With Otp")),
                ],
              ),
              ElevatedButton(
                  onPressed: () async {
                    final result = await _terraAuth.logout();
                    showToast("Logout: ${result.isSuccess}");
                  },
                  child: const Text("Logout")),
              ElevatedButton(
                  onPressed: () async {
                    final result = await _terraAuth.isAuthorized();
                    showToast("Authorized: ${result.getOrNull() ?? false}");
                  },
                  child: const Text("Check Authorization State")),
              ElevatedButton(
                  onPressed: () async {
                    final result = await _terraAuth.getAccessToken();
                    showToast("Result: ${result.isSuccess} ${result.getOrNull()?.accessToken}");
                  },
                  child: const Text("Get Access Token")),
              ElevatedButton(
                  onPressed: () async {
                    final result = await _terraAuth.refreshToken();
                    showToast("Result: ${result.isSuccess} ${result.getOrNull()?.accessToken}");
                  },
                  child: const Text("Refresh Token")),
              ElevatedButton(
                  onPressed: () async {
                    final result = await _terraAuth
                        .exchangToken("loyalty:61e08dde-9cf4-11eb-a8b3-0242ac130003");
                    showToast("Result: ${result.isSuccess} ${result.getOrNull()?.idToken}");
                  },
                  child: const Text("Exchange Token")),
            ],
          ),
        ),
      ),
    );
  }

  void showToast(String message) {
    _messangerKey.currentState!.showSnackBar(SnackBar(content: Text(message)));
  }
}
2
likes
0
pub points
70%
popularity

Publisher

unverified uploader

A flutter plug-in for the native Auth library (Android and iOS).

Homepage

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on auth_flutter