Supabase Authentication and Authorization

Ajay NadarAjay Nadar
2 min read

Introduction

Supabase Auth makes it easy to implement authentication and authorization in your app.

What is Authentication and Authorization?

What is Authentication?

Authentication means checking that a user is who they say they are

What is Authorization?
Authorization
means checking what resources user is allowed to access.

How to Install and perform Signup/Login in Next.js (Javascript) using Supabase?

  1. First Install the package @supabase/supabase-js for using supabase with Next.js with javascript.
npm install @supabase/supabase-js
  1. Import it in the Project
import { createClient } from "@supabase/supabase-js";
  1. Create Client and Pass the arguments Project url and anon key of the supabase
  const supabase = createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
  );

Sign Up:

let { data, error } = await supabase.auth.signUp({  email: 'someone@email.com',  password: 'DKtBWSsBgrZcGvEttzrc'})

Login:

let { data, error } = await supabase.auth.signInWithPassword({  email: 'someone@email.com',  password: 'DKtBWSsBgrZcGvEttzrc'})

JWT

JWT (Json Web Token) is a Protocol for Information sharing between Client and Server. JWT is used for Authentication of users and allows them access to protected resources.

In this process, the server generates a signed JWT and sends it to the client. The client then includes this token in subsequent requests to the server to authenticate themselves.

RLS

Row Level Security allows us to Define Granular Authorization Rules. RLS can be enabled in a Table in Public Schema to restrict unauthorized access to Table and Allows only users access based on the RLS Policy that is Defined when RLS is enabled. Disabling RLS will allow anyone with anon key and project url to access, update, delete, insert data in the Table.

Steps for Defining RLS Policy:

  1. Go to the Dashboard of the Supabase

  2. Go to Authentication —> Policies

  3. Now you will see all the Tables and Policies associated with it

  4. click on the “Create Policy” button for a Table you want to set the RLS Policy.

  5. Select from Select, Insert, Update and Delete for setting Policy on them

  6. After that Specify the SQL Code for that Policy

  7. Click on “Save Policy” Button.

10
Subscribe to my newsletter

Read articles from Ajay Nadar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ajay Nadar
Ajay Nadar