Flutter (Web)

Gravatar for steve.hobbs@auth0.com
By Steve Hobbs

This tutorial demonstrates how to add user login with Auth0 to an Flutter Web application using the Auth0 Flutter SDK We recommend that you log in to follow this quickstart with examples configured for your account.

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: Flutter 3+ | Xcode 14+ (for iOS) | Android Studio 4+ (for Android)

Add login to your Flutter app

Auth0 allows you to quickly add authentication and access user profile information in your application. This guide demonstrates how to integrate Auth0 with a Flutter Web application using the Auth0 Flutter SDK.

Getting started

This quickstart assumes you already have a Flutter application up and running. If not, check out the Flutter "getting started" guides to get started with a simple app.

You should also be familiar with the Flutter command line tool.

Configure Auth0

Get Your Application Keys

When you signed up for Auth0, a new application was created for you, or you could have created a new one. You will need some details about that application to communicate with Auth0. You can get these details from the Application Settings section in the Auth0 dashboard.

App Dashboard

You need the following information:

  • Domain
  • Client ID

Configure Callback URLs

A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated. The callback URL for your app must be added to the Allowed Callback URLs field in your Application Settings. If this field is not set, users will be unable to log in to the application and will get an error.

Configure Logout URLs

A logout URL is a URL in your application that Auth0 can return to after the user has been logged out of the authorization server. This is specified in the returnTo query parameter. The logout URL for your app must be added to the Allowed Logout URLs field in your Application Settings. If this field is not set, users will be unable to log out from the application and will get an error.

Configure Allowed Web Origins

You need to add the URL for your app to the Allowed Web Origins field in your Application Settings. If you don't register your application URL here, the application will be unable to silently refresh the authentication tokens and your users will be logged out the next time they visit the application, or refresh the page.

Install the Auth0 Flutter SDK

Add the Auth0 Flutter SDK into the project:

flutter pub add auth0_flutter

Was this helpful?

/

Add the following script tag to your index.html page:

<script src="https://cdn.auth0.com/js/auth0-spa-js/2.0/auth0-spa-js.production.js" defer></script>

Was this helpful?

/

Add login to your app

Universal Login is the easiest way to set up authentication in your application. We recommend using it for the best experience, best security, and the fullest array of features.

Integrate Auth0 Universal Login in your Flutter app by importing the SDK and instantiating the Auth0 class using your Auth0 domain and Client ID values. See this example, which instantiates the class inside a widget state object:

import 'package:auth0_flutter/auth0_flutter.dart';
import 'package:auth0_flutter/auth0_flutter_web.dart';

class MainView extends StatefulWidget {
  const MainView({Key? key}) : super(key: key);

  @override
  State<MainView> createState() => _MainViewState();
}

class _MainViewState extends State<MainView> {
  Credentials? _credentials;

  late Auth0Web auth0;

  @override
  void initState() {
    super.initState();
    auth0 = Auth0Web('{yourDomain}', '{yourClientId}');
  }

  @override
  Widget build(BuildContext context) {
    // ...
  }
}

Was this helpful?

/

Next, redirect your users to the Auth0 Universal Login page using loginWithRedirect. See this example of a ElevatedButton widget that logs the user in when clicked:

if (_credentials == null) {
  ElevatedButton(
    onPressed: () => auth0.loginWithRedirect(redirectUrl: 'http://localhost:3000'),
    child: const Text("Log in")
  )
}

Was this helpful?

/

When a user logs in, they are redirected back to your application. You are then able to access the ID and access tokens for this user by calling onLoad during startup and handling the credentials that are given to you:

auth0.onLoad().then((final credentials) => setState(() {
    // Handle or store credentials here
    _credentials = credentials;
  }));

Was this helpful?

/
Checkpoint

Add a button to your app that calls loginWithRedirect() and logs the user into your app. Verify that you are redirected to Auth0 for authentication and then back to your application.

Verify that you can access credentials as a result of calling onLoad and that you're able to access the ID and access tokens.

Add logout to your app

To log users out, redirect them to the Auth0 logout endpoint to clear their login session by calling the Auth0 Flutter SDK logout(). Read more about logging out of Auth0.

See this example of an ElevatedButton widget that logs the user out of the app:

ElevatedButton(
  onPressed: () async {
    await auth0.logout();
  },
  child: const Text("Log out"))

Was this helpful?

/
Checkpoint

Add a button to your app that calls logout() and logs the user out of your application. When you select it, verify that your Flutter app redirects you to the logout endpoint and back again. You should not be logged in to your application.

Show user profile information

The user profile automatically retrieves user profile properties for you when the page loads, and can be accessed and stored by calling onLoad during application startup. The returned object from onLoad contains a user property with all the user profile properties. This is internally populated by decoding the ID token.

See this example of a View component that displays the user profile on the screen:

import 'package:auth0_flutter/auth0_flutter.dart';
import 'package:flutter/material.dart';

class ProfileView extends StatelessWidget {
  const ProfileView({Key? key, required this.user}) : super(key: key);

  final UserProfile user;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        if (user.name != null) Text(user.name!),
        if (user.email != null) Text(user.email!)
      ],
    );
  }
}

Was this helpful?

/
Checkpoint

Log in and inspect the user property on the result. Verify the current user's profile information, such as email or name.

Did it work?

Any suggestion or typo?

Edit on GitHub