The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
run prettier
Jonathan Summers-Muir2d2eeddd6605
diff --git a/apps/reference/docs/guides/auth/auth-helpers/remix.mdx b/apps/reference/docs/guides/auth/auth-helpers/remix.mdxindex 652b73a918d..029b876d7b7 100644--- a/apps/reference/docs/guides/auth/auth-helpers/remix.mdx+++ b/apps/reference/docs/guides/auth/auth-helpers/remix.mdx@@ -66,11 +66,11 @@ SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEYLoader functions run on the server immediately before the component is rendered. They respond to all GET requests on a route. You can create an authenticated Supabase client by calling the `createSupabaseClient` function and passing it your `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and a `Request` and `Response`.```jsx-import { json } from '@remix-run/node'; // change this import to whatever runtime you are using-import { createSupabaseClient } from '@supabase/auth-helpers-remix';+import { json } from '@remix-run/node' // change this import to whatever runtime you are using+import { createSupabaseClient } from '@supabase/auth-helpers-remix'export const loader = async ({ request }) => {- const response = new Response();+ const response = new Response()// an empty response is required for the auth helpers// to set cookies to manage auth@@ -78,19 +78,19 @@ export const loader = async ({ request }) => {process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )- const { data } = await supabaseClient.from('test').select('*');+ const { data } = await supabaseClient.from('test').select('*')// in order for the set-cookie header to be set,// headers must be returned as part of the loader responsereturn json({ data },{- headers: response.headers+ headers: response.headers,}- );-};+ )+}```> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Loader` function.@@ -101,30 +101,30 @@ export const loader = async ({ request }) => {Loader functions run on the server immediately before the component is rendered. You can create an authenticated Supabase client by calling the `createSupabaseClient` function and passing it your `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and a `Request` and `Response`.```jsx-import { LoaderFunction, json } from '@remix-run/node'; // change this import to whatever runtime you are using-import { createSupabaseClient } from '@supabase/auth-helpers-remix';+import { LoaderFunction, json } from '@remix-run/node' // change this import to whatever runtime you are using+import { createSupabaseClient } from '@supabase/auth-helpers-remix'export const loader: LoaderFunction = async ({- request+ request,}: {- request: Request+ request: Request,}) => {- const response = new Response();+ const response = new Response()const supabaseClient = createSupabaseClient(process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )- const { data } = await supabaseClient.from('test').select('*');+ const { data } = await supabaseClient.from('test').select('*')return json({ data },{- headers: response.headers+ headers: response.headers,}- );-};+ )+}```> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Loader` function.@@ -145,27 +145,27 @@ export const loader: LoaderFunction = async ({Action functions run on the server and respond to HTTP requests to a route, other than GET - POST, PUT, PATCH, DELETE etc. You can create an authenticated Supabase client by calling the `createSupabaseClient` function and passing it your `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and a `Request` and `Response`.```jsx-import { json } from '@remix-run/node'; // change this import to whatever runtime you are using-import { createSupabaseClient } from '@supabase/auth-helpers-remix';+import { json } from '@remix-run/node' // change this import to whatever runtime you are using+import { createSupabaseClient } from '@supabase/auth-helpers-remix'export const action = async ({ request }) => {- const response = new Response();+ const response = new Response()const supabaseClient = createSupabaseClient(process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )- const { data } = await supabaseClient.from('test').select('*');+ const { data } = await supabaseClient.from('test').select('*')return json({ data },{- headers: response.headers+ headers: response.headers,}- );-};+ )+}```> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Action` function.@@ -176,31 +176,31 @@ export const action = async ({ request }) => {Action functions run on the server and respond to HTTP requests to a route, other than GET - POST, PUT, PATCH, DELETE etc. You can create an authenticated Supabase client by calling the `createSupabaseClient` function and passing it your `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and a `Request` and `Response`.```jsx-import { ActionFunction, json } from '@remix-run/node'; // change this import to whatever runtime you are using-import { createSupabaseClient } from '@supabase/auth-helpers-remix';+import { ActionFunction, json } from '@remix-run/node' // change this import to whatever runtime you are using+import { createSupabaseClient } from '@supabase/auth-helpers-remix'export const action: ActionFunction = async ({- request+ request,}: {- request: Request+ request: Request,}) => {- const response = new Response();+ const response = new Response()const supabaseClient = createSupabaseClient(process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )- const { data } = await supabaseClient.from('test').select('*');+ const { data } = await supabaseClient.from('test').select('*')return json({ data },{- headers: response.headers+ headers: response.headers,}- );-};+ )+}```> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Action` function.@@ -213,13 +213,15 @@ export const action: ActionFunction = async ({You can determine if a user is authenticated by checking their session using the `getSession` function.```jsx-const { data: { session }} = await supabaseClient.auth.getSession();+const {+ data: { session },+} = await supabaseClient.auth.getSession()```The session contains a user property.```jsx-const user = session?.user;+const user = session?.user```> This is the recommended way for accessing the logged in user. There is also a `getUser()` function but this does not refresh the session if it has expired.@@ -231,21 +233,22 @@ const user = session?.user;values={[{label: 'JavaScript', value: 'js'},{label: 'TypeScript', value: 'ts'},- ]}>+ ]}+><TabItem value="js">In order to use the Supabase client in the browser - fetching data in `useEffect` or subscribing to realtime events - we need to do a little more plumbing. Remix does not include a way to make environment variables available to the browser, so we need to pipe them through from a `loader` function in our `root.jsx` route and attach them to the `window`.```jsx title="app/root.jsx"export const loader = () => {- const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env;+ const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.envreturn json({env: {SUPABASE_URL,- SUPABASE_ANON_KEY- }- });-};+ SUPABASE_ANON_KEY,+ },+ })+}```> These may not be stored in `process.env` for environments other than Node.@@ -253,7 +256,7 @@ export const loader = () => {Next, we call the `useLoaderData` hook in our component to get the `env` object.```jsx title="app/root.jsx"-const { env } = useLoaderData();+const { env } = useLoaderData()```And then, add a `<script>` tag to attach these environment variables to the `window`. This should be placed immediately before the `<Scripts />` component in `app/root.jsx`@@ -261,7 +264,7 @@ And then, add a `<script>` tag to attach these environment variables to the `win```jsx title="app/root.jsx"<scriptdangerouslySetInnerHTML={{- __html: `window.env = ${JSON.stringify(env)}`+ __html: `window.env = ${JSON.stringify(env)}`,}}/>```@@ -269,7 +272,7 @@ And then, add a `<script>` tag to attach these environment variables to the `winFull example for Node:```jsx title="app/root.jsx"-import { json } from '@remix-run/node'; // change this import to whatever runtime you are using+import { json } from '@remix-run/node' // change this import to whatever runtime you are usingimport {Form,Links,@@ -278,28 +281,28 @@ import {Outlet,Scripts,ScrollRestoration,- useLoaderData-} from '@remix-run/react';-import { createSupabaseClient } from '@supabase/auth-helpers-remix';+ useLoaderData,+} from '@remix-run/react'+import { createSupabaseClient } from '@supabase/auth-helpers-remix'export const meta = () => ({charset: 'utf-8',title: 'New Remix App',- viewport: 'width=device-width,initial-scale=1'-});+ viewport: 'width=device-width,initial-scale=1',+})export const loader = () => {- const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env;+ const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.envreturn json({env: {SUPABASE_URL,- SUPABASE_ANON_KEY- }- });-};+ SUPABASE_ANON_KEY,+ },+ })+}export default function App() {- const { env } = useLoaderData();+ const { env } = useLoaderData()return (<html lang="en">@@ -312,14 +315,14 @@ export default function App() {<ScrollRestoration /><scriptdangerouslySetInnerHTML={{- __html: `window.env = ${JSON.stringify(env)}`+ __html: `window.env = ${JSON.stringify(env)}`,}}/><Scripts /><LiveReload /></body></html>- );+ )}```@@ -333,14 +336,14 @@ In order to use the Supabase client in the browser - fetching data in `useEffect```jsx title="app/root.tsx"export const loader = () => {- const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env;+ const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.envreturn json({env: {SUPABASE_URL,- SUPABASE_ANON_KEY- }- });-};+ SUPABASE_ANON_KEY,+ },+ })+}```> These may not be stored in `process.env` for environments other than Node.@@ -348,7 +351,7 @@ export const loader = () => {Next, we call the `useLoaderData` hook in our component to get the `env` object.```jsx title="app/root.tsx"-const { env } = useLoaderData();+const { env } = useLoaderData()```And then, add a `<script>` tag to attach these environment variables to the `window`. This should be placed immediately before the `<Scripts />` component in `app/root.tsx`@@ -356,7 +359,7 @@ And then, add a `<script>` tag to attach these environment variables to the `win```jsx title="app/root.tsx"<scriptdangerouslySetInnerHTML={{- __html: `window.env = ${JSON.stringify(env)}`+ __html: `window.env = ${JSON.stringify(env)}`,}}/>```@@ -364,7 +367,7 @@ And then, add a `<script>` tag to attach these environment variables to the `winFull example for Node:```jsx title="app/root.tsx"-import { json, MetaFunction, LoaderFunction } from '@remix-run/node'; // change this import to whatever runtime you are using+import { json, MetaFunction, LoaderFunction } from '@remix-run/node' // change this import to whatever runtime you are usingimport {Form,Links,@@ -373,28 +376,28 @@ import {Outlet,Scripts,ScrollRestoration,- useLoaderData-} from '@remix-run/react';-import { createSupabaseClient } from '@supabase/auth-helpers-remix';+ useLoaderData,+} from '@remix-run/react'+import { createSupabaseClient } from '@supabase/auth-helpers-remix'export const meta: MetaFunction = () => ({charset: 'utf-8',title: 'New Remix App',- viewport: 'width=device-width,initial-scale=1'-});+ viewport: 'width=device-width,initial-scale=1',+})export const loader: LoaderFunction = () => {- const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env;+ const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.envreturn json({env: {SUPABASE_URL,- SUPABASE_ANON_KEY- }- });-};+ SUPABASE_ANON_KEY,+ },+ })+}export default function App() {- const { env } = useLoaderData();+ const { env } = useLoaderData()return (<html lang="en">@@ -407,14 +410,14 @@ export default function App() {<ScrollRestoration /><scriptdangerouslySetInnerHTML={{- __html: `window.env = ${JSON.stringify(env)}`+ __html: `window.env = ${JSON.stringify(env)}`,}}/><Scripts /><LiveReload /></body></html>- );+ )}```@@ -451,29 +454,29 @@ Any of the [supported authentication strategies from `supabase-js`](https://supa```jsxexport const action = async ({ request }) => {- const { email, password } = Object.fromEntries(await request.formData());- const response = new Response();+ const { email, password } = Object.fromEntries(await request.formData())+ const response = new Response()const supabaseClient = createServerClient(process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )const { data, error } = await supabaseClient.auth.signUp({email,- password- });+ password,+ })// in order for the set-cookie header to be set,// headers must be returned as part of the loader responsereturn json({ data, error },{- headers: response.headers+ headers: response.headers,}- );-};+ )+}```</TabItem>@@ -530,29 +533,29 @@ Any of the [supported authentication strategies from `supabase-js`](https://supa```jsxexport const action = async ({ request }) => {- const { email, password } = Object.fromEntries(await request.formData());- const response = new Response();+ const { email, password } = Object.fromEntries(await request.formData())+ const response = new Response()const supabaseClient = createServerClient(process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )const { data, error } = await supabaseClient.auth.signInWithPassword({email: String(loginEmail),- password: String(loginPassword)- });+ password: String(loginPassword),+ })// in order for the set-cookie header to be set,// headers must be returned as part of the loader responsereturn json({ data, error },{- headers: response.headers+ headers: response.headers,}- );-};+ )+}```</TabItem>@@ -607,26 +610,26 @@ export const action: ActionFunction = async ({```jsxexport const action = async ({ request }) => {- const { email, password } = Object.fromEntries(await request.formData());- const response = new Response();+ const { email, password } = Object.fromEntries(await request.formData())+ const response = new Response()const supabaseClient = createServerClient(process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )- const { error } = await supabaseClient.auth.signOut();+ const { error } = await supabaseClient.auth.signOut()// in order for the set-cookie header to be set,// headers must be returned as part of the loader responsereturn json({ error },{- headers: response.headers+ headers: response.headers,}- );-};+ )+}```</TabItem>@@ -667,34 +670,34 @@ export const action: ActionFunction = async ({## Subscribe to realtime events```jsx-import { createBrowserClient } from '@supabase/auth-helpers-remix';-import { useState, useEffect } from 'react';+import { createBrowserClient } from '@supabase/auth-helpers-remix'+import { useState, useEffect } from 'react'export default function SubscribeToRealtime() {- const [data, setData] = useState([]);+ const [data, setData] = useState([])useEffect(() => {const supabaseClient = createBrowserClient(window.env.SUPABASE_URL,window.env.SUPABASE_ANON_KEY- );+ )const channel = supabaseClient.channel('test').on('postgres_changes',{ event: 'INSERT', schema: 'public', table: 'test' },(payload) => {- setData((data) => [...data, payload.new]);+ setData((data) => [...data, payload.new])})- .subscribe();+ .subscribe()return () => {- supabaseClient.removeChannel(channel);- };- }, [session]);+ supabaseClient.removeChannel(channel)+ }+ }, [session])- return <pre>{JSON.stringify({ data }, null, 2)}</pre>;+ return <pre>{JSON.stringify({ data }, null, 2)}</pre>}```@@ -800,8 +803,8 @@ You can pass types that were [generated with the Supabase CLI](/docs/reference/j### Server-side```ts-import { createServerClient } from '@supabase/auth-helpers-remix';-import { Database } from '../../db_types';+import { createServerClient } from '@supabase/auth-helpers-remix'+import { Database } from '../../db_types'export const loader = async ({ request }) => {const response = new Response()@@ -810,20 +813,20 @@ export const loader = async ({ request }) => {process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )}```### Client-side```ts-import { createBrowserClient } from '@supabase/auth-helpers-remix';-import { Database } from '../../db_types';+import { createBrowserClient } from '@supabase/auth-helpers-remix'+import { Database } from '../../db_types'const supabaseClient = createBrowserClient<Database>(process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY-);+)```## Server-side data fetching to OAuth APIs using `provider_token`@@ -831,66 +834,63 @@ const supabaseClient = createBrowserClient<Database>(When using third-party auth providers, sessions are initiated with an additional `provider_token` field which is persisted as an HTTPOnly cookie upon logging in to enabled usage on the server side. The `provider_token` can be used to make API requests to the OAuth provider's API endpoints on behalf of the logged-in user. In the following example, we fetch the user's full profile from the third-party API using their id and auth token:```jsx-import { User, createServerClient } from '@supabase/auth-helpers-remix';-import { ActionFunction, json, redirect } from '@remix-run/node'; // change this import to whatever runtime you are using-import { createServerClient } from '@supabase/auth-helpers-remix';+import { User, createServerClient } from '@supabase/auth-helpers-remix'+import { ActionFunction, json, redirect } from '@remix-run/node' // change this import to whatever runtime you are using+import { createServerClient } from '@supabase/auth-helpers-remix'interface Profile {/* ... */}export const loader: LoaderFunction = async ({- request+ request,}: {- request: Request+ request: Request,}) => {- const response = new Response();+ const response = new Response()const supabaseClient = createServerClient(process.env.SUPABASE_URL,process.env.SUPABASE_ANON_KEY,{ request, response }- );+ )const {- data: { session }- } = await supabaseClient.auth.getSession();+ data: { session },+ } = await supabaseClient.auth.getSession()- const user = session?.user;+ const user = session?.userif (!user) {// there is no user, therefore, we are redirecting// to the landing page. we still need to return// response.headers to attach the set-cookie headerreturn redirect('/', {- headers: response.headers- });+ headers: response.headers,+ })}// Retrieve provider_token from cookies- const provider_token = request.headers.get('sb-provider-token');+ const provider_token = request.headers.get('sb-provider-token')if (!provider_token) {// there is no provider_token, therefore, we are redirecting// to the landing page. we still need to return// response.headers to attach the set-cookie headerreturn redirect('/', {- headers: response.headers- });+ headers: response.headers,+ })}// Get logged in user's third-party id from metadata- const userId = user?.user_metadata.provider_id;+ const userId = user?.user_metadata.provider_idconst allRepos = await (- await fetch(- `https://api.github.com/search/repositories?q=user:${userId}`,- {- method: 'GET',- headers: {- Authorization: `token ${provider_token}`,- },- }- )+ await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {+ method: 'GET',+ headers: {+ Authorization: `token ${provider_token}`,+ },+ })).json()// in order for the set-cookie header to be set,@@ -898,13 +898,13 @@ export const loader: LoaderFunction = async ({return json({ allRepos, user },{- headers: response.headers+ headers: response.headers,}- );-};+ )+}export default function Page() {- const { allRepos, user } = useLoaderData();- return <pre>{JSON.stringify({ allRepos, user }, null, 2)}</pre>;+ const { allRepos, user } = useLoaderData()+ return <pre>{JSON.stringify({ allRepos, user }, null, 2)}</pre>}```