azure_auth 5.0.0
azure_auth: ^5.0.0 copied to clipboard
Flutter package for handling authentication using **Azure Active Directory B2C**, supporting **Facebook**, **Google**, **Email/Password**, and **Phone** sign-in through Azure's identity providers.
example/lib/main.dart
import 'dart:async';
import 'package:app_links/app_links.dart';
import 'package:azure_auth/features/auth/cubit/auth_cubit.dart';
import 'package:azure_auth/login_screen.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); // ⛔ removes status bar
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final AppLinks _appLinks = AppLinks();
StreamSubscription<Uri>? _linkSubscription;
late final AuthCubit _authCubit; // 👈 make it accessible
@override
void initState() {
// TODO: implement initState
super.initState();
_authCubit = AuthCubit(
// 👈 initialize it here
tenantId: "1a447afa-7141-484d-bb00-9faf34f2056c",
clientId: "c8ef13b1-01bb-4a68-b50a-766d65f34286",
redirectUri: "com.mangotree.vastuwheels://vsauth",
scope:
"openid profile email offline_access api://c8ef13b1-01bb-4a68-b50a-766d65f34286/scope_auth_prod",
);
initDeepLinks();
}
Future<void> initDeepLinks() async {
print('[vsauth] initDeepLinks: started');
try {
// 1) Cold start / opened by link: on iOS the URL is often only in getInitialLink
await Future.delayed(const Duration(milliseconds: 500)); // give iOS time to deliver URL
final initialUri = await _appLinks.getInitialLink();
print('[vsauth] initDeepLinks: getInitialLink() = $initialUri');
if (initialUri != null && _isVsauthCallback(initialUri)) {
print('[vsauth] initDeepLinks: handling initial URI');
await _handleAuthUri(initialUri);
return;
}
// 2) App resumed by link (e.g. Android, or iOS when URL goes to app_links)
_linkSubscription = _appLinks.uriLinkStream.listen(
(Uri uri) async {
print('[vsauth] initDeepLinks: stream received URI: $uri');
if (_isVsauthCallback(uri)) {
await _handleAuthUri(uri);
}
},
onError: (e) => print('[vsauth] initDeepLinks: stream error $e'),
);
print('[vsauth] initDeepLinks: uriLinkStream listening');
} catch (e) {
print('[vsauth] initDeepLinks: failed $e');
}
}
bool _isVsauthCallback(Uri uri) {
return uri.toString().contains('vsauth') && uri.queryParameters['code'] != null;
}
Future<void> _handleAuthUri(Uri uri) async {
print('[vsauth] _handleAuthUri: exchanging code for token');
final Map<String, dynamic>? userData = await _authCubit.getAuthrorisationCode(uri);
debugPrint('[vsauth] User data: $userData');
}
@override
void dispose() {
_linkSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: AuthScreen(
authCubit: _authCubit,
onLoginSuccess: () {},
onAuthCallbackUrl: _handleAuthUri,
commonFlowName: "Global_Inch_Auth_Mobile_Prod",
loginAsText: "Login",
),
);
}
}