Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<PageRoute> routeObserver = RouteObserver<PageRoute>();

Expand Down Expand Up @@ -72,7 +72,7 @@ class _BodyWidgetState extends State<BodyWidget> {

Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => LoginScreen()),
MaterialPageRoute(builder: (context) => AuthScreen()),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginScreen> {
class _AuthScreenState extends State<AuthScreen> {
bool isLoading = false;
List<StreamSubscription<OWEvent>>? registrationSubscriptions;
List<StreamSubscription<OWEvent>>? authenticationSubscriptions;
List<OWUserProfile> userProfiles = [];
String? selectedProfileId;

@override
initState() {
Expand All @@ -35,18 +33,16 @@ class _LoginScreenState extends State<LoginScreen> {
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);

Expand All @@ -71,7 +67,7 @@ class _LoginScreenState extends State<LoginScreen> {
}
}

registrationWithIdentityProvider(String identityProviderId) async {
_registraterWithIdentityProvider(String identityProviderId) async {
setState(() => isLoading = true);
try {
var registrationResponse = await Onegini.instance.userClient.registerUser(
Expand All @@ -94,25 +90,7 @@ class _LoginScreenState extends State<LoginScreen> {
}
}

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<dynamic> route) => false);
} catch (error) {
if (error is PlatformException) {
showFlutterToast(error.message);
}
}
}

cancelRegistration() async {
_cancelRegistration() async {
setState(() => isLoading = false);
try {
await Future.any([
Expand All @@ -124,53 +102,6 @@ class _LoginScreenState extends State<LoginScreen> {
}
}

Future<List<OWUserProfile>> 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<String> 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<String>(
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(
Expand All @@ -195,7 +126,7 @@ class _LoginScreenState extends State<LoginScreen> {
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 20),
_buildLoginWidget(),
LoginSection(),
SizedBox(height: 20),
_buildRegisterWidget(),
],
Expand All @@ -204,59 +135,6 @@ class _LoginScreenState extends State<LoginScreen> {
);
}

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<OWUserProfile> 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: [
Expand All @@ -267,7 +145,7 @@ class _LoginScreenState extends State<LoginScreen> {
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
openWeb();
_openWeb();
},
child: Text('Run WEB'),
),
Expand All @@ -290,7 +168,7 @@ class _LoginScreenState extends State<LoginScreen> {
),
),
onSelected: (value) {
registrationWithIdentityProvider(value);
_registraterWithIdentityProvider(value);
},
itemBuilder: (context) {
return identityProviders
Expand All @@ -316,11 +194,149 @@ class _LoginScreenState extends State<LoginScreen> {
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
cancelRegistration();
_cancelRegistration();
},
child: Text('Cancel'),
),
],
);
}
}

class LoginSection extends StatefulWidget {
@override
_LoginSectionState createState() => _LoginSectionState();
}

class _LoginSectionState extends State<LoginSection> {
List<OWUserProfile> 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<OWUserProfile> profiles) {
return DropdownButton(
value: selectedProfileId,
items: profiles
.map((e) =>
DropdownMenuItem(value: e.profileId, child: Text(e.profileId)))
.toList(),
onChanged: (profileId) =>
{setState(() => selectedProfileId = profileId)});
}

Future<List<OWUserProfile>> _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<String>(
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<String> _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<dynamic> route) => false);
} catch (error) {
if (error is PlatformException) {
showFlutterToast(error.message);
}
}
}
}
Loading