/supabase-tutorials

How to track auth events in Supabase?

Track Supabase auth events effortlessly with our guide. Set up your project, configure the database, create SQL triggers, and integrate your JS app for live monitoring.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to track auth events in Supabase?

 

Step 1: Set Up Your Supabase Project

 

  • Go to the Supabase website and sign in to your account.
  • Create a new project by clicking the "New Project" button.
  • Fill in the details like your project name, organization, and region, then click "Create New Project".
  • Once the project is created, you will be taken to the project dashboard.

 

Step 2: Configure Database

 

  • In the Supabase dashboard, navigate to the "Database" section.
  • Click on the "Tables" tab to create a new table to store your auth events.
  • Click "New Table" and set a name for the table, e.g., auth_events.
  • Define the columns for your table:
  • id: UUID, Primary Key
  • user_id: UUID
  • event_type: Text
  • timestamp: Timestamp
  • Save the changes to create the table.

 

Step 3: Setup Supabase Functions

 

  • In the Supabase dashboard, navigate to the "SQL" section.
  • Click "New Query" to write and execute a SQL function that will be triggered on auth events.
CREATE OR REPLACE FUNCTION log_auth_event()
RETURNS TRIGGER AS $$
BEGIN
  INSERT INTO auth_events (user_id, event_type, timestamp)
  VALUES (NEW.id, TG_ARGV[0], NOW());
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
  • Execute the function query to create the function in the database.

 

Step 4: Configure Triggers for Auth Events

 

  • In the same "SQL" section, create triggers that call this function for relevant auth events.
-- Trigger on signup event
CREATE TRIGGER on_signup
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION log_auth_event('signup');

-- Trigger on delete event
CREATE TRIGGER on_delete
AFTER DELETE ON auth.users
FOR EACH ROW EXECUTE FUNCTION log_auth_event('delete');
  • Execute these queries to set up the triggers for your events.

 

Step 5: Access the Auth Event Data

 

  • Navigate back to the "Database" section and select your auth_events table.
  • Here, you can view and query all tracked auth events as they occur.
SELECT * FROM auth_events ORDER BY timestamp DESC;
  • Use this to monitor or analyze user authentication activity.

 

Step 6: Integrate Event Tracking in Your Application

 

  • Use Supabase's JavaScript client or any other client library supported by Supabase in your application to write signup and delete operations.
  • Ensure you are importing the supabase client in your project. If you haven't installed the client, use npm to install it:
npm install @supabase/supabase-js
  • Implement basic auth operations in your app which will, in turn, trigger the events:
import { createClient } from '@supabase/supabase-js'

const supabaseUrl = 'your-supabase-url'
const supabaseAnonKey = 'your-anon-key'
const supabase = createClient(supabaseUrl, supabaseAnonKey)

// Sign up a new user
const signUpUser = async (email, password) => {
  const { user, session, error } = await supabase.auth.signUp({
    email: email,
    password: password
  })
  
  if (error) console.error('Error signing up:', error)
  else console.log('User signed up:', user)
}

// Delete a user
const deleteUser = async () => {
  const user = supabase.auth.user()

  if (user) {
    const { error } = await supabase.auth.api.deleteUser(user.id, supabase.config.jwtSecret)

    if (error) console.error('Error deleting user:', error)
    else console.log('User deleted')
  }
}
  • Once you perform these actions in your app, the corresponding auth events should be logged in your auth_events table.

 

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022