aad_oauth 0.4.1-beta.2 copy "aad_oauth: ^0.4.1-beta.2" to clipboard
aad_oauth: ^0.4.1-beta.2 copied to clipboard

A Flutter OAuth package for performing user authentication against Azure Active Directory OAuth2 v2.0 endpoint.

example/lib/main.dart

import 'package:aad_oauth/aad_oauth.dart';
import 'package:aad_oauth/model/config.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

final navigatorKey = GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AAD OAuth Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'AAD OAuth Home'),
      navigatorKey: navigatorKey,
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // Must configure flutter to start the web server for the app on
  // the port listed below. In VSCode, this can be done with
  // the following run settings in launch.json
  // "args": ["-d", "chrome","--web-port", "8483"]
  static final Config config = Config(
      tenant: 'YOUR_TENANT_ID',
      clientId: 'YOUR_CLIENT_ID',
      scope: 'openid profile offline_access',
      redirectUri: kIsWeb
          ? 'http://localhost:8483'
          : 'https://login.live.com/oauth20_desktop.srf',
      navigatorKey: navigatorKey,
      loader: SizedBox());
  final AadOAuth oauth = AadOAuth(config);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        children: <Widget>[
          ListTile(
            title: Text(
              'AzureAD OAuth',
              style: Theme.of(context).textTheme.headline5,
            ),
          ),
          ListTile(
            leading: Icon(Icons.launch),
            title: Text('Login'),
            onTap: () {
              login();
            },
          ),
          ListTile(
            leading: Icon(Icons.delete),
            title: Text('Logout'),
            onTap: () {
              logout();
            },
          ),
        ],
      ),
    );
  }

  void showError(dynamic ex) {
    showMessage(ex.toString());
  }

  void showMessage(String text) {
    var alert = AlertDialog(content: Text(text), actions: <Widget>[
      TextButton(
          child: const Text('Ok'),
          onPressed: () {
            Navigator.pop(context);
          })
    ]);
    showDialog(context: context, builder: (BuildContext context) => alert);
  }

  void login() async {
    final result = await oauth.login();
    result.fold(
      (l) => showError(l.toString()),
      (r) => showMessage('Logged in successfully, your access token: $r'),
    );
    var accessToken = await oauth.getAccessToken();
    if(accessToken != null) {
      ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text(accessToken)));
    }
  }

  void logout() async {
    await oauth.logout();
    showMessage('Logged out');
  }
}
145
likes
0
pub points
97%
popularity

Publisher

verified publisherearlybyte.ch

A Flutter OAuth package for performing user authentication against Azure Active Directory OAuth2 v2.0 endpoint.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

dartz, equatable, flutter, flutter_secure_storage, http, js, pedantic, shared_preferences, webview_flutter

More

Packages that depend on aad_oauth