Supabase Authentication and Authorization
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?
- First Install the package @supabase/supabase-js for using supabase with Next.js with javascript.
npm install @supabase/supabase-js
- Import it in the Project
import { createClient } from "@supabase/supabase-js";
- 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:
Go to the Dashboard of the Supabase
Go to Authentication —> Policies
Now you will see all the Tables and Policies associated with it
click on the “Create Policy” button for a Table you want to set the RLS Policy.
Select from Select, Insert, Update and Delete for setting Policy on them
After that Specify the SQL Code for that Policy
Click on “Save Policy” Button.
Subscribe to my newsletter
Read articles from Ajay Nadar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by