auth0_flutter2 0.0.16 auth0_flutter2: ^0.0.16 copied to clipboard
Cross-platform Auth0 authentication integration, in minutes, for mobile and web.
import 'dart:io';
import 'package:auth0_flutter2/auth0_flutter2.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
Auth0Flutter2.auth0Domain = "AUTH0_DOMAIN";
Auth0Flutter2.auth0ClientId = "AUTH0_CLIENT_ID";
Auth0Flutter2.redirectUri = "http://localhost:8080";
if (Platform.isAndroid) {
Auth0Flutter2.scheme = "demo";
}
// Set the URL strategy for our web app. Removes trailing hash(#)
// to ensure login callbacks will be captured and processed correctly.
Auth0Flutter2.setPathUrlStrategy();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Auth0Flutter2 Demo',
theme: ThemeData(
primarySwatch: Colors.deepPurple,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _userId = "";
@override
void initState() {
checkForRedirectCallback();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
Auth0Flutter2.instance.loginUser();
},
child: const Text(
"Login User",
),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {
Auth0Flutter2.instance.logoutUser();
},
child: const Text(
"Logout User",
),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () async {
var userId = await Auth0Flutter2.instance.getLoggedInUserId();
setState(() {
_userId = userId ?? "User Not Logged In";
});
},
child: const Text(
"Fetch Logged In UserId",
),
),
const SizedBox(height: 20),
Text(
"Authenticated User ID: $_userId",
textAlign: TextAlign.center,
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
],
),
),
);
}
Future<void> checkForRedirectCallback() async {
// Process the login state
var redirectCallbackHandled =
await Auth0Flutter2.instance.handleWebLoginCallback(
Uri.base.toString(),
);
if (redirectCallbackHandled) {
var userId = await Auth0Flutter2.instance.getLoggedInUserId();
setState(() {
_userId = userId ?? "User Not Logged In";
});
}
}
}