Skip to content

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

docs: add info about React Native `AppState` and Supabase Auth (#20287)

Stojan Dimitrovskia564bd5bd44e
diff --git a/apps/docs/pages/guides/auth/quickstarts/react-native.mdx b/apps/docs/pages/guides/auth/quickstarts/react-native.mdx
index 3a70851569e..87ab5ddbff2 100644
--- a/apps/docs/pages/guides/auth/quickstarts/react-native.mdx
+++ b/apps/docs/pages/guides/auth/quickstarts/react-native.mdx
@@ -73,6 +73,7 @@ export const meta = {
```ts lib/supabase.ts
+ import { AppState } from 'react-native'
import 'react-native-url-polyfill/auto'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { createClient } from '@supabase/supabase-js'
@@ -88,6 +89,19 @@ export const meta = {
detectSessionInUrl: false,
},
})
+
+ // Tells Supabase Auth to continuously refresh the session automatically
+ // if the app is in the foreground. When this is added, you will continue
+ // to receive `onAuthStateChange` events with the `TOKEN_REFRESHED` or
+ // `SIGNED_OUT` event if the user's session is terminated. This should
+ // only be registered once.
+ AppState.addEventListener('change', (state) => {
+ if (state === 'active') {
+ supabase.auth.startAutoRefresh()
+ } else {
+ supabase.auth.stopAutoRefresh()
+ }
+ })
```
</StepHikeCompact.Code>
diff --git a/apps/docs/pages/guides/getting-started/tutorials/with-expo-react-native.mdx b/apps/docs/pages/guides/getting-started/tutorials/with-expo-react-native.mdx
index 33eacabd3b3..647a20c842b 100644
--- a/apps/docs/pages/guides/getting-started/tutorials/with-expo-react-native.mdx
+++ b/apps/docs/pages/guides/getting-started/tutorials/with-expo-react-native.mdx
@@ -169,10 +169,22 @@ Users would be able to sign in with their email and password.
```tsx components/Auth.tsx
import React, { useState } from 'react'
-import { Alert, StyleSheet, View } from 'react-native'
+import { Alert, StyleSheet, View, AppState } from 'react-native'
import { supabase } from '../lib/supabase'
import { Button, Input } from 'react-native-elements'
+// Tells Supabase Auth to continuously refresh the session automatically if
+// the app is in the foreground. When this is added, you will continue to receive
+// `onAuthStateChange` events with the `TOKEN_REFRESHED` or `SIGNED_OUT` event
+// if the user's session is terminated. This should only be registered once.
+AppState.addEventListener('change', (state) => {
+ if (state === 'active') {
+ supabase.auth.startAutoRefresh()
+ } else {
+ supabase.auth.stopAutoRefresh()
+ }
+})
+
export default function Auth() {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
diff --git a/apps/docs/spec/common-client-libs-sections.json b/apps/docs/spec/common-client-libs-sections.json
index 9067d846595..d89fd4cc159 100644
--- a/apps/docs/spec/common-client-libs-sections.json
+++ b/apps/docs/spec/common-client-libs-sections.json
@@ -612,6 +612,20 @@
"product": "auth",
"type": "function"
},
+ {
+ "id": "start-auto-refresh",
+ "title": "Start auto-refresh session (non-browser)",
+ "slug": "auth-startautorefresh",
+ "product": "auth",
+ "type": "function"
+ },
+ {
+ "id": "stop-auto-refresh",
+ "title": "Stop auto-refresh session (non-browser)",
+ "slug": "auth-stopautorefresh",
+ "product": "auth",
+ "type": "function"
+ },
{
"id": "auth-mfa-api",
"title": "Auth MFA",
diff --git a/apps/docs/spec/supabase_js_v2.yml b/apps/docs/spec/supabase_js_v2.yml
index 0e7c36782ce..b243d6eb19e 100644
--- a/apps/docs/spec/supabase_js_v2.yml
+++ b/apps/docs/spec/supabase_js_v2.yml
@@ -749,6 +749,56 @@ functions:
```js
const { data, error } = await supabase.auth.getSession()
```
+ - id: start-auto-refresh
+ title: 'startAutoRefresh()'
+ $ref: '@supabase/gotrue-js.GoTrueClient.startAutoRefresh'
+ notes: |
+ - Only useful in non-browser environments such as React Native or Electron.
+ - The Supabase Auth library automatically starts and stops proactively refreshing the session when a tab is focused or not.
+ - On non-browser platforms, such as mobile or desktop apps built with web technologies, the library is not able to effectively determine whether the application is _focused_ or not.
+ - To give this hint to the application, you should be calling this method when the app is in focus and calling `supabase.auth.stopAutoRefresh()` when it's out of focus.
+ examples:
+ - id: start-stop-auto-refresh-react-native
+ name: Start and stop auto refresh in React Native
+ isSpotlight: true
+ code: |
+ ```js
+ import { AppState } from 'react-native'
+
+ // make sure you register this only once!
+ AppState.addEventListener('change', (state) => {
+ if (state === 'active') {
+ supabase.auth.startAutoRefresh()
+ } else {
+ supabase.auth.stopAutoRefresh()
+ }
+ })
+ ```
+ - id: stop-auto-refresh
+ title: 'stopAutoRefresh()'
+ $ref: '@supabase/gotrue-js.GoTrueClient.stopAutoRefresh'
+ notes: |
+ - Only useful in non-browser environments such as React Native or Electron.
+ - The Supabase Auth library automatically starts and stops proactively refreshing the session when a tab is focused or not.
+ - On non-browser platforms, such as mobile or desktop apps built with web technologies, the library is not able to effectively determine whether the application is _focused_ or not.
+ - When your application goes in the background or out of focus, call this method to stop the proactive refreshing of the session.
+ examples:
+ - id: start-stop-auto-refresh-react-native
+ name: Start and stop auto refresh in React Native
+ isSpotlight: true
+ code: |
+ ```js
+ import { AppState } from 'react-native'
+
+ // make sure you register this only once!
+ AppState.addEventListener('change', (state) => {
+ if (state === 'active') {
+ supabase.auth.startAutoRefresh()
+ } else {
+ supabase.auth.stopAutoRefresh()
+ }
+ })
+ ```
- id: get-user
title: 'getUser()'
$ref: '@supabase/gotrue-js.GoTrueClient.getUser'