Supabase

How to secure Supabase endpoints with JWTs?

Discover how to safeguard your Supabase endpoints with JWTs. Dive into our easy-guide and protect your data like a pro while making sure your APIs stay safe and sound.

Developer profile skeleton
a developer thinking

Overview

Securing Supabase endpoints with JSON Web Tokens (JWTs) ensures that only authenticated users can access your API routes. JWTs are a standard for secure claim transfers between client and server. This involves generating JWTs when users authenticate, validating these tokens in your Supabase functions, and setting up roles and policies to control access. Handling and validating JWTs correctly is crucial for maintaining the integrity and security of your application's data and user actions. Understanding these steps will help you safeguard your Supabase endpoints effectively.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to secure Supabase endpoints with JWTs?

Step 1: Set Up Supabase Project

First things first, make sure you have a Supabase project. If you don't, no worries! Just head over to the Supabase website, sign up, and follow the steps to create a new project. Easy peasy.

Step 2: Enable JWTs in Supabase Settings

Next, go to the Settings section in your Supabase dashboard. Under API Settings, you'll find an option to enable JWT (JSON Web Token) settings. Toggle that on. Make sure your JWT Secret is set securely, as this will be used to sign and verify tokens. Don't lose it!

Step 3: Configure Database Policies

Now, let's get into the Auth Policies in the Supabase dashboard. We need to create Row Level Security (RLS) policies to enforce JWTs on your tables.

  • Head over to Database > Table Editor.
  • Pick the table you want to secure.
  • Enable Row Level Security.
  • Add policies to control who can read/write based on JWT claims.

Here's an example policy to restrict access to authenticated users:

CREATE POLICY "Authenticated users only"
ON your_table_name
FOR SELECT
USING (auth.role() = 'authenticated');

Step 4: Integrate Authentication (Client-Side)

Time to handle authentication on the client side using Supabase's client library.

  • First, install the Supabase client library:
    npm install @supabase/supabase-js
    
  • Then, initialize the Supabase client:
    ```
    import { createClient } from '@supabase/supabase-js';

const supabaseUrl = 'https://your-project.supabase.co';
const supabaseKey = 'public-anon-key'; // replace with your actual key
const supabase = createClient(supabaseUrl, supabaseKey);


- Sign in and get the JWT:

const { user, error } = await supabase.auth.signIn({
email: 'user@example.com',
password: 'password'
});

const token = user.access_token; // JWT Token


<div style="color:#0065ff"><h3>Step 5: Server-Side API Calls with JWTs</h3></div>

For server-side requests to Supabase, make sure to include the JWT for secure access:

import axios from 'axios';

const token = 'your.jwt.token';

axios.get('https://your-project.supabase.co/rest/v1/your_table', {
headers: {
Authorization: Bearer ${token}
}
})
.then(response => console.log(response.data))
.catch(error => console.log(error));


<div style="color:#0065ff"><h3>Step 6: Test Token Validation</h3></div>

To make sure everything's working:
- Try accessing endpoints without a token to see if they're blocked.
- Use valid JWTs to check if access is granted based on your policies.

<div style="color:#0065ff"><h3>Step 7: Monitor and Maintain Token Security</h3></div>

Keep an eye on your JWT secret keys. Make sure tokens are stored securely on both the client and server sides. Rotate secrets periodically if needed to keep things secure.

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.