medha_auth 1.0.1
medha_auth: ^1.0.1 copied to clipboard
A Flutter authentication library for SSO integration with Microsoft authentication
Medha Auth #
A Flutter authentication library for Medha SSO integration with Microsoft authentication.
Features #
- WebView-based Microsoft SSO authentication
- Custom URL scheme redirects for mobile apps
- Secure token storage with flutter_secure_storage
- Automatic token refresh
- State management with streams
- Support for QA and Production environments
Installation #
Add this to your pubspec.yaml:
dependencies:
medha_auth:
path: ../path/to/packages/medha_auth
Setup #
1. Initialize Navigation Service #
In your main.dart:
import 'package:medha_auth/medha_auth.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
@override
Widget build(BuildContext context) {
// Initialize navigation service
NavigationService.setNavigatorKey(navigatorKey);
return MaterialApp(
navigatorKey: navigatorKey,
home: MyHomePage(),
);
}
}
2. Configure Custom URL Scheme #
iOS (ios/Runner/Info.plist):
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>medhaauth</string>
<key>CFBundleURLSchemes</key>
<array>
<string>medhaauth</string>
</array>
</dict>
</array>
Android (android/app/src/main/AndroidManifest.xml):
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme">
<!-- Other intent filters -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="medhaauth" />
</intent-filter>
</activity>
Usage #
1. Initialize Auth Client #
import 'package:medha_auth/medha_auth.dart';
class AuthService {
late AuthClient _authClient;
Future<void> initialize() async {
_authClient = AuthClient(
config: AuthConfig.qa(), // or AuthConfig.production()
);
await _authClient.initialize();
}
Stream<AuthState> get authState => _authClient.state;
}
2. Login #
try {
final result = await _authClient.login();
print('Login successful: ${result.userInfo}');
} catch (e) {
print('Login failed: $e');
}
3. Check Authentication State #
StreamBuilder<AuthState>(
stream: _authClient.state,
builder: (context, snapshot) {
final state = snapshot.data ?? const AuthState.unknown();
return state.when(
unknown: () => CircularProgressIndicator(),
loading: () => CircularProgressIndicator(),
authenticated: (tokens, userInfo) => AuthenticatedScreen(),
unauthenticated: (error) => LoginScreen(),
refreshing: (tokens) => RefreshingScreen(),
);
},
);
4. Get Valid Tokens #
final tokens = await _authClient.getValidTokens();
if (tokens != null) {
// Use tokens for API calls
final response = await http.get(
Uri.parse('https://api.example.com/data'),
headers: {
'Authorization': 'Bearer ${tokens.accessToken}',
},
);
}
5. Logout #
await _authClient.logout();
Configuration #
QA Environment #
AuthClient(config: AuthConfig.qa())
Production Environment #
AuthClient(config: AuthConfig.production())
Custom Configuration #
AuthClient(
config: AuthConfig(
ssoBaseUrl: 'https://your-sso.example.com:3000',
backendBaseUrl: 'https://your-backend.example.com:4000',
customScheme: 'yourapp',
),
)
Error Handling #
The library provides specific exception types:
AuthException- General authentication errorsAuthTimeoutException- Authentication timeoutAuthCancelledException- User cancelled authenticationAuthRedirectException- Redirect handling errorsAuthTokenException- Token-related errors
License #
Private - Medha Analytics