📲 Project Creation (Step-by-Step)
DatabaseKit & AuthKit Setup (Supabase)

DatabaseKit & AuthKit Setup (Supabase)

Steps are required for both DatabaseKit and AuthKit to work correctly.

Looking for other provider

This page refers to projects created with Supabase selected as the Foundation Provider.

Looking for Firebase docs? Click here

Set up a new Supabase Project

DatabaseKit is built on top of Supabase. Go to supabase.com/dashboard/projects (opens in a new tab) sign in to your account, and press on New Project

Supabase Dashboard Overview

Select your Supabase Organization, Enter your Project Name, create a Database Password (Save it somewhere!) and press on Create new Project.

Supabase New Project Screen

This will create a new Supabase project. Now, you need to wait a minute or two until supabase finishes setting up your project.

Supabase New Project Creation

Setting Up your Database

Creating a first table

After your supabase project has been created, it's time for us to prepare our database. We need to create all the necessary tables and columns.

In the Sidebar, go to Table Editor, and press on Create a new table.

Supabase Creating First Table

SwiftyLaunch generates a database usage example on the front, but we need to set up the columns first. Please follow the next steps to set up the columns correctly.

We will create a posts table with following columns:

  • id (Primary Key, int8), which will be automatically generated by Supabase.
  • title (Text), which will store the title of the post.
  • content (Text), which will store the content of the post.
  • creationDate (Timestamp), which will store the creation date of the post.
  • postUserID (uuid), which will store the user's ID who created the post. (will be automatically inferred by the currently authenticated user. More about that in AuthKit)

In the slide-out panel, enter posts in the Name field and turn ON RLS (Row Level Security).

Supabase Creating First Table Step 1

Then, enter all of the columns mentioned above. Make sure that the names and types are correct and that title, content and postUserID are not nullable. (To prevent empty values).

Additionally, set the default value of creationDate to be now() (current time when a new row is added), and postUserID to be auth.uid() (the currently authenticated user's ID).

Press on Save.

Supabase Creating First Table Step 2 Supabase Creating First Table Not Nullable

Adding RLS Policies

To protect our table from unauthorized access, we need to set up RLS Policies. This allows us to adjust granularly who can read/write to our table.

We will add three RLS policies:

  • Enabling read access to all users.
  • Enabling write access to authenticated users.
  • Enabling delete access to the user who created the post.

Enabling Read Access to All Users

In the sidebar, go to authentication > Policies. Make sure that we are in the posts table, and press on Create Policy.

Creating First RLS Policy

  1. Write a suitable name for your policy
  2. Set the table to be public.posts
  3. Set Policy Behavior to Permissive
  4. Set SELECT as the command
  5. Leave Target Roles empty, meaning to apply this policy to all users.
  6. Set check to true (meaning that all users can read from this table)
  7. Press Save policy

To cover it: this policy for table public.posts (2), allows (3) to read (4) for all users (5 & 6).

Entering Data for First Policy

Enabling Write Access to Authenticated Users

After creating the first policy, press again on Create Policy.

  1. Write a suitable name for your policy
  2. Set the table to be public.posts
  3. Set Policy Behavior to Permissive
  4. Set INSERT as the command
  5. Set Target Roles to authenticated (allowing only authenticated users to INSERT (write) to this table)
  6. Set check to true (meaning that all authenticated users can do so)
  7. Press Save policy

To cover it: this policy for table public.posts (2), allows (3) to write (4) for all (6) authenticated users (5).

Entering Data for Second Policy

Enabling Delete Access to Authenticated Post Authors

This is the last policy we'll create. Again, press on Create Policy.

  1. Write a suitable name for your policy
  2. Set the table to be public.posts
  3. Set Policy Behavior to Permissive
  4. Set DELETE as the command
  5. Set Target Roles to authenticated (allowing only authenticated users to DELETE from this table)
  6. Set check to (select auth.uid()) = "postUserID" (meaning that the currently authenticated user id must match the value in the column postUserID)
  7. Press Save policy

To cover it: this policy for table public.posts (2), allows (3) to delete (4) for authenticated users (5), whose user ID matches the post's postUserID column (6).

Entering Data for Third Policy

Setting up Sign in with Apple

Currently, AuthKit with Supabase only supports Sign in with Apple as the authentication method, because in comparison to Firebase, if we wanted to add email sign-in, we would have to set up our own mailing server, yada, yada. We want you to launch your app as soon as possible, and until there is a simple way to do email sign-in with Supabase as convenient as it is with Firebase, we wont include it in AuthKit via Supabase.

In this section, we will go over adding Sign in with Apple in your app.

Create a Services ID

Go to the Identifiers (opens in a new tab) of your Apple Developer Account and press on the + Button

Apple Developer Identifiers Section

Select Services IDs and press Continue.

New Identifier Type Selection Section

Enter a Services ID Name, enter an Identifier (BUNDLE_ID.ServicesID where BUNDLE_ID is the bundle id you've entered during Project Initialization and press Continue.

Then, press Register.

New Identifier Name Enter Section

Back in the Identifiers section, press on the newly created identifier.

Apple Developer Identifiers Section

Enable Sign in with Apple and press on Configure.

Created Identifier Page

In this Configuration Pop-up:

  1. Select your App in the Primary App ID dropdown.
  2. Enter supabase.co,SUPABASE_PROJECT_ID.supabase.co in the Domains and Subdomains URL field.
  3. Enter https://SUPABASE_PROJECT_ID.supabase.co/auth/v1/callback in the Return URLs field.
  4. Press Next and Done.

Replace SUPABASE_PROJECT_ID with your Supabase Project ID. You can find it by opening to your Supabase Project Dashboard and looking at the url. It will be structured in a following manner: supabase.com/dashboard/SUPABASE_PROJECT_ID

Return URL Configuration

Press Continue and Save.

Return URL Configuration

Create a .p8 Auth Key for Sign in with Apple

Go to the Keys Section (opens in a new tab) of your Apple Developer Account and press on the + Button

Apple Developer Keys Section

If you have created added a Key to use with NotifKit beforehand, you don't have to create a new one, just press Edit on the one you've already created.

Enter a Key Name, select Sign in with Apple and press Configure.

Apple Developer Key Creation Screen

Select your App ID and press Save.

Apple Developer Key Primary App ID Selection

Press on Continue, then Register.

Don't delete that key. If you are going to enable Push Notifications later, we will use the same key. You can't redownload it, so you'll have to generate a new one.

Apple Developer Key Creation Screen Apple Developer Register Key Screen

Next:

  1. Download the .p8 Key
  2. Write down your Developer Team ID
  3. Write down your Key ID

Apple Developer Download Key

Generate Secret JWT Key

First,

Download this ruby script

to generate a new secret key for supabase project. Then, install the JWT ruby gem (opens in a new tab) that we will need to execute the generation script. You can install it by running sudo gem install jwt in your terminal.

After installing it, open the generation script. In the script, replace placeholder values with

  1. The path to the downloaded service auth key.
  2. Your Developer Team ID
  3. Your ServicesID
  4. Your service auth key ID.

JWT Generation Script Example

After doing that, execute the script by running ruby generateJWToken.rb. Write down that generated token.

JWT Generation Running Script Example

Connect Sign in with Apple to Supabase (AuthKit)

In your Supabase Dashboard, go to Authentication > Providers.

  1. Here, press on Apple. Enable Sign in with Apple. Then,
  2. enter your client ids: that is the ServicesID and your app bundle id (comma seperated with no space),
  3. enter the secret key that we have generated in the previous step
  4. Press on save.

You can also disable sign in with email, if you don't intend to add it yourself.

JWT Generation Running Script Example

Don't delete the service key. If you are going to enable Push Notifications later, we will use the same key. You can't redownload it, so you'll have to generate a new one.

Connect Supabase with your App

Go to Settings > API. Copy over the Project URL, and the public project API Key.

Getting Supabase URL and Key

In Xcode, go to the Supabase-Info.plist file, paste the project URL into the SUPABASE_URL field, and the public api key into the SUPABASE_KEY field.

Inserting Supabase URL and Key into xcode

Test if everything is set up correctly

It's recommended to wait until you have set up the rest of your app before doing any tests, but if you can't wait, you can temporarily disable other Modules.

Run your app on a real device or a simulator. You should be on the first tab, named DB Example. Here, press on the New Post Button in the top right corner. A Sign-In Sheet should appear. Press Continue with Apple.

App DBKit Test 1

After a successful log-in, enter a Post Title and press the Publish Button. Now you can see your post in a "Timeline"

App DBKit Test 2

If you go to your Supabase, you should see a new user in the Authentication > Users tab and a new post in the Table Editor > posts tab.

App DBKit Test 3

App DBKit Test 4