Chrysolites Tech Academy

Photo Social App with FlutterFlow and Supabase · Course book

Chapter 2: Email Sign-In

Pairs with Lesson 2: Email Authentication with FlutterFlow and Supabase

Every social app begins with accounts. Before anyone can post a photo or follow a friend, the app must let them sign up, prove who they are and sign in again later, and it must send the emails that confirm addresses and reset passwords. This chapter builds email authentication for the photo app with FlutterFlow and Supabase: creating the FlutterFlow project, linking it to Supabase Authentication, designing the login and sign-up pages, creating a matching user record when someone signs up, testing the flow, and setting up a custom email server so that confirmation emails are delivered reliably. It also works out by hand why short numeric codes and link expiry keep accounts safe.

Creating the project and linking Supabase

A new FlutterFlow project starts from a blank app or a template. A blank app is the better choice for learning, because every page and setting is then something you created and understand. The project's settings hold the app's name, theme colours and fonts, which are worth setting early so that every page you design uses them automatically.

Linking Supabase takes two values from the Supabase project's settings: the project URL and the public anon key. Pasting them into FlutterFlow's Supabase integration settings connects the two, and switching the project's authentication type to Supabase makes FlutterFlow's sign-up, login and logout actions use Supabase accounts. The anon key is designed to be included in apps: on its own it grants only what the database's row level security policies allow. The separate service role key, which bypasses every policy, must never be placed in an app.

FlutterFlow also needs to know which pages to show at the start. An entry page is shown to signed-out users, usually the login page, and a logged-in page is shown to users with a saved session, usually the home feed. FlutterFlow handles the redirect between them automatically when authentication is enabled.

Once a user has signed in, the Supabase client stores a session on the device: a short-lived access token sent with every request, and a refresh token used to obtain new access tokens before they expire. That is why users stay signed in when they close and reopen the app. FlutterFlow exposes the authenticated user's id and email as values that any page or action can use, which later chapters rely on constantly, for example to set the author of a new post or to check whether a profile belongs to the person viewing it.

Email and password is not the only option. Supabase also supports sign-in with providers such as Google and Apple, and passwordless sign-in with a link or code sent by email, all of which FlutterFlow can use. Many apps offer two or three choices. Starting with email and password keeps the series simple, and the same users table and policies work unchanged if other providers are added later; on iOS, an app that offers some third-party sign-in options is generally expected to offer Sign in with Apple as well.

Login and sign-up pages

The login page needs a logo or app name, an email field, a password field, a login button and a link to sign up; the sign-up page adds a confirmation password field. In FlutterFlow, each field is a text field widget with its keyboard type set to email or its "password" option enabled to hide the characters, and the button's action is the matching authentication action: Create Account on the sign-up page and Log In on the login page, each given the values of the fields.

Validation catches mistakes before a request is sent. FlutterFlow forms can require fields, check that an email looks valid and set a minimum password length, showing an error message under the field when a rule fails. The sign-up page should also check that the two password fields match. When the server rejects a request, for example because the email is already registered or the password is wrong, a friendly message, shown with a snack bar, is far better than nothing happening.

PageWidgetsMain actionOn success
LoginEmail and password fields, login button, sign-up linkLog In with email and passwordGo to the home feed
Sign upEmail, password and confirm fields, create buttonCreate Account, then create the user recordGo to onboarding or the home feed
Any signed-in pageA log-out button in settingsLog OutReturn to the login page

Creating the user record

Supabase stores accounts in its own authentication schema, which holds the email, the hashed password and the account id, but not the app's own profile information such as a username, display name or profile photo. Those belong in a users table in the public schema, linked to the account by sharing its id. Creating that row as soon as an account is created ensures every account has a profile.

In FlutterFlow this is a second action after Create Account: an Insert Row action on the users table, setting the id to the newly authenticated user's id and the email to the value entered. A row level security policy should allow a user to insert and update only the row whose id equals auth.uid(), their own. A more robust alternative, independent of the app, is a database trigger on the authentication table that inserts the profile row automatically whenever an account is created; either way, the result is one profile per account.

Because the profile row and the account share the same id, any page can find the current user's profile by querying the users table for the authenticated user's id, and every post, like and comment created later can refer to the user by that id.

Confirmation emails and a custom SMTP server

By default, Supabase asks new users to confirm their email by clicking a link before they can sign in. This proves the address belongs to them, which matters for password resets and for stopping people signing up with someone else's email. The first test of the sign-up flow usually reveals two things: the email arrives, and clicking its link opens a default page rather than the app, which later chapters improve with deep links.

Supabase's built-in email service is meant for development: it sends only a small number of emails per hour and from a shared sender, so messages may land in spam. For a real app, Supabase lets you configure a custom SMTP server, the standard protocol for sending email, from an email delivery provider. The settings are the provider's host name, a port, a username and password or API key, and the sender name and address. Port 587 usually uses STARTTLS, upgrading a plain connection to an encrypted one, while port 465 uses an encrypted connection from the start; the provider's documentation says which to use.

Deliverability depends on more than the server. Sending from your own domain, with the domain's DNS records set up as the provider instructs, including SPF, DKIM and DMARC records that prove the provider is allowed to send on your behalf, makes it far more likely that confirmation emails reach inboxes. The email templates in Supabase can then be edited to carry the app's name and a clear call to action.

Codes, links and expiry

Confirmation links and one-time codes are secrets with a short life. A link contains a long random token, practically impossible to guess. Short numeric codes, sometimes used instead for convenience, are much easier to guess, which is why services limit attempts and set expiry times.

Worked example. A 6-digit code has 106 = 1,000,000 possible values. If an attacker may try 5 codes before the code is invalidated, the chance of success is 5 / 1,000,000 = 0.0005 percent. A 4-digit code, with 10,000 values, would give 5 / 10,000 = 0.05 percent, a hundred times higher. Expiry limits the window further: a code valid for 10 minutes, 600 seconds, cannot be used tomorrow, and a confirmation link valid for 24 hours expires after 86,400 seconds.
4 digits 6 digits 8 digits 10−210−410−610−8 13510 attempts allowedchance of guessing (log scale)
Figure 2.1 — Each extra two digits make a code a hundred times harder to guess; limiting attempts keeps even short codes safe.

The final test runs the whole flow on a fresh email address: sign up, receive the confirmation email from the custom sender, confirm, log in, see the home page, log out and return to the login page. Testing the error cases too, a wrong password, an existing email and an unconfirmed account, ensures every message makes sense.

Practice tasks

  1. What is the chance of guessing a 6-digit code with 3 attempts? With an 8-digit code and 10 attempts?
  2. A reset link expires after 2 hours. How many seconds is that?
  3. Explain why the anon key may be placed in the app but the service role key must not.
  4. Describe two ways to make sure every new account gets a row in the users table.
  5. Why does a custom SMTP server, sending from your own domain, improve email delivery?
  6. List three error cases to test on the login and sign-up pages, and the message each should show.

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.