Quick Start
Minimal setup to get the auth screen running in your app.
1. Initialize the SDK
Call SsoSdk.initialize() before runApp():
import 'package:authcentral_sdk/authcentral_sdk.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SsoSdk.initialize(SsoConfig(
baseUrl: 'https://api.authcentral-1.hero-dev.xyz',
origin: SsoOrigin.heroDev,
appName: 'MyApp',
));
runApp(const MyApp());
}
2. Get an access token
Before opening the auth screen, obtain an access token using your app's private token:
const appToken = String.fromEnvironment('APP_TOKEN');
final authToken = await SsoSdk.instance.getAccessToken(appToken);
3. Open the auth screen
Push the auth screen into your navigation stack:
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => SsoSdk.authScreen(
enabledProviders: [
SsoProvider.emailPassword,
SsoProvider.google,
SsoProvider.apple,
],
authToken: authToken,
onSuccess: (tokens) {
Navigator.of(context).pop();
// User is authenticated, tokens.authToken / tokens.refreshToken available
},
onCancel: () => Navigator.of(context).pop(),
),
),
);
Alternatively, show it as a bottom sheet:
await SsoSdk.showAuthBottomSheet(
context: context,
enabledProviders: [
SsoProvider.emailPassword,
SsoProvider.google,
SsoProvider.apple,
],
authToken: authToken,
onSuccess: (tokens) {
// Auto-dismissed on success
},
onCancel: () {
// User swiped down or cancelled
},
);
See Auth Screen for all parameters and customization options.
4. Observe auth state
After authentication, listen to token changes reactively:
StreamBuilder<SsoTokens?>(
stream: SsoSdk.instance.tokenStore.tokensStream,
initialData: SsoSdk.instance.tokenStore.currentTokens,
builder: (context, snapshot) {
if (snapshot.data != null) {
return HomePage();
}
return LoginPage();
},
)
5. Logout
await SsoSdk.instance.authController.logout();