masamune_auth_apple 3.1.1
masamune_auth_apple: ^3.1.1 copied to clipboard
Authentication plugin for Masamune that can implement Apple sign-in, works only on IOS.
Masamune Auth Apple
[GitHub] | [YouTube] | [Packages] | [X] | [LinkedIn] | [mathru.net]
Masamune Auth Apple #
Usage #
masamune_auth_apple adds Apple Sign In support on top of the Masamune/Katana authentication stack. It is intended to be used together with:
katana_auth– core authentication abstractionskatana_auth_firebase– optional Firebase Authentication integrationmasamune_auth_apple_firebase– optional helper for exchanging Apple credentials with Firebase
Installation #
Install the base packages first:
flutter pub add katana_auth
flutter pub add katana_auth_firebase
Then add the Apple-specific packages:
flutter pub add masamune_auth_apple
flutter pub add masamune_auth_apple_firebase
Register Adapters #
Place the adapters near the root of your app so they are available to the authentication controller.
// lib/adapter.dart
/// Masamune adapters used in the application.
final masamuneAdapters = <MasamuneAdapter>[
const UniversalMasamuneAdapter(),
/// Apple Sign In adapters
const AppleAuthMasamuneAdapter(),
FirebaseAppleAuthMasamuneAdapter(
functionsAdapter: const FunctionsMasamuneAdapter(),
),
];
Note: This package requires katana_auth and katana_auth_firebase to be installed separately. Those packages provide the core authentication infrastructure (AuthAdapter, FirebaseAuthAdapter).
AppleAuthMasamuneAdapter prepares the native Apple sign-in flow on iOS. FirebaseAppleAuthMasamuneAdapter exchanges the Apple credential for a Firebase token (via Cloud Functions or the Firebase SDK).
Authenticate with Apple #
Use Authentication from katana_auth to manage sign-in state.
class SignInPage extends PageScopedWidget {
@override
Widget build(BuildContext context, PageRef ref) {
final auth = ref.app.controller(Authentication.query());
// Initialize on page load
ref.page.on(
initOrUpdate: () {
auth.initialize();
},
);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Show user info if signed in
if (auth.isSignedIn)
Column(
children: [
Text("Welcome!"),
Text("User ID: ${auth.userId}"),
Text("Email: ${auth.userEmail ?? 'N/A'}"),
],
)
else
ElevatedButton.icon(
icon: Icon(Icons.apple),
label: const Text("Sign in with Apple"),
onPressed: () async {
try {
await auth.signIn(AppleAuthQuery.signIn());
print("Signed in successfully!");
} catch (e) {
print("Sign in failed: $e");
}
},
),
// Sign out button
if (auth.isSignedIn)
TextButton(
onPressed: () async {
await auth.signOut();
},
child: const Text("Sign Out"),
),
],
),
),
);
}
}
State Management: The Authentication controller is a ChangeNotifier, so the UI automatically rebuilds when sign-in state changes.
Handling Tokens and Linking #
- When
katana_auth_firebaseis installed, Apple credentials are automatically linked to the Firebase user. - Link additional providers later with
auth.link(AppleAuthQuery.link())if your application allows multiple sign-in methods. - For anonymous user upgrade:
auth.link(AppleAuthQuery.link())preserves existing user data.
iOS Configuration #
Apple Sign In requires platform-specific configuration:
- Enable Sign In with Apple in the Apple Developer portal and create the necessary identifiers.
- Update
Info.plistwith theNSAppleMusicUsageDescriptionandCFBundleURLSchemesentries generated by Xcode. - Ensure your bundle ID matches the identifier configured in the Apple Developer console.
Tips #
- Test on real iOS devices; the simulator may not fully support Apple Sign In flows.
- Use
AuthLoggerAdapterto capture sign-in analytics or error reports. - Provide fallbacks or alternative sign-in methods for platforms where Apple Sign In is unavailable.
- Combine with
masamune_purchaseor other Masamune modules to unlock premium features once the user signs in.
GitHub Sponsors #
Sponsors are always welcome. Thank you for your support!