medha_auth 1.0.4
medha_auth: ^1.0.4 copied to clipboard
A Flutter authentication library for SSO integration with Microsoft authentication
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:medha_auth/medha_auth.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
MyApp({super.key});
@override
Widget build(BuildContext context) {
// Initialize navigation service
NavigationService.setNavigatorKey(navigatorKey);
return MaterialApp(
title: 'Medha Auth Example',
navigatorKey: navigatorKey,
home: AuthScreen(),
);
}
}
class AuthScreen extends StatefulWidget {
const AuthScreen({super.key});
@override
_AuthScreenState createState() => _AuthScreenState();
}
class _AuthScreenState extends State<AuthScreen> {
late AuthClient _authClient;
@override
void initState() {
super.initState();
_initializeAuth();
}
Future<void> _initializeAuth() async {
// Example: Configure AuthConfig with your own URLs and application name
// In production, load these from environment variables or config files
_authClient = AuthClient(
config: AuthConfig(
ssoBaseUrl: 'https://qa-medhasso.medha-analytics.ai',
backendBaseUrl: 'https://qa-medhasso-service.medha-analytics.ai',
applicationName: 'MedhaAuthExample',
customScheme: 'medhaauth',
),
);
await _authClient.initialize();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Medha Auth Example')),
body: StreamBuilder<AuthState>(
stream: _authClient.state,
builder: (context, snapshot) {
final state = snapshot.data ?? const AuthState.unknown();
return Center(
child: state.when(
unknown: () => CircularProgressIndicator(),
loading: () => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('Authenticating...'),
],
),
authenticated: (tokens, userInfo) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.check_circle, color: Colors.green, size: 64),
SizedBox(height: 16),
Text('Welcome ${userInfo?['name'] ?? 'User'}!'),
SizedBox(height: 16),
ElevatedButton(
onPressed: () => _authClient.logout(),
child: Text('Logout'),
),
],
),
unauthenticated: (error) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.login, size: 64),
SizedBox(height: 16),
Text('Please login to continue'),
if (error != null) ...[
SizedBox(height: 8),
Text('Error: $error', style: TextStyle(color: Colors.red)),
],
SizedBox(height: 16),
ElevatedButton(
onPressed: () => _authClient.login(),
child: Text('Login with Microsoft'),
),
],
),
refreshing: (tokens) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('Refreshing tokens...'),
],
),
),
);
},
),
);
}
@override
void dispose() {
_authClient.dispose();
super.dispose();
}
}