Thanks for coming back to me. I’ve made some modifications to my code, and initially thought I had made some progress by changing some of the args being passed to the auth0 SDK:
auth0
.onLoad(
audience: auth0Audience,
useRefreshTokens: true,
cacheLocation: CacheLocation.localStorage)
.then((final credentials) => () {
auth0AuthenticatedUser = credentials?.user;
});
I believe that adding ‘useRefreshTokens’ and ‘cacheLocation’ has helped, because before they were put in place, my login scenario would not work at all. With these in place, if I start a debug session with my app already logged in (presumably with a cookie or equivalent providing persistence) then it works as expected.
However in any other scenario, for example, if I start a debug session logged out, and try to login with a redirect, then I am consistently hitting this same exception:
Paused on promise rejection - Error: missing_transaction: Invalid state
As previously mentioned, when this occurs, the error is visible in Chrome Dev, but never ‘bubbles up’ to VS code, which just behaves as if a breakpoint has been hit.
I’m a bit lost for ideas at this stage - cannot understand for the life of me why this shouldn’t work.
thanks
Robert
My latest code for my login page:
import 'package:flutter/material.dart';
import 'package:auth0_flutter/auth0_flutter_web.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:go_router/go_router.dart';
class LoginPage3 extends StatefulWidget {
final Auth0Web auth0;
const LoginPage3({super.key, required this.auth0});
@override
State<LoginPage3> createState() => LoginPage3State();
}
class LoginPage3State extends State<LoginPage3> {
bool? isLoggedInWidgetVar;
String redirectUrl = dotenv.env['AUTH0_LOGIN_REDIRECT_URL'].toString();
String appBarName = dotenv.env['APP_BAR_NAME'].toString();
late Future<bool> loggedInFuture;
@override
initState() {
super.initState();
WidgetsBinding.instance
.addPostFrameCallback((_) => ifLoggedInThenRedirectCallback());
loggedInFuture = loginHasValidCredentials();
}
Future<bool> loginHasValidCredentials() async {
final loggedIn = await widget.auth0.hasValidCredentials();
// if (loggedIn) {
// await widget.auth0.credentials();
// }
isLoggedInWidgetVar = loggedIn;
return loggedIn;
}
void ifLoggedInThenRedirectCallback() {
bool isLoggedIn = (isLoggedInWidgetVar ??= false);
if (isLoggedIn) {
context.go('/coreMenu');
}
}
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: loggedInFuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.requireData) // logged in
{
return const SizedBox.shrink();
} else // not logged in
{
return Scaffold(
appBar: AppBar(
title: Text(appBarName),
backgroundColor: Colors.purple,
foregroundColor: Colors.white,
),
body: Column(children: [
// Logo
const Icon(Icons.quiz),
// Login button
ElevatedButton(
onPressed: () => {
widget.auth0
.loginWithRedirect(redirectUrl: redirectUrl)
},
child: const Text('LOGIN')),
// Sign up button
TextButton(
onPressed: () => throw Exception(), //todo
child: const Text('SIGN UP / CREATE ACCOUNT'))
]));
}
} else // awaiting async login status
{
return const CircularProgressIndicator();
}
});
}
}