azure_auth 4.0.0 copy "azure_auth: ^4.0.0" to clipboard
azure_auth: ^4.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: "d0d50091-7460-441c-b8b8-65cbfc813061",
      clientId: "c119ad8f-36b8-485d-8022-0cb40631c0dd",
      redirectUri: "com.mangotree.vscompass://vsauth",
      scope:
"openid profile email offline_access api://c119ad8f-36b8-485d-8022-0cb40631c0dd/scope_vs_auth_test"
    );
    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: "login_all_global_Inch",
        loginAsText: "Login",
      ),
    );
  }
}
1
likes
110
points
92
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter package for handling authentication using **Azure Active Directory B2C**, supporting **Facebook**, **Google**, **Email/Password**, and **Phone** sign-in through Azure's identity providers.

Homepage

License

MIT (license)

Dependencies

app_links, auto_route, crypto, dart_jsonwebtoken, dio, flutter, flutter_bloc, flutter_secure_storage, flutter_svg, flutter_web_auth_2, form_validator, get, google_fonts, json_serializable

More

Packages that depend on azure_auth