This tutorial demonstrates how to build a basic user management app. The app authenticates and identifies the user, stores their profile information in the database, and allows the user to log in, update their profile details, and upload a profile photo. The app uses:
Supabase Database - a Postgres database for storing your user data and Row Level Security so data is protected and users can only access their own information.
Before we start building we're going to set up our Database and API. This is as simple as starting a new Project in Supabase and then creating a "schema" inside the database.
Now we are going to set up the database schema. We can use the "User Management Starter" quickstart in the SQL Editor, or you can just copy/paste the SQL from below and run it yourself.
You can easily pull the database schema down to your local project by running the db pull command. Read the local development docs for detailed instructions.
_10
supabase link --project-ref <project-id>
_10
# You can get <project-id> from your project's dashboard URL: https://supabase.com/dashboard/project/<project-id>
Now that you've created some database tables, you are ready to insert data using the auto-generated API.
We just need to get the Project URL and anon key from the API settings.
Then let's install the only additional dependency: supabase-js
_10
npm install @supabase/supabase-js
And finally, we want to save the environment variables in the src/environments/environment.ts file.
All we need are the API URL and the anon key that you copied earlier.
These variables will be exposed on the browser, and that's completely fine since we have Row Level Security enabled on our Database.
environment.ts
_10
export const environment = {
_10
production: false,
_10
supabaseUrl: 'YOUR_SUPABASE_URL',
_10
supabaseKey: 'YOUR_SUPABASE_KEY',
_10
}
Now that we have the API credentials in place, let's create a SupabaseService with ionic g s supabase to initialize the Supabase client and implement functions to communicate with the Supabase API.
src/app/supabase.service.ts
_80
import { Injectable } from '@angular/core'
_80
import { LoadingController, ToastController } from '@ionic/angular'
_80
import { AuthChangeEvent, createClient, Session, SupabaseClient } from '@supabase/supabase-js'
_80
import { environment } from '../environments/environment'
Let's set up a route to manage logins and signups. We'll use Magic Links so users can sign in with their email without using passwords.
Create a LoginPage with the ionic g page login Ionic CLI command.
This guide will show the template inline, but the example app will have templateUrls
src/app/login/login.page.ts
_54
import { Component, OnInit } from '@angular/core'
_54
import { SupabaseService } from '../supabase.service'
_54
_54
@Component({
_54
selector: 'app-login',
_54
template: `
_54
<ion-header>
_54
<ion-toolbar>
_54
<ion-title>Login</ion-title>
_54
</ion-toolbar>
_54
</ion-header>
_54
_54
<ion-content>
_54
<div class="ion-padding">
_54
<h1>Supabase + Ionic Angular</h1>
_54
<p>Sign in via magic link with your email below</p>
After a user is signed in, we can allow them to edit their profile details and manage their account.
Create an AccountComponent with ionic g page account Ionic CLI command.
src/app/account.page.ts
_99
import { Component, OnInit } from '@angular/core'
_99
import { Router } from '@angular/router'
_99
import { Profile, SupabaseService } from '../supabase.service'
Let's create an avatar for the user so that they can upload a profile photo.
First, install two packages in order to interact with the user's camera.
_10
npm install @ionic/pwa-elements @capacitor/camera
CapacitorJS is a cross-platform native runtime from Ionic that enables web apps to be deployed through the app store and provides access to native device API.
Ionic PWA elements is a companion package that will polyfill certain browser APIs that provide no user interface with custom Ionic UI.
With those packages installed, we can update our main.ts to include an additional bootstrapping call for the Ionic PWA Elements.
src/main.ts
_15
import { enableProdMode } from '@angular/core'
_15
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'
_15
_15
import { AppModule } from './app/app.module'
_15
import { environment } from './environments/environment'
_15
_15
import { defineCustomElements } from '@ionic/pwa-elements/loader'
_15
defineCustomElements(window)
_15
_15
if (environment.production) {
_15
enableProdMode()
_15
}
_15
platformBrowserDynamic()
_15
.bootstrapModule(AppModule)
_15
.catch((err) => console.log(err))
Then create an AvatarComponent with this Ionic CLI command:
_10
ionic g component avatar --module=/src/app/account/account.module.ts --create-module
src/app/avatar.component.ts
_108
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
_108
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'
_108
import { SupabaseService } from '../supabase.service'
_108
import { Camera, CameraResultType } from '@capacitor/camera'