retro_auth 1.0.1
retro_auth: ^1.0.1 copied to clipboard
A Flutter plugin to implement Smartters auth
example/lib/main.dart
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:retro_auth/retro_auth.dart';
import 'package:retro_auth_example/env_config.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Retro Auth - All Methods Demo',
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
home: RetroAuthDemoPage(),
);
}
}
class RetroAuthDemoPage extends StatefulWidget {
const RetroAuthDemoPage({super.key});
@override
RetroAuthDemoPageState createState() => RetroAuthDemoPageState();
}
class RetroAuthDemoPageState extends State<RetroAuthDemoPage> {
late RetroAuth _retroAuth;
IUser? _currentUser;
bool _isLoading = false;
String _lastResult = '';
@override
void initState() {
super.initState();
_initializeAuth();
}
Future<void> _initializeAuth() async {
try {
_retroAuth = RetroAuth();
await _retroAuth.initialize(baseUrl: "https://api.replyme.live",userFromJson: UserResponse.fromJson);
setState(() {
_lastResult = 'Auth initialized successfully';
});
} catch (e) {
setState(() {
_lastResult = 'Failed to initialize auth: $e';
});
}
}
void _setLoading(bool loading) {
setState(() {
_isLoading = loading;
});
}
void _setResult(String result) {
setState(() {
_lastResult = result;
});
}
// OAuth Methods
Future<void> _loginWithOAuth() async {
_setLoading(true);
try {
final user = await _retroAuth.loginWithOauth(
clientId: 'your_client_id',
clientSecret: 'your_client_secret',
redirectUri: 'com.yourapp://oauth/callback',
authorizationUrl: 'https://your-oauth-provider.com/oauth/authorize',
tokenUrl: 'https://your-oauth-provider.com/oauth/token',
scopes: ['openid', 'profile', 'email'],
serviceName: '/v1/authentication',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('OAuth login successful! User ID: ${user.userId}');
} else {
_setResult('OAuth login returned null');
}
} catch (e) {
_setResult('OAuth login failed: $e');
} finally {
_setLoading(false);
}
}
// Apple Sign In
Future<void> _loginWithApple() async {
_setLoading(true);
try {
final user = await _retroAuth.loginWithApple(
null,
serviceName: '/v1/authenticate',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('Apple login successful! User ID: ${user.userId}');
} else {
_setResult('Apple login returned null');
}
} catch (e) {
_setResult('Apple login failed: $e');
} finally {
_setLoading(false);
}
}
// Google Sign In
Future<void> _loginWithGoogle() async {
_setLoading(true);
try {
final user = await _retroAuth.loginWithGoogle(
GoogleConfig(
clientId: envConfig["clientId"],
clientSecret: envConfig["serverClientId"],
),
serviceName: '/v1/authenticate',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('Google login successful! User ID: ${user.userId}');
} else {
_setResult('Google login returned null');
}
} catch (e, s) {
_setResult('Google login failed: $e');
log("Google login Error", error: e, stackTrace: s);
} finally {
_setLoading(false);
}
}
// Facebook Sign In
Future<void> _loginWithFacebook() async {
_setLoading(true);
try {
final user = await _retroAuth.loginWithFacebook(
null,
serviceName: '/v1/authenticate',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('Facebook login successful! User ID: ${user.userId}');
} else {
_setResult('Facebook login returned null');
}
} catch (e) {
_setResult('Facebook login failed: $e');
} finally {
_setLoading(false);
}
}
// Discord Sign In
Future<void> _loginWithDiscord() async {
_setLoading(true);
try {
final user = await _retroAuth.loginWithDiscord(
null,
serviceName: '/v1/authenticate',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('Discord login successful! User ID: ${user.userId}');
} else {
_setResult('Discord login returned null');
}
} catch (e) {
_setResult('Discord login failed: $e');
} finally {
_setLoading(false);
}
}
// LinkedIn Sign In
Future<void> _loginWithLinkedIn() async {
_setLoading(true);
try {
final user = await _retroAuth.loginWithLinkedIn(
null,
context,
redirectUrl: 'https://your-redirect-url.com',
serviceName: '/v1/authenticate',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('LinkedIn login successful! User ID: ${user.userId}');
} else {
_setResult('LinkedIn login returned null');
}
} catch (e) {
_setResult('LinkedIn login failed: $e');
} finally {
_setLoading(false);
}
}
// TrueCaller Sign In
Future<void> _loginWithTrueCaller() async {
_setLoading(true);
try {
final user = await _retroAuth.loginWithTrueCaller(
null,
serviceName: '/v1/authenticate',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('TrueCaller login successful! User ID: ${user.userId}');
} else {
_setResult('TrueCaller login returned null');
}
} catch (e) {
_setResult('TrueCaller login failed: $e');
} finally {
_setLoading(false);
}
}
// Email OTP Methods
Future<void> _sendEmailOtp() async {
_setLoading(true);
try {
final result = await _retroAuth.sendEmailOtp(
'user1@gmail.com',
strategy: "emailOtp",
purpose: 'login',
entity: "USER",
serviceName: '/v1/authenticate',
);
_setResult('Email OTP sent: ${result?.message}');
} catch (e, s) {
_setResult('Send email OTP failed: $e');
log("Send email otp Error ", error: e, stackTrace: s);
} finally {
_setLoading(false);
}
}
Future<void> _verifyEmailOtp() async {
_setLoading(true);
try {
final user = await _retroAuth.verifyEmailOtp(
'test@example.com',
'123456',
serviceName: '/v1/authenticate',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('Email OTP verified! User ID: ${user.userId}');
} else {
_setResult('Email OTP verification returned null');
}
} catch (e) {
_setResult('Email OTP verification failed: $e');
} finally {
_setLoading(false);
}
}
// Phone OTP Methods
Future<void> _sendPhoneOtp() async {
_setLoading(true);
try {
final result = await _retroAuth.sendPhoneOtp(
'+1234567890',
serviceName: '/v1/authenticate',
);
_setResult('Phone OTP sent: $result');
} catch (e) {
_setResult('Send phone OTP failed: $e');
} finally {
_setLoading(false);
}
}
Future<void> _verifyPhoneOtp() async {
_setLoading(true);
try {
final user = await _retroAuth.verifyPhoneOtp(
'+1234567890',
'123456',
serviceName: '/v1/authenticate',
);
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('Phone OTP verified! User ID: ${user.userId}');
} else {
_setResult('Phone OTP verification returned null');
}
} catch (e) {
_setResult('Phone OTP verification failed: $e');
} finally {
_setLoading(false);
}
}
// Password Reset Methods
Future<void> _resetPasswordSendEmailOtp() async {
_setLoading(true);
try {
final result = await _retroAuth.resetPasswordSendEmailOtp(
'test@example.com',
serviceName: '/v1/authenticate',
);
_setResult('Reset password email OTP sent: $result');
} catch (e) {
_setResult('Reset password email OTP failed: $e');
} finally {
_setLoading(false);
}
}
Future<void> _resetPasswordVerifyEmailOtp() async {
_setLoading(true);
try {
final result = await _retroAuth.resetPasswordVerifyEmailOtp(
'test@example.com',
'123456',
serviceName: '/v1/authenticate',
);
_setResult('Reset password email OTP verified: $result');
} catch (e) {
_setResult('Reset password email OTP verification failed: $e');
} finally {
_setLoading(false);
}
}
// Other Methods
Future<void> _updatePassword() async {
if (_currentUser == null) {
_setResult('No user logged in to update password');
return;
}
_setLoading(true);
try {
final result = await _retroAuth.updatePassword(
'newPassword123',
serviceName: '/v1/authenticate',
);
_setResult('Password updated: $result');
} catch (e) {
_setResult('Password update failed: $e');
} finally {
_setLoading(false);
}
}
Future<void> _guestLogin() async {
_setLoading(true);
try {
final user = await _retroAuth.guestLogin(serviceName: '/v1/authenticate');
if (user != null) {
setState(() {
_currentUser = user;
});
_setResult('Guest login successful! User ID: ${user.userId}');
} else {
_setResult('Guest login returned null');
}
} catch (e) {
_setResult('Guest login failed: $e');
} finally {
_setLoading(false);
}
}
Future<void> _deleteAccount() async {
if (_currentUser == null) {
_setResult('No user logged in to delete account');
return;
}
_setLoading(true);
try {
final result = await _retroAuth.deleteAccount(
serviceName: '/v1/authenticate',
);
setState(() {
_currentUser = null;
});
_setResult('Account deleted: $result');
} catch (e) {
_setResult('Account deletion failed: $e');
} finally {
_setLoading(false);
}
}
Future<void> _logout() async {
if (_currentUser == null) {
_setResult('No user logged in to logout');
return;
}
_setLoading(true);
try {
final result = await _retroAuth.logout(serviceName: '/v1/authenticate');
setState(() {
_currentUser = null;
});
_setResult('Logout successful: $result');
} catch (e) {
_setResult('Logout failed: $e');
} finally {
_setLoading(false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Retro Auth - All Methods Demo'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: SingleChildScrollView(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Current User Status
if (_currentUser != null) ...[
Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Current User',
style: Theme.of(context).textTheme.titleLarge,
),
SizedBox(height: 8),
Text('ID: ${_currentUser!.userId}'),
// Text('Email: ${_currentUser.email}'),
],
),
),
),
SizedBox(height: 16),
],
// Last Result
Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Last Result',
style: Theme.of(context).textTheme.titleLarge,
),
SizedBox(height: 8),
Text(_lastResult),
],
),
),
),
SizedBox(height: 16),
// OAuth Methods
_buildMethodSection('OAuth Methods', [
_buildMethodButton('Login with OAuth', _loginWithOAuth),
]),
// Social Login Methods
_buildMethodSection('Social Login Methods', [
_buildMethodButton('Login with Apple', _loginWithApple),
_buildMethodButton('Login with Google', _loginWithGoogle),
_buildMethodButton('Login with Facebook', _loginWithFacebook),
_buildMethodButton('Login with Discord', _loginWithDiscord),
_buildMethodButton('Login with LinkedIn', _loginWithLinkedIn),
_buildMethodButton('Login with TrueCaller', _loginWithTrueCaller),
]),
// OTP Methods
_buildMethodSection('OTP Methods', [
_buildMethodButton('Send Email OTP', _sendEmailOtp),
_buildMethodButton('Verify Email OTP', _verifyEmailOtp),
_buildMethodButton('Send Phone OTP', _sendPhoneOtp),
_buildMethodButton('Verify Phone OTP', _verifyPhoneOtp),
]),
// Password Reset Methods
_buildMethodSection('Password Reset Methods', [
_buildMethodButton(
'Send Reset Email OTP',
_resetPasswordSendEmailOtp,
),
_buildMethodButton(
'Verify Reset Email OTP',
_resetPasswordVerifyEmailOtp,
),
]),
// Other Methods
_buildMethodSection('Other Methods', [
_buildMethodButton('Update Password', _updatePassword),
_buildMethodButton('Guest Login', _guestLogin),
_buildMethodButton('Delete Account', _deleteAccount),
_buildMethodButton('Logout', _logout),
]),
],
),
),
);
}
Widget _buildMethodSection(String title, List<Widget> children) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: Theme.of(
context,
).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold),
),
SizedBox(height: 8),
...children,
SizedBox(height: 16),
],
);
}
Widget _buildMethodButton(String text, VoidCallback onPressed) {
return Padding(
padding: EdgeInsets.only(bottom: 8.0),
child: ElevatedButton(
onPressed: _isLoading ? null : onPressed,
child: Text(text),
),
);
}
}