Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: fix remix auth helper docs

thorwebdevfb0b6b134204
diff --git a/apps/reference/docs/guides/auth/auth-helpers/nextjs.mdx b/apps/reference/docs/guides/auth/auth-helpers/nextjs.mdx
index 4e8ffdd87dc..b509792f914 100644
--- a/apps/reference/docs/guides/auth/auth-helpers/nextjs.mdx
+++ b/apps/reference/docs/guides/auth/auth-helpers/nextjs.mdx
@@ -70,6 +70,7 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY
## Basic Setup
<Tabs
+ groupId="language"
defaultValue="js"
values={[
{label: 'JavaScript', value: 'js'},
diff --git a/apps/reference/docs/guides/auth/auth-helpers/remix.mdx b/apps/reference/docs/guides/auth/auth-helpers/remix.mdx
index fcfd3c57c0e..023eada8d07 100644
--- a/apps/reference/docs/guides/auth/auth-helpers/remix.mdx
+++ b/apps/reference/docs/guides/auth/auth-helpers/remix.mdx
@@ -53,9 +53,12 @@ SUPABASE_URL=YOUR_SUPABASE_URL
SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY
```
-## Loader
+## Basic Setup
+
+In your "root route" load environment variables and session and make them available client-side:
<Tabs
+ groupId="language"
defaultValue="js"
values={[
{label: 'JavaScript', value: 'js'},
@@ -63,29 +66,33 @@ SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY
]}>
<TabItem value="js">
-Loader 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 `createServerClient` 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 { createServerClient } from '@supabase/auth-helpers-remix'
-
+```jsx title="app/root.jsx"
export const loader = async ({ request }) => {
- const response = new Response()
- // an empty response is required for the auth helpers
- // to set cookies to manage auth
+ // Environment variables may be stored somewhere other than `process.env` in runtimes other than node.
+ // We need to pipe these Supabase environment variables to the browser.
+ const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env
- const supabaseClient = createServerClient(
- process.env.SUPABASE_URL,
- process.env.SUPABASE_ANON_KEY,
+ // We can retrieve the session on the server and hand it to the client.
+ // This is used to make sure the session is available immediately upon rendering.
+ const response = new Response()
+ const supabase = createServerClient(
+ process.env.SUPABASE_URL!,
+ process.env.SUPABASE_ANON_KEY!,
{ request, response }
)
+ const {
+ data: { session: initialSession },
+ } = await supabase.auth.getSession()
- 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 response
+ // In order for the set-cookie header to be set, headers must be returned as part of the loader response.
return json(
- { data },
+ {
+ initialSession,
+ env: {
+ SUPABASE_URL,
+ SUPABASE_ANON_KEY,
+ },
+ },
{
headers: response.headers,
}
@@ -93,33 +100,36 @@ export const loader = async ({ request }) => {
}
```
-> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Loader` function.
-
</TabItem>
<TabItem value="ts">
-Loader functions run on the server immediately before the component is rendered. You can create an authenticated Supabase client by calling the `createServerClient` 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 { createServerClient } from '@supabase/auth-helpers-remix'
+```tsx title="app/root.tsx"
+export const loader: LoaderFunction = async ({ request }) => {
+ // Environment variables may be stored somewhere other than `process.env` in runtimes other than node.
+ // We need to pipe these Supabase environment variables to the browser.
+ const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env
-export const loader: LoaderFunction = async ({
- request,
-}: {
- request: Request,
-}) => {
+ // We can retrieve the session on the server and hand it to the client.
+ // This is used to make sure the session is available immediately upon rendering.
const response = new Response()
- const supabaseClient = createServerClient(
- process.env.SUPABASE_URL,
- process.env.SUPABASE_ANON_KEY,
+ const supabase = createServerClient<Database>(
+ process.env.SUPABASE_URL!,
+ process.env.SUPABASE_ANON_KEY!,
{ request, response }
)
+ const {
+ data: { session: initialSession },
+ } = await supabase.auth.getSession()
- 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 response.
return json(
- { data },
+ {
+ initialSession,
+ env: {
+ SUPABASE_URL,
+ SUPABASE_ANON_KEY,
+ },
+ },
{
headers: response.headers,
}
@@ -127,14 +137,13 @@ export const loader: LoaderFunction = async ({
}
```
-> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Loader` function.
-
</TabItem>
</Tabs>
-## Action
+Next, upon the initial client-side rendering of your application, set up a browser client and listen to session changes and expose both throughout your routes via the outlet context:
<Tabs
+ groupId="language"
defaultValue="js"
values={[
{label: 'JavaScript', value: 'js'},
@@ -142,313 +151,193 @@ export const loader: LoaderFunction = async ({
]}>
<TabItem value="js">
-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 `createServerClient` 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 { createServerClient } from '@supabase/auth-helpers-remix'
+```jsx title="app/root.jsx"
+import { useEffect, useState } from 'react'
+import { json } from '@remix-run/node'
+import { Outlet, useLoaderData } from '@remix-run/react'
+import {
+ createServerClient,
+ createBrowserClient,
+} from '@supabase/auth-helpers-remix'
-export const action = async ({ request }) => {
- const response = new Response()
+export const loader: LoaderFunction = async ({ request }) => {
+ // ... see loader function above.
+}
- const supabaseClient = createServerClient(
- process.env.SUPABASE_URL,
- process.env.SUPABASE_ANON_KEY,
- { request, response }
- )
+export default function App() {
+ const { env, initialSession } = useLoaderData()
+ const [supabase, setSupabase] = useState(null)
+ const [session, setSession] = useState(initialSession)
- const { data } = await supabaseClient.from('test').select('*')
+ const context = { supabase, session }
- return json(
- { data },
- {
- headers: response.headers,
+ useEffect(() => {
+ if (!supabase) {
+ const supabase = createBrowserClient(
+ env.SUPABASE_URL,
+ env.SUPABASE_ANON_KEY
+ )
+ setSupabase(supabase)
+ const {
+ data: { subscription },
+ } = supabase.auth.onAuthStateChange((_, session) => setSession(session))
+ return () => {
+ subscription.unsubscribe()
+ }
}
+ }, [])
+
+ return (
+ <html lang="en">
+ <body>
+ <Outlet context={context} />
+ </body>
+ </html>
)
}
```
-> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Action` function.
-
</TabItem>
<TabItem value="ts">
-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 `createServerClient` function and passing it your `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and a `Request` and `Response`.
+```tsx title="app/root.tsx"
+import { useEffect, useState } from 'react'
+import { json, LoaderFunction } from '@remix-run/node'
+import { Outlet, useLoaderData } from '@remix-run/react'
+import {
+ createServerClient,
+ createBrowserClient,
+ supabase,
+ Session,
+} from '@supabase/auth-helpers-remix'
+import { Database } from '../db_types'
-```jsx
-import { ActionFunction, json } from '@remix-run/node' // change this import to whatever runtime you are using
-import { createServerClient } from '@supabase/auth-helpers-remix'
+export type ContextType = {
+ supabase: supabase<Database> | null
+ session: Session | null
+}
-export const action: ActionFunction = async ({
- request,
-}: {
- request: Request,
-}) => {
- const response = new Response()
+type LoaderData = {
+ env: { SUPABASE_URL: string; SUPABASE_ANON_KEY: string }
+ initialSession: Session | null
+}
- const supabaseClient = createServerClient(
- process.env.SUPABASE_URL,
- process.env.SUPABASE_ANON_KEY,
- { request, response }
- )
+export const loader: LoaderFunction = async ({ request }) => {
+ // ... see loader function above.
+}
- const { data } = await supabaseClient.from('test').select('*')
+export default function App() {
+ const { env, initialSession } = useLoaderData<LoaderData>()
+ const [supabase, setSupabase] = useState<supabase | null>(null)
+ const [session, setSession] = useState<Session | null>(initialSession)
- return json(
- { data },
- {
- headers: response.headers,
+ const context: ContextType = { supabase, session }
+
+ useEffect(() => {
+ if (!supabase) {
+ const supabase = createBrowserClient<Database>(
+ env.SUPABASE_URL,
+ env.SUPABASE_ANON_KEY
+ )
+ setSupabase(supabase)
+ const {
+ data: { subscription },
+ } = supabase.auth.onAuthStateChange((_, session) => setSession(session))
+ return () => {
+ subscription.unsubscribe()
+ }
}
+ }, [])
+
+ return (
+ <html lang="en">
+ <body>
+ <Outlet context={context} />
+ </body>
+ </html>
)
}
```
-> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Action` function.
-
</TabItem>
</Tabs>
-## Session and User
-
-You can determine if a user is authenticated by checking their session using the `getSession` function.
-
-```jsx
-const {
- data: { session },
-} = await supabaseClient.auth.getSession()
-```
-
-The session contains a user property.
-
-```jsx
-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.
-
-## Client-side
+Now the supabase client and session are available throughout the application via the `useOutletContext`:
<Tabs
+ groupId="language"
defaultValue="js"
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
- return json({
- env: {
- SUPABASE_URL,
- SUPABASE_ANON_KEY,
- },
- })
-}
-```
-
-> These may not be stored in `process.env` for environments other than Node.
-
-Next, we call the `useLoaderData` hook in our component to get the `env` object.
-
-```jsx title="app/root.jsx"
-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`
-
-```jsx title="app/root.jsx"
-<script
- dangerouslySetInnerHTML={{
- __html: `window.env = ${JSON.stringify(env)}`,
- }}
-/>
-```
+```jsx title="app/routes/index.jsx"
+import { useOutletContext } from '@remix-run/react'
-Full example for Node:
+export default function Index() {
+ const { supabase, session } = useOutletContext()
-```jsx title="app/root.jsx"
-import { json } from '@remix-run/node' // change this import to whatever runtime you are using
-import {
- Form,
- Links,
- LiveReload,
- Meta,
- Outlet,
- Scripts,
- ScrollRestoration,
- useLoaderData,
-} from '@remix-run/react'
-import {
- createBrowserClient,
- createServerClient,
-} from '@supabase/auth-helpers-remix'
-
-export const meta = () => ({
- charset: 'utf-8',
- title: 'New Remix App',
- viewport: 'width=device-width,initial-scale=1',
-})
-
-export const loader = () => {
- const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env
- return json({
- env: {
- SUPABASE_URL,
- SUPABASE_ANON_KEY,
- },
- })
-}
-
-export default function App() {
- const { env } = useLoaderData()
-
- return (
- <html lang="en">
- <head>
- <Meta />
- <Links />
- </head>
- <body>
- <Outlet />
- <ScrollRestoration />
- <script
- dangerouslySetInnerHTML={{
- __html: `window.env = ${JSON.stringify(env)}`,
- }}
- />
- <Scripts />
- <LiveReload />
- </body>
- </html>
- )
+ return <pre>{JSON.stringify({ session }, null, 2)}</pre>
}
```
-Now we can call `createBrowserClient` in our components to fetch data client-side, or subscribe to realtime events - changes in the database.
-
</TabItem>
-
<TabItem value="ts">
-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.tsx` route and attach them to the `window`.
+```tsx title="app/routes/index.tsx"
+import { useOutletContext } from '@remix-run/react'
+import type { ContextType } from '../root'
-```jsx title="app/root.tsx"
-export const loader = () => {
- const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env
- return json({
- env: {
- SUPABASE_URL,
- SUPABASE_ANON_KEY,
- },
- })
-}
-```
+export default function Index() {
+ const { supabase, session } = useOutletContext<ContextType>()
-> These may not be stored in `process.env` for environments other than Node.
-
-Next, we call the `useLoaderData` hook in our component to get the `env` object.
-
-```jsx title="app/root.tsx"
-const { env } = useLoaderData()
+ return <pre>{JSON.stringify({ session }, null, 2)}</pre>
+}
```
-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`
+</TabItem>
+</Tabs>
-```jsx title="app/root.tsx"
-<script
- dangerouslySetInnerHTML={{
- __html: `window.env = ${JSON.stringify(env)}`,
- }}
-/>
-```
+You can find a full working example in the [source code on GitHub](https://github.com/supabase/auth-helpers/tree/main/examples/remix).
-Full example for Node:
+## Usage with TypeScript
-```jsx title="app/root.tsx"
-import { json, MetaFunction, LoaderFunction } from '@remix-run/node' // change this import to whatever runtime you are using
-import {
- Form,
- Links,
- LiveReload,
- Meta,
- Outlet,
- Scripts,
- ScrollRestoration,
- useLoaderData,
-} from '@remix-run/react'
-import {
- createBrowserClient,
- createServerClient,
-} from '@supabase/auth-helpers-remix'
+You can pass types that were [generated with the Supabase CLI](/docs/reference/javascript/typescript-support#generating-types) to the `createServerClient` or `createBrowserClient` functions to get enhanced type safety and auto completion:
-export const meta: MetaFunction = () => ({
- charset: 'utf-8',
- title: 'New Remix App',
- viewport: 'width=device-width,initial-scale=1',
-})
+### Server-side
-export const loader: LoaderFunction = () => {
- const { SUPABASE_URL, SUPABASE_ANON_KEY } = process.env
- return json({
- env: {
- SUPABASE_URL,
- SUPABASE_ANON_KEY,
- },
- })
-}
+```tsx
+import { createServerClient } from '@supabase/auth-helpers-remix'
+import { Database } from '../../db_types'
-export default function App() {
- const { env } = useLoaderData()
+export const loader = async ({ request }) => {
+ const response = new Response()
- return (
- <html lang="en">
- <head>
- <Meta />
- <Links />
- </head>
- <body>
- <Outlet />
- <ScrollRestoration />
- <script
- dangerouslySetInnerHTML={{
- __html: `window.env = ${JSON.stringify(env)}`,
- }}
- />
- <Scripts />
- <LiveReload />
- </body>
- </html>
+ const supabase = createServerClient<Database>(
+ process.env.SUPABASE_URL,
+ process.env.SUPABASE_ANON_KEY,
+ { request, response }
)
}
```
-Now we can call `createBrowserClient` in our components to fetch data client-side, or subscribe to realtime events - changes in the database.
-
-</TabItem>
-</Tabs>
-
-## Authentication
-
-Now that authentication is based on cookies, users can sign in and out server-side with actions.
+### Client-side
-Given this Remix `<Form />` component.
+```tsx
+import { createBrowserClient } from '@supabase/auth-helpers-remix'
+import { Database } from '../../db_types'
-```jsx
-<Form method="post">
- <input type="text" name="email" />
- <input type="password" name="password" />
- <button type="submit">Go!</button>
-</Form>
+const supabase = createBrowserClient<Database>(SUPABASE_URL, SUPABASE_ANON_KEY)
```
-### Signing Up
+## Loader
+
+Loader 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 `createServerClient` function and passing it your `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and a `Request` and `Response`.
<Tabs
+ groupId="language"
defaultValue="js"
values={[
{label: 'JavaScript', value: 'js'},
@@ -456,28 +345,27 @@ Given this Remix `<Form />` component.
]}>
<TabItem value="js">
-Any of the [supported authentication strategies from `supabase-js`](https://supabase.com/docs/reference/javascript/auth-signup) will work server-side. This is how you would handle simple `email` and `password` auth.
-
```jsx
-export const action = async ({ request }) => {
- const { email, password } = Object.fromEntries(await request.formData())
+import { json } from '@remix-run/node' // change this import to whatever runtime you are using
+import { createServerClient } from '@supabase/auth-helpers-remix'
+
+export const loader = async ({ request }) => {
const response = new Response()
+ // an empty response is required for the auth helpers
+ // to set cookies to manage auth
- const supabaseClient = createServerClient(
+ const supabase = createServerClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY,
{ request, response }
)
- const { data, error } = await supabaseClient.auth.signUp({
- email,
- password,
- })
+ const { data } = await supabase.from('test').select('*')
// in order for the set-cookie header to be set,
// headers must be returned as part of the loader response
return json(
- { data, error },
+ { data },
{
headers: response.headers,
}
@@ -486,48 +374,42 @@ export const action = async ({ request }) => {
```
</TabItem>
-
<TabItem value="ts">
-Any of the [supported authentication strategies from `supabase-js`](https://supabase.com/docs/reference/javascript/auth-signup) will work server-side. This is how you would handle simple `email` and `password` auth.
-
-```jsx
-export const action: ActionFunction = async ({
- request
-}: {
- request: Request;
-}) => {
- const { email, password } = Object.fromEntries(await request.formData());
- const response = new Response();
+```tsx
+import { LoaderFunction, json } from '@remix-run/node' // change this import to whatever runtime you are using
+import { createServerClient } from '@supabase/auth-helpers-remix'
- const supabaseClient = createServerClient(
- process.env.SUPABASE_URL!,
- process.env.SUPABASE_ANON_KEY!,
+export const loader: LoaderFunction = async ({ request }) => {
+ const response = new Response()
+ const supabase = createServerClient(
+ process.env.SUPABASE_URL,
+ process.env.SUPABASE_ANON_KEY,
{ request, response }
- );
+ )
- const { data, error } = await supabaseClient.auth.signUp({
- email: String(registerEmail),
- password: String(registerPassword)
- });
+ const { data } = await supabase.from('test').select('*')
- // in order for the set-cookie header to be set,
- // headers must be returned as part of the loader response
return json(
- { data, error },
+ { data },
{
- headers: response.headers
+ headers: response.headers,
}
- );
-};
+ )
+}
```
</TabItem>
</Tabs>
-### Login
+> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Loader` function.
+
+## Action
+
+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 `createServerClient` function and passing it your `SUPABASE_URL`, `SUPABASE_ANON_KEY`, and a `Request` and `Response`.
<Tabs
+ groupId="language"
defaultValue="js"
values={[
{label: 'JavaScript', value: 'js'},
@@ -535,28 +417,23 @@ export const action: ActionFunction = async ({
]}>
<TabItem value="js">
-Any of the [supported authentication strategies from `supabase-js`](https://supabase.com/docs/reference/javascript/auth-signinwithpassword) will work server-side. This is how you would handle simple `email` and `password` auth.
-
```jsx
+import { json } from '@remix-run/node' // change this import to whatever runtime you are using
+import { createServerClient } from '@supabase/auth-helpers-remix'
+
export const action = async ({ request }) => {
- const { email, password } = Object.fromEntries(await request.formData())
const response = new Response()
- const supabaseClient = createServerClient(
+ const supabase = 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),
- })
+ const { data } = await supabase.from('test').select('*')
- // in order for the set-cookie header to be set,
- // headers must be returned as part of the loader response
return json(
- { data, error },
+ { data },
{
headers: response.headers,
}
@@ -565,48 +442,65 @@ export const action = async ({ request }) => {
```
</TabItem>
-
<TabItem value="ts">
-Any of the [supported authentication strategies from `supabase-js`](https://supabase.com/docs/reference/javascript/auth-signinwithpassword) will work server-side. This is how you would handle simple `email` and `password` auth.
+```tsx
+import { ActionFunction, json } from '@remix-run/node' // change this import to whatever runtime you are using
+import { createServerClient } from '@supabase/auth-helpers-remix'
-```jsx
-export const action: ActionFunction = async ({
- request
-}: {
- request: Request;
-}) => {
- const { email, password } = Object.fromEntries(await request.formData());
- const response = new Response();
+export const action: ActionFunction = async ({ request }) => {
+ const response = new Response()
- const supabaseClient = createServerClient(
- process.env.SUPABASE_URL!,
- process.env.SUPABASE_ANON_KEY!,
+ const supabase = 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)
- });
+ const { data } = await supabase.from('test').select('*')
- // in order for the set-cookie header to be set,
- // headers must be returned as part of the loader response
return json(
- { data, error },
+ { data },
{
- headers: response.headers
+ headers: response.headers,
}
- );
-};
+ )
+}
```
</TabItem>
</Tabs>
-### Logout
+> Supabase will set cookie headers to manage the user's auth session, therefore, the `response.headers` must be returned from the `Action` function.
+
+## Session and User
+
+You can determine if a user is authenticated by checking their session using the `getSession` function.
+
+```jsx
+const {
+ data: { session },
+} = await supabase.auth.getSession()
+```
+
+The session contains a user property.
+
+```jsx
+const user = session?.user
+```
+
+## Authentication UI
+
+### Pre-built Auth UI
+
+We recommend using our [pre-built Auth UI](/docs/guides/auth/auth-helpers/auth-ui) for authenticating users. It supports custom themes and extensible styles to match your brand and aesthetic.
+
+### Custom Auth UI
+
+Should you prefer to build your own Auth UI, you can use our [JavaScript Library](/docs/reference/javascript/) to interact with Supabase Auth from the client-side.
<Tabs
+ groupId="language"
defaultValue="js"
values={[
{label: 'JavaScript', value: 'js'},
@@ -614,291 +508,399 @@ export const action: ActionFunction = async ({
]}>
<TabItem value="js">
-```jsx
-export const action = async ({ request }) => {
- const { email, password } = Object.fromEntries(await request.formData())
- const response = new Response()
+```jsx title="app/routes/signin.jsx"
+import { useOutletContext } from '@remix-run/react'
+
+export default function Auth() {
+ const { supabase, session } = useOutletContext()
+
+ const handleSubmit = async (event) => {
+ event.preventDefault()
+ if (!supabase) return
+ const formData = new FormData(event.currentTarget)
+ const email = formData.get('loginEmail')
+ const password = formData.get('loginPassword')
+ // Sign in
+ const { error } = await supabase.auth.signInWithPassword({
+ email,
+ password,
+ })
+ if (error) alert(error.message)
+ }
- const supabaseClient = createServerClient(
- process.env.SUPABASE_URL,
- process.env.SUPABASE_ANON_KEY,
- { request, response }
+ if (session)
+ return (
+ <div>
+ <pre>{JSON.stringify({ session }, null, 2)}</pre>
+ </div>
+ )
+
+ // Auth form
+ return (
+ <div>
+ <form onSubmit={handleSubmit}>
+ <h1>Custom login form:</h1>
+ <div>
+ <label htmlFor="loginEmail">Email:</label>
+ <input type="text" id="loginEmail" name="loginEmail" required />
+ </div>
+ <div>
+ <label htmlFor="loginPassword">Password:</label>
+ <input
+ type="password"
+ id="loginPassword"
+ name="loginPassword"
+ required
+ />
+ </div>
+ <button type="submit">Login</button>
+ </form>
+ </div>
)
+}
+```
- const { error } = await supabaseClient.auth.signOut()
+</TabItem>
+<TabItem value="ts">
- // in order for the set-cookie header to be set,
- // headers must be returned as part of the loader response
- return json(
- { error },
- {
- headers: response.headers,
- }
+```tsx title="app/routes/signin.tsx"
+import { useOutletContext } from '@remix-run/react'
+import type { ContextType } from '../root'
+
+export default function Auth() {
+ const { supabase, session } = useOutletContext<ContextType>()
+
+ const handleSubmit: React.FormEventHandler<HTMLFormElement> = async (
+ event
+ ) => {
+ event.preventDefault()
+ if (!supabase) return
+ const formData = new FormData(event.currentTarget)
+ const email = formData.get('loginEmail') as string
+ const password = formData.get('loginPassword') as string
+ // Sign in
+ const { error } = await supabase.auth.signInWithPassword({
+ email,
+ password,
+ })
+ if (error) alert(error.message)
+ }
+
+ if (session)
+ return (
+ <div>
+ <pre>{JSON.stringify({ session }, null, 2)}</pre>
+ </div>
+ )
+
+ // Auth form
+ return (
+ <div>
+ <form onSubmit={handleSubmit}>
+ <h1>Custom login form:</h1>
+ <div>
+ <label htmlFor="loginEmail">Email:</label>
+ <input type="text" id="loginEmail" name="loginEmail" required />
+ </div>
+ <div>
+ <label htmlFor="loginPassword">Password:</label>
+ <input
+ type="password"
+ id="loginPassword"
+ name="loginPassword"
+ required
+ />
+ </div>
+ <button type="submit">Login</button>
+ </form>
+ </div>
)
}
```
</TabItem>
+</Tabs>
-<TabItem value="ts">
+## Subscribe to realtime events
-```jsx
-export const action: ActionFunction = async ({
- request
-}: {
- request: Request;
-}) => {
- const { email, password } = Object.fromEntries(await request.formData());
- const response = new Response();
+[Supabase Realtime](/docs/guides/realtime) uses WebSockets to send database changes to the connected clients.
+
+We recommend loading the initial data server-side and then using the Supabase Realtime listener to update the client-side state as changes occur:
+
+<Tabs
+ groupId="language"
+ defaultValue="js"
+ values={[
+ {label: 'JavaScript', value: 'js'},
+ {label: 'TypeScript', value: 'ts'},
+ ]}>
+<TabItem value="js">
+
+```jsx title="app/routes/realtime.jsx"
+import { useEffect, useState } from 'react'
+import { json } from '@remix-run/node'
+import { useLoaderData } from '@remix-run/react'
+import { useOutletContext } from '@remix-run/react'
+import { createServerClient } from '@supabase/auth-helpers-remix'
+// Fetch the initial data server-side, then subscribe to updates client-side.
+export const loader = async ({ request }) => {
+ const response = new Response()
const supabaseClient = createServerClient(
- process.env.SUPABASE_URL!,
- process.env.SUPABASE_ANON_KEY!,
+ process.env.SUPABASE_URL,
+ process.env.SUPABASE_ANON_KEY,
{ request, response }
- );
+ )
+
+ const { data, error } = await supabaseClient.from('test').select('*')
- const { error } = await supabaseClient.auth.signOut();
+ if (error) {
+ throw error
+ }
// in order for the set-cookie header to be set,
// headers must be returned as part of the loader response
return json(
- { error },
+ { data },
{
- headers: response.headers
+ headers: response.headers,
}
- );
-};
-```
-
-</TabItem>
-</Tabs>
-
-## Subscribe to realtime events
-
-```jsx
-import { createBrowserClient } from '@supabase/auth-helpers-remix'
-import { useState, useEffect } from 'react'
+ )
+}
export default function SubscribeToRealtime() {
- const [data, setData] = useState([])
+ const { session, supabase } = useOutletContext()
+ const { data: serverLoadedData } = useLoaderData()
+ const [data, setData] = useState(serverLoadedData)
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])
- }
- )
- .subscribe()
-
- return () => {
- supabaseClient.removeChannel(channel)
+ if (supabase && session) {
+ // Subscribe to updates client-side
+ const channel = supabase
+ .channel('test')
+ .on(
+ 'postgres_changes',
+ { event: 'INSERT', schema: 'public', table: 'test' },
+ (payload) => {
+ setData((data) => [...data, payload.new])
+ }
+ )
+ .subscribe()
+
+ return () => {
+ supabase.removeChannel(channel)
+ }
}
- }, [session])
+ }, [session, supabase])
- return <pre>{JSON.stringify({ data }, null, 2)}</pre>
+ return <pre>{JSON.stringify(data, null, 2)}</pre>
}
```
-> Note: `window.env` is not automatically populated by Remix. Check out the "Client-side" instructions above to configure this.
-
-In this example we are listening to `INSERT` events on the `test` table. Anytime new rows are added to Supabase's `test` table, our UI will automatically update new data.
+</TabItem>
+<TabItem value="ts">
-## Merge server and client state on realtime events
+```tsx title="app/routes/realtime.tsx"
+import { useEffect, useState } from 'react'
+import { json, LoaderFunction } from '@remix-run/node'
+import { useLoaderData } from '@remix-run/react'
+import { useOutletContext } from '@remix-run/react'
+import { createServerClient } from '@supabase/auth-helpers-remix'
+import type { ContextType } from '../root'
+import { Database } from '../../db_types'
+type TestData = Database['public']['Tables']['test']['Row']
-```jsx
-import { json, LoaderFunction } from '@remix-run/node';
-import { useLoaderData, useNavigate } from '@remix-run/react';
-import {
- createServerClient,
- createBrowserClient
-} from '@supabase/auth-helpers-remix';
-import { useEffect } from 'react';
-import { Database } from '../../db_types';
-
-// this route demonstrates how to subscribe to realtime updates
-// and synchronize data between server and client
-export const loader: LoaderFunction = async ({
- request
-}: {
- request: Request;
-}) => {
- const response = new Response();
+// Fetch the initial data server-side, then subscribe to updates client-side.
+export const loader: LoaderFunction = async ({ request }) => {
+ const response = new Response()
const supabaseClient = createServerClient<Database>(
process.env.SUPABASE_URL!,
process.env.SUPABASE_ANON_KEY!,
{ request, response }
- );
-
- const {
- data: { session }
- } = await supabaseClient.auth.getSession();
+ )
- const { data, error } = await supabaseClient.from('test').select('*');
+ const { data, error } = await supabaseClient.from('test').select('*')
if (error) {
- throw error;
+ throw error
}
// in order for the set-cookie header to be set,
// headers must be returned as part of the loader response
return json(
- { data, session },
+ { data },
{
- headers: response.headers
+ headers: response.headers,
}
- );
-};
+ )
+}
export default function SubscribeToRealtime() {
- const { data, session } = useLoaderData();
- const navigate = useNavigate();
+ const { session, supabase } = useOutletContext<ContextType>()
+ const { data: serverLoadedData } = useLoaderData<{ data: TestData[] }>()
+ const [data, setData] = useState<TestData[]>(serverLoadedData)
useEffect(() => {
- // Note: window.env is not automatically populated by Remix
- // Check out the [example in this repo](../root.tsx) or
- // [Remix docs](https://remix.run/docs/en/v1/guides/envvars#browser-environment-variables) for more info
- const supabaseClient = createBrowserClient<Database>(
- window.env.SUPABASE_URL,
- window.env.SUPABASE_ANON_KEY
- );
- // make sure you have enabled `Replication` for your table to receive realtime events
- // https://supabase.com/docs/guides/database/replication
- const channel = supabaseClient
- .channel('test')
- .on(
- 'postgres_changes',
- { event: '*', schema: 'public', table: 'test' },
- (payload: any) => {
- // you could manually merge the `payload` with `data` here
- // the `navigate` trick below causes all active loaders to be called again
- // this handles inserts, updates and deletes, keeping everything in sync
- // which feels more remix-y than manually merging state
- // https://sergiodxa.com/articles/automatic-revalidation-in-remix
- navigate('.', { replace: true });
- }
- )
- .subscribe();
-
- return () => {
- supabaseClient.removeChannel(channel);
- };
- }, [session]);
+ if (supabase && session) {
+ // Subscribe to updates client-side
+ const channel = supabase
+ .channel('test')
+ .on(
+ 'postgres_changes',
+ { event: 'INSERT', schema: 'public', table: 'test' },
+ (payload) => {
+ setData((data) => [...data, payload.new as TestData])
+ }
+ )
+ .subscribe()
+
+ return () => {
+ supabase.removeChannel(channel)
+ }
+ }
+ }, [session, supabase])
- return (
- <div style={{ fontFamily: 'system-ui, sans-serif', lineHeight: '1.4' }}>
- <pre>{JSON.stringify({ data }, null, 2)}</pre>
- </div>
- );
+ return <pre>{JSON.stringify(data, null, 2)}</pre>
}
```
-> Note: `window.env` is not automatically populated by Remix. Check out the "Client-side" instructions above to configure this.
+</TabItem>
+</Tabs>
-## Usage with TypeScript
+## Server-side data fetching to OAuth APIs using `provider_token`
-You can pass types that were [generated with the Supabase CLI](/docs/reference/javascript/typescript-support#generating-types) to the `createServerClient` or `createBrowserClient` functions to get enhanced type safety and auto completion:
+When using third-party auth providers, sessions are initiated with an additional `provider_token` field which is persisted in the auth cookie and can be accessed within the session object. The `provider_token` can be used to make API requests to the OAuth provider's API endpoints on behalf of the logged-in user.
-### Server-side
+<Tabs
+ groupId="language"
+ defaultValue="js"
+ values={[
+ {label: 'JavaScript', value: 'js'},
+ {label: 'TypeScript', value: 'ts'},
+ ]}>
+<TabItem value="js">
-```ts
+```jsx
+import { json, redirect } from '@remix-run/node' // change this import to whatever runtime you are using
+import { useLoaderData } from '@remix-run/react'
import { createServerClient } from '@supabase/auth-helpers-remix'
-import { Database } from '../../db_types'
export const loader = async ({ request }) => {
const response = new Response()
- const supabaseClient = createServerClient<Database>(
+ const supabase = createServerClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY,
{ request, response }
)
-}
-```
-### Client-side
+ const {
+ data: { session },
+ } = await supabase.auth.getSession()
-```ts
-import { createBrowserClient } from '@supabase/auth-helpers-remix'
-import { Database } from '../../db_types'
+ if (!session) {
+ // there is no session, therefore, we are redirecting
+ // to the landing page. we still need to return
+ // response.headers to attach the set-cookie header
+ return redirect('/', {
+ headers: response.headers,
+ })
+ }
-const supabaseClient = createBrowserClient<Database>(
- process.env.SUPABASE_URL,
- process.env.SUPABASE_ANON_KEY
-)
-```
+ // Retrieve provider_token & logged in user's third-party id from metadata
+ const { provider_token, user } = session
+ const userId = user.user_metadata.user_name
-## Server-side data fetching to OAuth APIs using `provider_token`
+ const allRepos = await (
+ await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {
+ method: 'GET',
+ headers: {
+ Authorization: `token ${provider_token}`,
+ },
+ })
+ ).json()
-When using third-party auth providers, sessions are initiated with an additional `provider_token` field which is persisted in the auth cookie and can be accessed within the session object. 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 order for the set-cookie header to be set,
+ // headers must be returned as part of the loader response
+ return json(
+ { user, allRepos },
+ {
+ headers: response.headers,
+ }
+ )
+}
+
+export default function ProtectedPage() {
+ // by fetching the user in the loader, we ensure it is available
+ // for first SSR render - no flashing of incorrect state
+ const { user, allRepos } = useLoaderData()
-```js
-import { json, LoaderFunction, redirect } from '@remix-run/node'; // change this import to whatever runtime you are using
-import { useLoaderData } from '@remix-run/react';
-import { createServerClient, User } from '@supabase/auth-helpers-remix';
-import { Database } from '../../db_types';
+ return <pre>{JSON.stringify({ user, allRepos }, null, 2)}</pre>
+}
+```
-export const loader: LoaderFunction = async ({
- request
-}: {
- request: Request;
-}) => {
- const response = new Response();
+</TabItem>
+<TabItem value="ts">
- const supabaseClient = createServerClient<Database>(
+```tsx
+import { json, LoaderFunction, redirect } from '@remix-run/node' // change this import to whatever runtime you are using
+import { useLoaderData } from '@remix-run/react'
+import { createServerClient, User } from '@supabase/auth-helpers-remix'
+import { Database } from '../../db_types'
+
+export const loader: LoaderFunction = async ({ request }) => {
+ const response = new Response()
+
+ const supabase = createServerClient<Database>(
process.env.SUPABASE_URL!,
process.env.SUPABASE_ANON_KEY!,
{ request, response }
- );
+ )
const {
- data: { session }
- } = await supabaseClient.auth.getSession();
+ data: { session },
+ } = await supabase.auth.getSession()
if (!session) {
// there is no session, therefore, we are redirecting
// to the landing page. we still need to return
// response.headers to attach the set-cookie header
return redirect('/', {
- headers: response.headers
- });
+ headers: response.headers,
+ })
}
// Retrieve provider_token & logged in user's third-party id from metadata
- const { provider_token, user } = session;
- const userId = user.user_metadata.user_name;
+ const { provider_token, user } = session
+ const userId = user.user_metadata.user_name
const allRepos = await (
await fetch(`https://api.github.com/search/repositories?q=user:${userId}`, {
method: 'GET',
headers: {
- Authorization: `token ${provider_token}`
- }
+ Authorization: `token ${provider_token}`,
+ },
})
- ).json();
+ ).json()
// in order for the set-cookie header to be set,
// headers must be returned as part of the loader response
return json(
{ user, allRepos },
{
- headers: response.headers
+ headers: response.headers,
}
- );
-};
+ )
+}
export default function ProtectedPage() {
// by fetching the user in the loader, we ensure it is available
// for first SSR render - no flashing of incorrect state
- const { user, allRepos } = useLoaderData<{ user: User; allRepos: any }>();
+ const { user, allRepos } = useLoaderData<{ user: User; allRepos: any }>()
- return <pre>{JSON.stringify({ user, allRepos }, null, 2)}</pre>;
+ return <pre>{JSON.stringify({ user, allRepos }, null, 2)}</pre>
}
```
+
+</TabItem>
+</Tabs>