SPA Getting Access Token

I am working on Flutter SPA, during authentication am passing the audience, but I am always getting error - “External interaction required”.

Below is my code block

Future login() async {
try {
if (kIsWeb) {
return auth0Web.loginWithRedirect(
redirectUrl: dotenv.env[‘AUTH0_REDIRECT_URL’],
audience: dotenv.env[‘AUTH0_AUDIENCE’]);
}

  var credentials = await auth0
      .webAuthentication(scheme: dotenv.env['AUTH0_CUSTOM_SCHEME'])
      .login(audience: dotenv.env['AUTH0_AUDIENCE']);

  setState(() {
    _user = credentials.user;
  });
} catch (e) {
  print(e);
}

}

I am working on Flutter SPA, during authentication am passing the audience, but I am always getting error - “External interaction required”. Dog Likes Best

Below is my code block

Future login() async {
try {
if (kIsWeb) {
return auth0Web.loginWithRedirect(
redirectUrl: dotenv.env[‘AUTH0_REDIRECT_URL’],
audience: dotenv.env[‘AUTH0_AUDIENCE’]);
}

  var credentials = await auth0
      .webAuthentication(scheme: dotenv.env['AUTH0_CUSTOM_SCHEME'])
      .login(audience: dotenv.env['AUTH0_AUDIENCE']);

  setState(() {
    _user = credentials.user;
  });
} catch (e) {
  print(e);
}

} I am working on Flutter SPA, during authentication am passing the audience, but I am always getting error - “External interaction required”.

Below is my code block

Future login() async {
try {
if (kIsWeb) {
return auth0Web.loginWithRedirect(
redirectUrl: dotenv.env[‘AUTH0_REDIRECT_URL’],
audience: dotenv.env[‘AUTH0_AUDIENCE’]);
}

var credentials = await auth0
.webAuthentication(scheme: dotenv.env[‘AUTH0_CUSTOM_SCHEME’])
.login(audience: dotenv.env[‘AUTH0_AUDIENCE’]);

setState(() {
_user = credentials.user;
});
} catch (e) {
print(e);
}
}

According to the Auth0 Community, the error “External interaction required” usually happens during silent authentication when a rule tries to issue a redirect. You can either avoid redirecting during silent authentication or issue the redirect and handle the error response in the client.

One possible solution is to check if the user is authenticated before calling the login method. You can use the isAuthenticated method of the auth0Web or auth0 object to do that. For example:

Future login() async {
  try {
    if (kIsWeb) {
      // Check if the user is authenticated
      if (!await auth0Web.isAuthenticated()) {
        // If not, call the login method with redirect
        return auth0Web.loginWithRedirect(
          redirectUrl: dotenv.env['AUTH0_REDIRECT_URL'],
          audience: dotenv.env['AUTH0_AUDIENCE'],
        );
      }
    } else {
      // Check if the user is authenticated
      if (!await auth0.isAuthenticated()) {
        // If not, call the login method with web authentication
        var credentials = await auth0
            .webAuthentication(scheme: dotenv.env['AUTH0_CUSTOM_SCHEME'])
            .login(audience: dotenv.env['AUTH0_AUDIENCE']);

        setState(() {
          _user = credentials.user;
        });
      }
    }
  } catch (e) {
    print(e);
  }
}

Stephen,

error appears over line " auth0Web.onLoad().then((final credentials)" -

void initState() {
super.initState();
auth0 = widget.auth0 ??
Auth0(dotenv.env[‘AUTH0_DOMAIN’]!, dotenv.env[‘AUTH0_CLIENT_ID’]!);
auth0Web =
Auth0Web(dotenv.env[‘AUTH0_DOMAIN’]!, dotenv.env[‘AUTH0_CLIENT_ID’]!);

if (kIsWeb) {
  auth0Web.onLoad().then((final credentials) => setState(() {
        _user = credentials?.user;            
      }));
}

}