auth_flutter 2.0.0 auth_flutter: ^2.0.0 copied to clipboard
A flutter plug-in for the native Auth library (Android and iOS).
import 'dart:async';
import 'package:auth_flutter/provider/custom_token_provider.dart';
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>();
final _phoneVerifyController = TextEditingController();
final _otpVerifyController = TextEditingController();
final _emailController = TextEditingController();
final _otpEmailController = TextEditingController();
@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();
_phoneVerifyController.dispose();
_otpVerifyController.dispose();
_emailController.dispose();
_otpEmailController.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: Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.loginWithGoogle();
showToast("Login Result: ${result.isSuccess}");
},
child: const Text("Google Login")),
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.loginWithFacebook();
showToast("Login Result: ${result.isSuccess}");
},
child: const Text("Facebook Login")),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.loginWithApple();
showToast("Login Result: ${result.isSuccess}");
},
child: const Text("Apple Login")),
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("Username Login")),
],
),
ElevatedButton(
onPressed: () async {
final credential = CustomProvider.createCredential("idToken");
final result = await _terraAuth.loginWithCredential(credential);
showToast("Login Result: ${result.isSuccess}");
},
child: const Text("Custom Credential Login")),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(
flex: 1,
child: TextFormField(
controller: _phoneController,
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 12),
hintText: "Phone number",
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue)),
),
)),
const SizedBox(
width: 12,
),
Flexible(
flex: 1,
child: TextFormField(
controller: _otpController,
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 12),
hintText: "Otp",
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue)),
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
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")),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
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.logout();
showToast("Logout: ${result.isSuccess}");
},
child: const Text("Logout")),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.getAccessToken();
showToast(
"Result: ${result.isSuccess} ${result.getOrNull()?.accessToken}");
},
child: const Text("Access Token")),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
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
.exchangeToken("loyalty:61e08dde-9cf4-11eb-a8b3-0242ac130003");
showToast("Result: ${result.isSuccess} ${result.getOrNull()?.idToken}");
},
child: const Text("Exchange Token")),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.getUserInfo();
showToast("User: ${result.getOrNull()?.email ?? false}");
},
child: const Text("Get User")),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(
flex: 1,
child: TextFormField(
controller: _phoneVerifyController,
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 12),
hintText: "Verify Phone",
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue)),
),
)),
const SizedBox(
width: 12,
),
Flexible(
flex: 1,
child: TextFormField(
controller: _otpVerifyController,
decoration: const InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 12),
hintText: "Verify Code",
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue)),
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.verifyPhone(_phoneVerifyController.text);
showToast("Verify: ${result.isSuccess}");
},
child: const Text("Verify Phone")),
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.confirmPhone(
_phoneVerifyController.text, _otpVerifyController.text);
showToast("Confirm: ${result.isSuccess}");
},
child: const Text("Confirm Phone")),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(
flex: 1,
child: TextFormField(
controller: _emailController,
decoration: const InputDecoration(
hintText: "Verify Email",
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue)),
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 12),
),
)),
const SizedBox(
width: 12,
),
Flexible(
flex: 1,
child: TextFormField(
controller: _otpEmailController,
decoration: const InputDecoration(
hintText: "Verify email",
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
borderSide: BorderSide(color: Colors.blue)),
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 12),
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.verifyEmail(_emailController.text);
showToast("Verify: ${result.isSuccess}");
},
child: const Text("Verify Email")),
ElevatedButton(
onPressed: () async {
final result = await _terraAuth.confirmEmail(
_emailController.text, _otpEmailController.text);
showToast("Confirm: ${result.isSuccess}");
},
child: const Text("Confirm Email")),
],
),
],
),
),
),
),
);
}
void showToast(String message) {
_messangerKey.currentState!.showSnackBar(SnackBar(content: Text(message)));
}
}