Secure User Authentication in Flutter Mobile Apps: A Comprehensive Guide to Appwrite Auth Integration

Debasish Das
4 min readMar 22, 2023

--

This is a continuation of the series of blogs on Appwrite Service Integrations and implementations. You can find the last blogs in the below link.

If you’re a Flutter developer looking for a secure and reliable backend solution for your mobile app, look no further than Appwrite. Appwrite is an open source backend server that offers a range of features, including authentication, database, storage, and more. In this tutorial, we’ll focus on how to integrate Appwrite Auth into a Flutter mobile app.

Below is the link to the Appwrite GitHub Repository.

Prerequisites

Before we begin, make sure you have the following:

  • A basic understanding of Flutter development
  • Flutter SDK installed on your machine
  • Android Studio or Visual Studio Code installed on your machine
  • An Appwrite server running on your local machine or cloud instance.
  • Appwrite Flutter SDK added to your project

Setting Up Appwrite Auth

The first step is to set up Appwrite Auth in your Appwrite dashboard. Follow these steps to create a new Appwrite project and enable authentication:

  1. Log in to your Appwrite dashboard and create a new project.
  2. Click on the “Authentication” tab in the left-hand menu.
  3. Click on the “Add a new method” button.
  4. Choose your desired authentication method (e.g. Email/Password, Google, Apple, etc.).
  5. Follow the on-screen instructions to set up the authentication method.
  6. Save your changes and note down the project ID and API key.

Adding Appwrite Auth to Your Flutter Project

With Appwrite Auth set up, it’s time to integrate it into your Flutter project. Here’s how:

Step#1: Open your Flutter project in Android Studio or Visual Studio Code.

Step#2: Add the Appwrite Flutter SDK to your project by adding the following dependency to your pubspec.yaml file:

dependencies:
appwrite: ^0.6.0

Run flutter pub get to install the dependencies.

Please Note: This version appwrite: ^0.6.0 was latest when I wrote this article, please check for an updated version when you are following it.

Step#3: Import the Appwrite SDK in your Dart code:

import 'package:appwrite/appwrite.dart';

Step#4: Set up the Appwrite client in your code, using the project ID and API key from the Appwrite dashboard:

final client = Client()
..setEndpoint('http://localhost/v1') // Replace with your Appwrite endpoint
..setProject('PROJECT_ID') // Replace with your Appwrite project ID
..setKey('API_KEY'); // Replace with your Appwrite API key

Step#5: Create a new instance of the Appwrite Auth service:

final auth = Account(client);

Step#6: Implement the authentication method in your Flutter app. For example, here’s how to implement email/password authentication:

final email = 'john@doe.com';
final password = 'john!doe';

try {
final result = await auth.createSession(email: email, password: password);
// Store the session ID in a local variable
final sessionId = response.session;
print('Logged in successfully with session ID: $sessionId');
// Authentication successful
} on AppwriteException catch (e) {
// Authentication failed
print('Failed to login: ${e.message}');
}

Step#7: You can also implement other authentication methods, such as Google, Apple, and others. Refer to the Appwrite Flutter SDK documentation for more information. Below is an example.

try {
final response = await client.account.createOAuth2Session(
provider: 'google', // Replace with the OAuth2 provider
success: 'REDIRECT_URL', // Replace with the redirect URL
failure: 'REDIRECT_URL', // Replace with the redirect URL
);
// Store the session ID in a local variable
final sessionId = response.session;
print('Logged in successfully with session ID: $sessionId');
} on AppwriteException catch (e) {
// Handle login errors
print('Failed to login: ${e.message}');
}

Step#8: Call the getAccount method to retrieve the account details of the authenticated user:

try {
final response = await client.account.get();
// Store the user's account data in a local variable
final account = response.data;
print('User account details: $account');
} on AppwriteException catch (e) {
// Handle errors
print('Failed to get account details: ${e.message}');
}

Logging out the user from your app

try {
await client.account.deleteSession(sessionId: 'SESSION_ID'); // Replace with the user's session ID
print('Logged out successfully');
} on AppwriteException catch (e) {
// Handle errors
print('Failed to logout: ${e.message}');
}

Conclusion

Integrating Appwrite Auth into a Flutter mobile app is a straightforward process that can provide a secure and reliable authentication solution for your app. By following the steps outlined in this tutorial, you should be able to set up Appwrite Auth in your Appwrite dashboard and add it to your Flutter project with ease.

I will be coming up with more articles on the other services integration of Appwrite. Stay Tuned.

To get started with Appwrite, visit the Appwrite website and create a free account. Happy coding!

--

--

Debasish Das

Mobile App Developer. Passionate about GoLang, System Design, Problem Solving, Distributed Systems. Loves to build products that scales to million.