piano_id 1.0.4
piano_id: ^1.0.4 copied to clipboard
Piano ID Flutter SDK.
Piano ID SDK for Flutter #
Piano ID Flutter SDK provides user authentication via Piano ID, including sign-in/sign-up UI, token management, and user info retrieval.
Installation #
Add the dependency to your pubspec.yaml:
dependencies:
piano_id: ^1.0.4
piano_id depends on piano_common, so you need a Piano instance first:
import 'package:piano_common/piano_common.dart';
import 'package:piano_id/piano_id.dart';
final piano = await Piano.init(
endpoint: PianoEndpoint.production,
aid: '<AID>',
);
Sign In #
Show the Piano ID authentication screen. The SDK presents a platform-appropriate modal (Cupertino on iOS, Material bottom sheet on Android) with an embedded WebView.
piano.id() returns a new PianoIDClient instance on each call:
final token = await piano.id().signIn(context: context);
if (token != null) {
print('Access token: ${token.accessToken}');
print('Refresh token: ${token.refreshToken}');
}
Sign Up #
Open the registration screen instead of login:
final token = await piano.id().signIn(
context: context,
signUp: true,
);
Disable Sign Up #
Hide the sign-up option on the login screen:
final token = await piano.id().signIn(
context: context,
signUpEnabled: false,
);
Custom Events #
Listen for custom events from the Piano ID form:
final token = await piano.id().signIn(
context: context,
onCustomEvent: (event) {
print('Custom event: $event');
},
);
Localization #
The sign-in screen is shown in the language configured on the Piano instance, which defaults to en_US:
final piano = await Piano.init(
endpoint: PianoEndpoint.production,
aid: '<AID>',
language: 'fr_FR',
);
final token = await piano.id().signIn(context: context);
Sign Out #
Invalidate a user's access token:
await piano.id().signOut(accessToken: token.accessToken);
Refresh Token #
Exchange a refresh token for a new token pair:
final newToken = await piano.id().refreshToken(
refreshToken: token.refreshToken,
);
print('New access token: ${newToken.accessToken}');
User Info #
Fetch the authenticated user's profile:
final user = await piano.id().userInfo(
accessToken: token.accessToken,
);
print('${user.firstName} ${user.lastName}');
print('Email: ${user.email}');
print('UID: ${user.uid}');
Optionally pass a form name:
final user = await piano.id().userInfo(
accessToken: token.accessToken,
formName: 'registration',
);
for (final field in user.customFields) {
print('${field.fieldName}: ${field.value}');
}
Form Info #
Check whether a user has filled all custom fields for a form:
final form = await piano.id().formInfo(
accessToken: token.accessToken,
formName: 'registration',
);
if (!form.hasAllCustomFieldValuesFilled) {
// Prompt the user to complete their profile
}
Error Handling #
All methods throw exception on failure:
try {
await piano.id().signOut(accessToken: token.accessToken);
} on PianoException catch (e) {
print('Piano error: ${e.message}');
} catch (e) {
print('Piano error: ${e}');
}