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
265 changes: 138 additions & 127 deletions example/lib/screens/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:onegini/onegini.dart';
import 'package:onegini/onegini.gen.dart';
import 'package:onegini_example/ow_broadcast_helper.dart';
import 'package:onegini_example/screens/user_screen.dart';
import 'package:collection/collection.dart';

import '../components/display_toast.dart';

Expand All @@ -23,6 +24,8 @@ class _LoginScreenState extends State<LoginScreen> {
bool isLoading = false;
List<StreamSubscription<OWEvent>>? registrationSubscriptions;
List<StreamSubscription<OWEvent>>? authenticationSubscriptions;
List<OWUserProfile> userProfiles = [];
String? selectedProfileId;

@override
initState() {
Expand All @@ -31,8 +34,8 @@ class _LoginScreenState extends State<LoginScreen> {
OWBroadcastHelper.initRegistrationSubscriptions(context);
this.authenticationSubscriptions =
OWBroadcastHelper.initAuthenticationSubscriptions(context);

super.initState();
getUserProfiles();
}

@override
Expand Down Expand Up @@ -123,7 +126,13 @@ class _LoginScreenState extends State<LoginScreen> {

Future<List<OWUserProfile>> getUserProfiles() async {
try {
var profiles = await Onegini.instance.userClient.getUserProfiles();
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");
Expand All @@ -132,7 +141,6 @@ class _LoginScreenState extends State<LoginScreen> {
}

Future<String> getImplicitUserDetails(String profileId) async {
var returnString = "";
try {
await Onegini.instance.userClient
.authenticateUserImplicitly(profileId, ["read"]);
Expand All @@ -143,9 +151,7 @@ class _LoginScreenState extends State<LoginScreen> {

var res = json.decode(response.body);

returnString = res["decorated_user_id"];

return returnString;
return res["decorated_user_id"];
} catch (err) {
print("Caught error: $err");
return "Error occured check logs";
Expand Down Expand Up @@ -181,136 +187,141 @@ class _LoginScreenState extends State<LoginScreen> {
),
body: isLoading
? Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(
height: 20,
),
ElevatedButton(
onPressed: () {
cancelRegistration();
},
child: Text('Cancel'),
),
],
),
child: _buildCancelRegistrationWidget(),
)
: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 20,
),
FutureBuilder<List<OWUserProfile>>(
//userProfiles
future: getUserProfiles(),
builder: (context, snapshot) {
final userProfileData = snapshot.data;
return (userProfileData != null &&
userProfileData.length > 0)
? Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"──── Login ────",
style: TextStyle(fontSize: 30),
textAlign: TextAlign.center,
),
_buildImplicitUserData(
userProfileData.first.profileId),
ElevatedButton(
onPressed: () {
authenticate(
userProfileData.first.profileId,
null);
},
child: Text('Preferred authenticator'),
),
Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
authenticate(
userProfileData.first.profileId,
OWAuthenticatorType.pin);
},
child: Text('Pin'),
),
SizedBox(height: 10, width: 10),
ElevatedButton(
onPressed: () {
authenticate(
userProfileData.first.profileId,
OWAuthenticatorType.biometric);
},
child: Text('Biometrics'),
),
],
),
])
: SizedBox.shrink();
}),
SizedBox(
height: 20,
),
Text(
"──── Register ────",
style: TextStyle(fontSize: 30),
),
SizedBox(
height: 20,
),
ElevatedButton(
onPressed: () {
openWeb();
},
child: Text('Run WEB'),
),
SizedBox(
height: 20,
),
FutureBuilder<List<OWIdentityProvider>>(
future: Onegini.instance.userClient.getIdentityProviders(),
builder: (BuildContext context, snapshot) {
final identityProviders = snapshot.data;
return identityProviders != null
? PopupMenuButton<String>(
child: Container(
padding: EdgeInsets.all(20),
color: Colors.blue,
child: Text(
"Run with providers",
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w700),
),
),
onSelected: (value) {
registrationWithIdentityProvider(value);
},
itemBuilder: (context) {
return identityProviders
.map((e) => PopupMenuItem<String>(
child: Text(e.name),
value: e.id,
))
.toList();
})
: SizedBox.shrink();
},
),
SizedBox(height: 20),
_buildLoginWidget(),
SizedBox(height: 20),
_buildRegisterWidget(),
],
),
),
);
}

Widget _buildLoginWidget() {
final profileId = selectedProfileId;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really a need for this variable? 🤔 You can use selectedProfileId

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dart takes a similar approach to null safety as Kotlin does, so checking if a variable is null is not enough if it's mutable. Similar to why we use ?.let in Kotlin. In Dart however this check is enough when the variable is final.

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: [
Text(
"──── Register ────",
style: TextStyle(fontSize: 30),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
openWeb();
},
child: Text('Run WEB'),
),
SizedBox(height: 20),
FutureBuilder<List<OWIdentityProvider>>(
future: Onegini.instance.userClient.getIdentityProviders(),
builder: (BuildContext context, snapshot) {
final identityProviders = snapshot.data;
return identityProviders != null
? PopupMenuButton<String>(
child: Container(
padding: EdgeInsets.all(20),
color: Colors.blue,
child: Text(
"Run with providers",
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w700),
),
),
onSelected: (value) {
registrationWithIdentityProvider(value);
},
itemBuilder: (context) {
return identityProviders
.map((e) => PopupMenuItem<String>(
child: Text(e.name),
value: e.id,
))
.toList();
})
: SizedBox.shrink();
},
),
],
);
}

Column _buildCancelRegistrationWidget() {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
cancelRegistration();
},
child: Text('Cancel'),
),
],
);
}
}
21 changes: 12 additions & 9 deletions example/lib/screens/user_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -227,6 +226,11 @@ class _UserScreenState extends State<UserScreen> with RouteAware {
DrawerHeader(
child: Container(),
),
ListTile(
title: Text("ProfileId: ${profileId}"),
leading: Icon(Icons.person),
),
Divider(),
ListTile(
title: Text("Authenticators"),
leading: Icon(Icons.lock_rounded),
Expand Down Expand Up @@ -425,8 +429,7 @@ class _InfoState extends State<Info> {
ResourceRequestType.anonymous,
RequestDetails(
path: "application-details", method: HttpRequestMethod.get));
var res = json.decode(response.body);
return applicationDetailsFromJson(res);
return applicationDetailsFromJson(response.body);
}

Future<ClientResource> getClientResource() async {
Expand Down Expand Up @@ -465,11 +468,11 @@ class _InfoState extends State<Info> {
children: [
Text(
"application identifier => ",
style: TextStyle(fontSize: 18),
style: TextStyle(fontSize: 15),
),
Text(
snapshot.data?.applicationIdentifier ?? "",
style: TextStyle(fontSize: 18),
style: TextStyle(fontSize: 15),
)
],
),
Expand All @@ -480,11 +483,11 @@ class _InfoState extends State<Info> {
children: [
Text(
"application platform => ",
style: TextStyle(fontSize: 18),
style: TextStyle(fontSize: 15),
),
Text(
snapshot.data?.applicationPlatform ?? "",
style: TextStyle(fontSize: 18),
style: TextStyle(fontSize: 15),
)
],
),
Expand All @@ -495,11 +498,11 @@ class _InfoState extends State<Info> {
children: [
Text(
"application version => ",
style: TextStyle(fontSize: 18),
style: TextStyle(fontSize: 15),
),
Text(
snapshot.data?.applicationVersion ?? "",
style: TextStyle(fontSize: 18),
style: TextStyle(fontSize: 15),
)
],
),
Expand Down