native_authentication 0.1.0+2 copy "native_authentication: ^0.1.0+2" to clipboard
native_authentication: ^0.1.0+2 copied to clipboard

Native bindings for platform-specific authentication APIs like ASWebAuthenticationSession and Chrome Custom Tabs.

example/lib/main.dart

import 'dart:developer';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:native_authentication/native_authentication.dart';

final NativeAuthentication nativeAuth = NativeAuthentication(
  logger: Logger.root,
);

void main() {
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((record) {
    log(
      record.message,
      level: record.level.value,
      name: record.loggerName,
    );
  });
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  Future<String> _result = Future<String>.value('awaiting callback');

  CallbackType get callbackType {
    const localhost = CallbackType.localhost(port: 7777);
    if (kIsWeb) {
      return localhost;
    }
    return switch (Platform.operatingSystem) {
      'ios' || 'android' || 'macos' => const CallbackType.custom('celest'),
      _ => localhost,
    };
  }

  Future<void> _performCallback() async {
    final session = nativeAuth.startCallback(
      uri: Uri.parse('https://my-authorization-server'),
      type: callbackType,
    );
    setState(() {
      _result = session.redirectUri.then((uri) => 'callback: $uri');
    });
  }

  @override
  Widget build(BuildContext context) {
    const textStyle = TextStyle(fontSize: 25);
    const spacerSmall = SizedBox(height: 10);
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Native Authentication'),
        ),
        body: Center(
          child: Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: _performCallback,
                  child: const Text('PERFORM CALLBACK'),
                ),
                spacerSmall,
                FutureBuilder<String>(
                  future: _result,
                  builder: (BuildContext context, AsyncSnapshot<String> value) {
                    final displayValue = (value.hasData)
                        ? value.data
                        : value.hasError
                            ? '${value.error}'
                            : 'loading';
                    return Text(
                      'result = $displayValue',
                      style: textStyle,
                      textAlign: TextAlign.center,
                    );
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
1
likes
150
points
1
downloads

Publisher

verified publishercelest.dev

Weekly Downloads

Native bindings for platform-specific authentication APIs like ASWebAuthenticationSession and Chrome Custom Tabs.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-2-Clause-Patent (license)

Dependencies

async, collection, ffi, jni, logging, meta, native_assets_cli, native_toolchain_c, objective_c, path, stream_transform, web

More

Packages that depend on native_authentication