DatabaseKit & AuthKit Setup (Supabase)
Steps are required for both DatabaseKit and AuthKit to work correctly.
This page refers to projects created with Supabase selected as the Foundation Provider.
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
Select your Supabase Organization, Enter your Project Name, create a Database Password (Save it somewhere!) and press on Create new Project.
This will create a new Supabase project. Now, you need to wait a minute or two until supabase finishes setting up your project.
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.
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).
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.
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.
- Write a suitable name for your policy
- Set the table to be public.posts
- Set Policy Behavior to Permissive
- Set SELECT as the command
- Leave Target Roles empty, meaning to apply this policy to all users.
- Set check to true (meaning that all users can read from this table)
- Press Save policy
To cover it: this policy for table public.posts (2), allows (3) to read (4) for all users (5 & 6).
Enabling Write Access to Authenticated Users
After creating the first policy, press again on Create Policy.
- Write a suitable name for your policy
- Set the table to be public.posts
- Set Policy Behavior to Permissive
- Set INSERT as the command
- Set Target Roles to authenticated (allowing only authenticated users to INSERT (write) to this table)
- Set check to true (meaning that all authenticated users can do so)
- Press Save policy
To cover it: this policy for table public.posts (2), allows (3) to write (4) for all (6) authenticated users (5).
Enabling Delete Access to Authenticated Post Authors
This is the last policy we'll create. Again, press on Create Policy.
- Write a suitable name for your policy
- Set the table to be public.posts
- Set Policy Behavior to Permissive
- Set DELETE as the command
- Set Target Roles to authenticated (allowing only authenticated users to DELETE from this table)
- Set check to (select auth.uid()) = "postUserID" (meaning that the currently authenticated user id must match the value in the column postUserID)
- 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).
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
Select Services IDs and press Continue.
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.
Back in the Identifiers section, press on the newly created identifier.
Enable Sign in with Apple and press on Configure.
In this Configuration Pop-up:
- Select your App in the Primary App ID dropdown.
- Enter
supabase.co,SUPABASE_PROJECT_ID.supabase.co
in the Domains and Subdomains URL field. - Enter
https://SUPABASE_PROJECT_ID.supabase.co/auth/v1/callback
in the Return URLs field. - 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
Press Continue and Save.
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
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.
Select your App ID and press Save.
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.
Next:
- Download the .p8 Key
- Write down your Developer Team ID
- Write down your Key ID
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
- The path to the downloaded service auth key.
- Your Developer Team ID
- Your ServicesID
- Your service auth key ID.
After doing that, execute the script by running ruby generateJWToken.rb
. Write down that generated token.
Connect Sign in with Apple to Supabase (AuthKit)
In your Supabase Dashboard, go to Authentication > Providers.
- Here, press on Apple. Enable Sign in with Apple. Then,
- enter your client ids: that is the ServicesID and your app bundle id (comma seperated with no space),
- enter the secret key that we have generated in the previous step
- Press on save.
You can also disable sign in with email, if you don't intend to add it yourself.
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.
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.
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.
After a successful log-in, enter a Post Title and press the Publish Button. Now you can see your post in a "Timeline"
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.