mobileApplicationsKonectarApp/lib/openid/openid_screen.dart

161 lines
4.4 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
//import 'package:flutter_login_test/openid_browser.dart';
import 'package:openid_client/openid_client.dart';
import 'package:pwa_ios/views/konectarpage.dart';
import 'openid_io.dart' if (dart.library.html) 'openid_browser.dart';
const keycloakUri = 'https://sso.konectar.io/auth/realms/konectar';
const scopes = ['profile'];
late final Client client;
class OpenidScreen extends StatelessWidget {
OpenidScreen({super.key, this.credential});
Credential? credential;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'openid_client demo',
home: LoginPage(
title: 'Login',
credential: credential,
),
);
}
}
class LoginPage extends StatefulWidget {
LoginPage({super.key, required this.title, required this.credential});
Credential? credential;
final String title;
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
UserInfo? userInfo;
Future<Client> getClient() async {
var uri = Uri.parse(keycloakUri);
if (!kIsWeb && Platform.isAndroid)
uri = uri.replace(host: 'sso.konectar.io');
var clientId = 'appwildcard';
var issuer = await Issuer.discover(uri);
return Client(issuer, clientId);
}
_asyncMethod() async {
client = await getClient();
var credential = await authenticate(client, scopes: scopes);
var userInfo = await credential.getUserInfo();
setState(() {
this.userInfo = userInfo;
Timer.run(() {
// import 'dart:async:
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const MyApp()));
});
});
}
@override
void initState() {
if (widget.credential != null) {
widget.credential!.getUserInfo().then((userInfo) {
setState(() {
this.userInfo = userInfo;
// Timer.run(() { // import 'dart:async:
// Navigator.of(context).pushNamed('/details');
// });
});
});
} else {
WidgetsBinding.instance.addPostFrameCallback((_) {
_asyncMethod();
});
}
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
if (userInfo != null) ...[
Text('Hello ${userInfo!.name}'),
Text(userInfo!.email ?? ''),
OutlinedButton(
child: const Text('Logout'),
onPressed: () async {
setState(() {
userInfo = null;
});
})
],
if (userInfo == null)
const CircularProgressIndicator(
backgroundColor: Colors.blue,
semanticsLabel: 'Please wait..',
)
// OutlinedButton(
// child: const Text('Login'),
// onPressed: () async {
// var credential = await authenticate(client, scopes: scopes);
// var userInfo = await credential.getUserInfo();
// setState(() {
// this.userInfo = userInfo;
// });
// }),
],
),
),
);
}
}
// import 'firebase_options.dart';
// Future<void> main() async {
// WidgetsFlutterBinding.ensureInitialized();
// // iOS requires you run in release mode to test dynamic links ("flutter run --release").
// await Firebase.initializeApp(
// // options: DefaultFirebaseOptions.currentPlatform,
// );
// // runApp(
// // MaterialApp(
// // title: 'Dynamic Links Example',
// // routes: <String, WidgetBuilder>{
// // '/': (BuildContext context) => _MainScreen(),
// // '/helloworld': (BuildContext context) => _DynamicLinkScreen(),
// // },
// // ),
// // );
// runApp(
// MaterialApp(
// title: 'Dynamic Links Example',
// routes: <String, WidgetBuilder>{
// '/': (BuildContext context) => Login(),
// '/details': (BuildContext context) => Login(),
// },
// ),
// );
// //runApp(MaterialApp.router(routerConfig: router));
// }