diff --git a/example/lib/main.dart b/example/lib/main.dart index 6eccea43..ebf34aa0 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -3,7 +3,7 @@ import 'package:flutter/services.dart'; import 'package:onegini/onegini.dart'; import 'package:onegini/onegini.gen.dart'; import 'package:onegini_example/components/display_toast.dart'; -import 'package:onegini_example/screens/login_screen.dart'; +import 'package:onegini_example/screens/auth_screen.dart'; final RouteObserver routeObserver = RouteObserver(); @@ -72,7 +72,7 @@ class _BodyWidgetState extends State { Navigator.pushReplacement( context, - MaterialPageRoute(builder: (context) => LoginScreen()), + MaterialPageRoute(builder: (context) => AuthScreen()), ); } diff --git a/example/lib/screens/login_screen.dart b/example/lib/screens/auth_screen.dart similarity index 84% rename from example/lib/screens/login_screen.dart rename to example/lib/screens/auth_screen.dart index 4fd74a61..e254053a 100644 --- a/example/lib/screens/login_screen.dart +++ b/example/lib/screens/auth_screen.dart @@ -15,17 +15,15 @@ import 'package:collection/collection.dart'; import '../components/display_toast.dart'; -class LoginScreen extends StatefulWidget { +class AuthScreen extends StatefulWidget { @override - _LoginScreenState createState() => _LoginScreenState(); + _AuthScreenState createState() => _AuthScreenState(); } -class _LoginScreenState extends State { +class _AuthScreenState extends State { bool isLoading = false; List>? registrationSubscriptions; List>? authenticationSubscriptions; - List userProfiles = []; - String? selectedProfileId; @override initState() { @@ -35,18 +33,16 @@ class _LoginScreenState extends State { this.authenticationSubscriptions = OWBroadcastHelper.initAuthenticationSubscriptions(context); super.initState(); - getUserProfiles(); } @override void dispose() { OWBroadcastHelper.stopListening(registrationSubscriptions); OWBroadcastHelper.stopListening(authenticationSubscriptions); - super.dispose(); } - openWeb() async { + _openWeb() async { /// Start registration setState(() => isLoading = true); @@ -71,7 +67,7 @@ class _LoginScreenState extends State { } } - registrationWithIdentityProvider(String identityProviderId) async { + _registraterWithIdentityProvider(String identityProviderId) async { setState(() => isLoading = true); try { var registrationResponse = await Onegini.instance.userClient.registerUser( @@ -94,25 +90,7 @@ class _LoginScreenState extends State { } } - authenticate(String profileId, OWAuthenticatorType? authenticatorType) async { - try { - var registrationResponse = await Onegini.instance.userClient - .authenticateUser(profileId, authenticatorType); - Navigator.pushAndRemoveUntil( - context, - MaterialPageRoute( - builder: (context) => UserScreen( - userProfileId: registrationResponse.userProfile.profileId, - )), - (Route route) => false); - } catch (error) { - if (error is PlatformException) { - showFlutterToast(error.message); - } - } - } - - cancelRegistration() async { + _cancelRegistration() async { setState(() => isLoading = false); try { await Future.any([ @@ -124,53 +102,6 @@ class _LoginScreenState extends State { } } - Future> getUserProfiles() async { - try { - final profiles = await Onegini.instance.userClient.getUserProfiles(); - setState(() { - userProfiles = profiles; - if (selectedProfileId == null) { - selectedProfileId = profiles.firstOrNull?.profileId; - } - }); - return profiles; - } catch (err) { - print("caught error in getUserProfiles: $err"); - return []; - } - } - - Future getImplicitUserDetails(String profileId) async { - try { - await Onegini.instance.userClient - .authenticateUserImplicitly(profileId, ["read"]); - var response = await Onegini.instance.resourcesMethods.requestResource( - ResourceRequestType.implicit, - RequestDetails( - path: "user-id-decorated", method: HttpRequestMethod.get)); - - var res = json.decode(response.body); - - return res["decorated_user_id"]; - } catch (err) { - print("Caught error: $err"); - return "Error occured check logs"; - } - } - - Widget _buildImplicitUserData(String profileId) { - return FutureBuilder( - future: getImplicitUserDetails(profileId), - builder: (context, snapshot) { - if (snapshot.hasData) { - return Text("${snapshot.data}"); - } else if (snapshot.hasError) { - return Text("Error getting implicit details."); - } - return CircularProgressIndicator(); - }); - } - @override Widget build(BuildContext context) { return Scaffold( @@ -195,7 +126,7 @@ class _LoginScreenState extends State { mainAxisAlignment: MainAxisAlignment.center, children: [ SizedBox(height: 20), - _buildLoginWidget(), + LoginSection(), SizedBox(height: 20), _buildRegisterWidget(), ], @@ -204,59 +135,6 @@ class _LoginScreenState extends State { ); } - Widget _buildLoginWidget() { - final profileId = selectedProfileId; - return (userProfiles.length > 0 && profileId != null) - ? Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - "──── Login ────", - style: TextStyle(fontSize: 30), - textAlign: TextAlign.center, - ), - _buildSelectUserProfile(userProfiles), - _buildImplicitUserData(profileId), - ElevatedButton( - onPressed: () { - authenticate(profileId, null); - }, - child: Text('Preferred authenticator'), - ), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - ElevatedButton( - onPressed: () { - authenticate(profileId, OWAuthenticatorType.pin); - }, - child: Text('Pin'), - ), - SizedBox(height: 10, width: 10), - ElevatedButton( - onPressed: () { - authenticate(profileId, OWAuthenticatorType.biometric); - }, - child: Text('Biometrics'), - ), - ], - ), - ]) - : SizedBox.shrink(); - } - - DropdownButton _buildSelectUserProfile(List profiles) { - return DropdownButton( - value: selectedProfileId, - items: profiles - .map((e) => - DropdownMenuItem(value: e.profileId, child: Text(e.profileId))) - .toList(), - onChanged: (profileId) => - {setState(() => selectedProfileId = profileId)}); - } - Column _buildRegisterWidget() { return Column( children: [ @@ -267,7 +145,7 @@ class _LoginScreenState extends State { SizedBox(height: 20), ElevatedButton( onPressed: () { - openWeb(); + _openWeb(); }, child: Text('Run WEB'), ), @@ -290,7 +168,7 @@ class _LoginScreenState extends State { ), ), onSelected: (value) { - registrationWithIdentityProvider(value); + _registraterWithIdentityProvider(value); }, itemBuilder: (context) { return identityProviders @@ -316,7 +194,7 @@ class _LoginScreenState extends State { SizedBox(height: 20), ElevatedButton( onPressed: () { - cancelRegistration(); + _cancelRegistration(); }, child: Text('Cancel'), ), @@ -324,3 +202,141 @@ class _LoginScreenState extends State { ); } } + +class LoginSection extends StatefulWidget { + @override + _LoginSectionState createState() => _LoginSectionState(); +} + +class _LoginSectionState extends State { + List userProfiles = []; + String? selectedProfileId; + + @override + initState() { + super.initState(); + _fetchUserProfiles(); + } + + @override + Widget build(BuildContext context) { + final _selectedProfileId = selectedProfileId; + return (userProfiles.length > 0 && _selectedProfileId != null) + ? Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + "──── Login ────", + style: TextStyle(fontSize: 30), + textAlign: TextAlign.center, + ), + _buildSelectUserProfile(userProfiles), + _buildImplicitUserData(_selectedProfileId), + ElevatedButton( + onPressed: () { + _authenticate(_selectedProfileId, null); + }, + child: Text('Preferred authenticator'), + ), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + ElevatedButton( + onPressed: () { + _authenticate( + _selectedProfileId, OWAuthenticatorType.pin); + }, + child: Text('Pin'), + ), + SizedBox(height: 10, width: 10), + ElevatedButton( + onPressed: () { + _authenticate( + _selectedProfileId, OWAuthenticatorType.biometric); + }, + child: Text('Biometrics'), + ), + ], + ), + ]) + : SizedBox.shrink(); + } + + DropdownButton _buildSelectUserProfile(List profiles) { + return DropdownButton( + value: selectedProfileId, + items: profiles + .map((e) => + DropdownMenuItem(value: e.profileId, child: Text(e.profileId))) + .toList(), + onChanged: (profileId) => + {setState(() => selectedProfileId = profileId)}); + } + + Future> _fetchUserProfiles() async { + try { + final profiles = await Onegini.instance.userClient.getUserProfiles(); + setState(() { + userProfiles = profiles; + selectedProfileId = + selectedProfileId ?? profiles.firstOrNull?.profileId; + }); + return profiles; + } catch (err) { + print("caught error in getUserProfiles: $err"); + return []; + } + } + + Widget _buildImplicitUserData(final String profileId) { + return FutureBuilder( + future: _getImplicitUserDetails(profileId), + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.done && + snapshot.hasData) { + return Text("${snapshot.data}"); + } else if (snapshot.hasError) { + return Text("Error getting implicit details."); + } + return CircularProgressIndicator(); + }); + } + + Future _getImplicitUserDetails(final String profileId) async { + try { + await Onegini.instance.userClient + .authenticateUserImplicitly(profileId, ["read"]); + var response = await Onegini.instance.resourcesMethods.requestResource( + ResourceRequestType.implicit, + RequestDetails( + path: "user-id-decorated", method: HttpRequestMethod.get)); + + var res = json.decode(response.body); + + return res["decorated_user_id"]; + } catch (err) { + print("Caught error: $err"); + return "Error occured check logs"; + } + } + + _authenticate( + final String profileId, OWAuthenticatorType? authenticatorType) async { + try { + var registrationResponse = await Onegini.instance.userClient + .authenticateUser(profileId, authenticatorType); + Navigator.pushAndRemoveUntil( + context, + MaterialPageRoute( + builder: (context) => UserScreen( + userProfileId: registrationResponse.userProfile.profileId, + )), + (Route route) => false); + } catch (error) { + if (error is PlatformException) { + showFlutterToast(error.message); + } + } + } +} diff --git a/example/lib/screens/otp_screen.dart b/example/lib/screens/otp_screen.dart index 725e9b0e..f7b6573f 100644 --- a/example/lib/screens/otp_screen.dart +++ b/example/lib/screens/otp_screen.dart @@ -3,7 +3,7 @@ import 'package:flutter/services.dart'; import 'package:onegini/callbacks/onegini_custom_registration_callback.dart'; import 'package:onegini_example/components/display_toast.dart'; -import 'login_screen.dart'; +import 'auth_screen.dart'; class OtpScreen extends StatefulWidget { final String password; @@ -42,7 +42,7 @@ class _OtpScreenState extends State { }); Navigator.pushReplacement( context, - MaterialPageRoute(builder: (context) => LoginScreen()), + MaterialPageRoute(builder: (context) => AuthScreen()), ); } diff --git a/example/lib/screens/user_screen.dart b/example/lib/screens/user_screen.dart index 4a2f15c2..2312f874 100644 --- a/example/lib/screens/user_screen.dart +++ b/example/lib/screens/user_screen.dart @@ -18,7 +18,7 @@ import 'package:onegini/onegini.gen.dart'; // ignore: import_of_legacy_library_into_null_safe import '../main.dart'; // ignore: import_of_legacy_library_into_null_safe -import 'login_screen.dart'; +import 'auth_screen.dart'; class UserScreen extends StatefulWidget { final String userProfileId; @@ -113,7 +113,7 @@ class _UserScreenState extends State with RouteAware { }); Navigator.pushReplacement( context, - MaterialPageRoute(builder: (_) => LoginScreen()), + MaterialPageRoute(builder: (_) => AuthScreen()), ); } @@ -127,7 +127,7 @@ class _UserScreenState extends State with RouteAware { }); Navigator.pushReplacement( context, - MaterialPageRoute(builder: (_) => LoginScreen()), + MaterialPageRoute(builder: (_) => AuthScreen()), ); } @@ -144,7 +144,7 @@ class _UserScreenState extends State with RouteAware { error.code == PlatformErrorCodes.userNotAuthenticated) { Navigator.pushReplacement( context, - MaterialPageRoute(builder: (_) => LoginScreen()), + MaterialPageRoute(builder: (_) => AuthScreen()), ); } }