From 4cc09ba8879e7f6fd523403576dae226b1436ecd Mon Sep 17 00:00:00 2001 From: Michiel Vrins Date: Tue, 2 May 2023 16:52:46 +0200 Subject: [PATCH 1/2] FP-87: Move loginSection into seperate widget and rename login_screen --- example/lib/main.dart | 4 +- .../{login_screen.dart => auth_screen.dart} | 270 ++++++++++-------- example/lib/screens/otp_screen.dart | 4 +- example/lib/screens/user_screen.dart | 8 +- 4 files changed, 151 insertions(+), 135 deletions(-) rename example/lib/screens/{login_screen.dart => auth_screen.dart} (86%) 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 86% rename from example/lib/screens/login_screen.dart rename to example/lib/screens/auth_screen.dart index cbe4729c..2b373e2a 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,14 +33,12 @@ class _LoginScreenState extends State { this.authenticationSubscriptions = OWBroadcastHelper.initAuthenticationSubscriptions(context); super.initState(); - getUserProfiles(); } @override void dispose() { OWBroadcastHelper.stopListening(registrationSubscriptions); OWBroadcastHelper.stopListening(authenticationSubscriptions); - super.dispose(); } @@ -94,24 +90,6 @@ 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 { setState(() => isLoading = false); try { @@ -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,60 +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: [ @@ -325,3 +202,142 @@ 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 7750e2dd..00c77320 100644 --- a/example/lib/screens/user_screen.dart +++ b/example/lib/screens/user_screen.dart @@ -17,7 +17,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; @@ -112,7 +112,7 @@ class _UserScreenState extends State with RouteAware { }); Navigator.pushReplacement( context, - MaterialPageRoute(builder: (_) => LoginScreen()), + MaterialPageRoute(builder: (_) => AuthScreen()), ); } @@ -126,7 +126,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 == "10012") { Navigator.pushReplacement( context, - MaterialPageRoute(builder: (_) => LoginScreen()), + MaterialPageRoute(builder: (_) => AuthScreen()), ); } } From e82edce770d13ac7fa7b9af2729d8173d896bc3d Mon Sep 17 00:00:00 2001 From: Michiel Vrins Date: Fri, 26 May 2023 16:04:08 +0200 Subject: [PATCH 2/2] FP-87: Convert methods to private in auth_screen --- example/lib/screens/auth_screen.dart | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/example/lib/screens/auth_screen.dart b/example/lib/screens/auth_screen.dart index 26c7cf7e..e254053a 100644 --- a/example/lib/screens/auth_screen.dart +++ b/example/lib/screens/auth_screen.dart @@ -42,7 +42,7 @@ class _AuthScreenState extends State { super.dispose(); } - openWeb() async { + _openWeb() async { /// Start registration setState(() => isLoading = true); @@ -67,7 +67,7 @@ class _AuthScreenState extends State { } } - registrationWithIdentityProvider(String identityProviderId) async { + _registraterWithIdentityProvider(String identityProviderId) async { setState(() => isLoading = true); try { var registrationResponse = await Onegini.instance.userClient.registerUser( @@ -90,7 +90,7 @@ class _AuthScreenState extends State { } } - cancelRegistration() async { + _cancelRegistration() async { setState(() => isLoading = false); try { await Future.any([ @@ -145,7 +145,7 @@ class _AuthScreenState extends State { SizedBox(height: 20), ElevatedButton( onPressed: () { - openWeb(); + _openWeb(); }, child: Text('Run WEB'), ), @@ -168,7 +168,7 @@ class _AuthScreenState extends State { ), ), onSelected: (value) { - registrationWithIdentityProvider(value); + _registraterWithIdentityProvider(value); }, itemBuilder: (context) { return identityProviders @@ -194,7 +194,7 @@ class _AuthScreenState extends State { SizedBox(height: 20), ElevatedButton( onPressed: () { - cancelRegistration(); + _cancelRegistration(); }, child: Text('Cancel'), ), @@ -215,7 +215,7 @@ class _LoginSectionState extends State { @override initState() { super.initState(); - fetchUserProfiles(); + _fetchUserProfiles(); } @override @@ -235,7 +235,7 @@ class _LoginSectionState extends State { _buildImplicitUserData(_selectedProfileId), ElevatedButton( onPressed: () { - authenticate(_selectedProfileId, null); + _authenticate(_selectedProfileId, null); }, child: Text('Preferred authenticator'), ), @@ -244,7 +244,7 @@ class _LoginSectionState extends State { children: [ ElevatedButton( onPressed: () { - authenticate( + _authenticate( _selectedProfileId, OWAuthenticatorType.pin); }, child: Text('Pin'), @@ -252,7 +252,7 @@ class _LoginSectionState extends State { SizedBox(height: 10, width: 10), ElevatedButton( onPressed: () { - authenticate( + _authenticate( _selectedProfileId, OWAuthenticatorType.biometric); }, child: Text('Biometrics'), @@ -274,7 +274,7 @@ class _LoginSectionState extends State { {setState(() => selectedProfileId = profileId)}); } - Future> fetchUserProfiles() async { + Future> _fetchUserProfiles() async { try { final profiles = await Onegini.instance.userClient.getUserProfiles(); setState(() { @@ -291,7 +291,7 @@ class _LoginSectionState extends State { Widget _buildImplicitUserData(final String profileId) { return FutureBuilder( - future: getImplicitUserDetails(profileId), + future: _getImplicitUserDetails(profileId), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) { @@ -303,7 +303,7 @@ class _LoginSectionState extends State { }); } - Future getImplicitUserDetails(final String profileId) async { + Future _getImplicitUserDetails(final String profileId) async { try { await Onegini.instance.userClient .authenticateUserImplicitly(profileId, ["read"]); @@ -321,7 +321,7 @@ class _LoginSectionState extends State { } } - authenticate( + _authenticate( final String profileId, OWAuthenticatorType? authenticatorType) async { try { var registrationResponse = await Onegini.instance.userClient