supabase-js v2 focuses on "quality-of-life" improvements for developers and addresses some of the largest pain points in v1. v2 includes type support, a rebuilt Auth library with async methods, improved errors, and more.
No new features will be added to supabase-js v1 , but we'll continuing merging security fixes to v1, with maintenance patches for the next 3 months.
Install the latest version
npm install @supabase/supabase-js@2
Optionally if you are using custom configuration with createClient
then follow below:
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, \{
schema: 'custom',
persistSession: false,
\})
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, \{
db: \{
schema: 'custom',
\},
auth: \{
persistSession: true,
\},
\})
Read more about the constructor options.
The signIn() method has been deprecated in favor of more explicit method signatures to help with type hinting. Previously it was difficult for developers to know what they were missing (e.g., a lot of developers didn't realize they could use passwordless magic links).
const \{ user, error \} = await supabase
.auth
.signIn(\{ email, password \})
const \{
data: \{ user \},
error,
\} = await supabase
.auth
.signInWithPassword(\{ email, password \})
const \{ error \} = await supabase
.auth
.signIn(\{ email \})
const \{ error \} = await supabase
.auth
.signInWithOtp(\{ email \})
const \{ error \} = await supabase
.auth
.signIn(\{ provider \})
const \{ error \} = await supabase
.auth
.signInWithOAuth(\{ provider \})
const \{ error \} = await supabase
.auth
.signIn(\{ phone, password \})
const \{ error \} = await supabase
.auth
.signInWithPassword(\{ phone, password \})
const \{ error \} = await supabase
.auth
.api
.sendMobileOTP(phone)
const \{ data, error \} = await supabase
.auth
.signInWithOtp(\{ phone \})
// After receiving a SMS with a OTP.
const \{ data, error \} = await supabase
.auth
.verifyOtp(\{ phone, token \})
const \{ data, error \} = await supabase
.auth
.api
.resetPasswordForEmail(email)
const \{ data, error \} = await supabase
.auth
.resetPasswordForEmail(email)
const session = supabase.auth.session()
const \{
data: \{ session \},
\} = await supabase.auth.getSession()
const user = supabase.auth.user()
const \{
data: \{ user \},
\} = await supabase.auth.getUser()
const \{ user, error \} = await supabase
.auth
.update(\{ attributes \})
const \{
data: \{ user \},
error,
\} = await supabase.auth.updateUser(\{ attributes \})
access_token
JWT with Supabaseconst \{ user, error \} = supabase.auth.setAuth(access_token)
const supabase = createClient(
SUPABASE_URL,
SUPABASE_ANON_KEY,
\{
global: \{
headers: \{
Authorization: `Bearer $\{access_token\}`,
\},
\},
\}
)
The cookie-related methods like setAuthCookie
and getUserByCookie
have been removed.
For Next.js you can use the Auth Helpers to help you manage cookies. If you can't use the Auth Helpers, you can use server-side rendering.
Some the PR for additional background information.
.insert()
/ .upsert()
/ .update()
/ .delete()
don't return rows by default: PR.
Previously, these methods return inserted/updated/deleted rows by default (which caused some confusion), and you can opt to not return it by specifying returning: 'minimal'
. Now the default behavior is to not return rows. To return inserted/updated/deleted rows, add a .select()
call at the end.
const \{ data, error \} = await supabase
.from('my_table')
.insert(\{ new_data \})
const \{ data, error \} = await supabase
.from('my_table')
.insert(\{ new_data \})
.select()
const \{ data, error \} = await supabase
.from('my_table')
.update(\{ new_data \})
.eq('id', id)
const \{ data, error \} = await supabase
.from('my_table')
.update(\{ new_data \})
.eq('id', id)
.select()
const \{ data, error \} = await supabase
.from('my_table')
.upsert(\{ new_data \})
const \{ data, error \} = await supabase
.from('my_table')
.upsert(\{ new_data \})
.select()
const \{ data, error \} = await supabase
.from('my_table')
.delete()
.eq('id', id)
const \{ data, error \} = await supabase
.from('my_table')
.delete()
.eq('id', id)
.select()
const userListener = supabase
.from('users')
.on('*', (payload) => handleAllEventsPayload(payload.new))
.subscribe()
const userListener = supabase
.channel('public:user')
.on('postgres_changes', \{ event: '*', schema: 'public', table: 'user' \}, (payload) =>
handleAllEventsPayload()
)
.subscribe()
userListener.unsubscribe()
supabase.removeChannel(userListener)