diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts index eec8e3802cc..345c055455b 100644 --- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts +++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts @@ -490,6 +490,7 @@ export const auth = { items: [ { name: 'Next.js', url: '/guides/auth/quickstarts/nextjs', items: [] }, { name: 'React', url: '/guides/auth/quickstarts/react', items: [] }, + { name: 'React Native', url: '/guides/auth/quickstarts/react-native', items: [] }, ], }, { diff --git a/apps/docs/pages/guides/auth/quickstarts/react-native.mdx b/apps/docs/pages/guides/auth/quickstarts/react-native.mdx new file mode 100644 index 00000000000..3a70851569e --- /dev/null +++ b/apps/docs/pages/guides/auth/quickstarts/react-native.mdx @@ -0,0 +1,260 @@ +import Layout from '~/layouts/DefaultGuideLayout' +import StepHikeCompact from '~/components/StepHikeCompact' + +export const meta = { + title: 'Use Supabase Auth with React Native', + subtitle: 'Learn how to use Supabase Auth with React Native', + breadcrumb: 'Auth Quickstarts', +} + + + + + + + [Launch a new project](https://supabase.com/dashboard) in the Supabase Dashboard. + + Your new database has a table for storing your users. You can see that this table is currently empty by running some SQL in the [SQL Editor](https://supabase.com/dashboard/project/_/sql). + + + + + + ```sql SQL_EDITOR + select * from auth.users; + ```` + + + + + + + + + + Create a React app using the `create-expo-app` command. + + + + + + ```bash Terminal + npx create-expo-app -t expo-template-blank-typescript my-app + ``` + + + + + + + + + Install `supabase-js` and the required dependencies. + + + + + + ```bash Terminal + cd my-app && npx expo install @supabase/supabase-js @react-native-async-storage/async-storage react-native-elements react-native-url-polyfill + ``` + + + + + + + + + Create a helper file `lib/supabase.ts` that exports a Supabase client using your [Project URL and public API (anon) key](https://supabase.com/dashboard/project/_/settings/api). + + + + + + ```ts lib/supabase.ts + import 'react-native-url-polyfill/auto' + import AsyncStorage from '@react-native-async-storage/async-storage' + import { createClient } from '@supabase/supabase-js' + + const supabaseUrl = YOUR_REACT_NATIVE_SUPABASE_URL + const supabaseAnonKey = YOUR_REACT_NATIVE_SUPABASE_ANON_KEY + + export const supabase = createClient(supabaseUrl, supabaseAnonKey, { + auth: { + storage: AsyncStorage, + autoRefreshToken: true, + persistSession: true, + detectSessionInUrl: false, + }, + }) + ``` + + + + + + + + + Let's set up a React Native component to manage logins and sign ups. + + + + + + ```tsx components/Auth.tsx + import React, { useState } from 'react' + import { Alert, StyleSheet, View } from 'react-native' + import { supabase } from '../lib/supabase' + import { Button, Input } from 'react-native-elements' + + export default function Auth() { + const [email, setEmail] = useState('') + const [password, setPassword] = useState('') + const [loading, setLoading] = useState(false) + + async function signInWithEmail() { + setLoading(true) + const { error } = await supabase.auth.signInWithPassword({ + email: email, + password: password, + }) + + if (error) Alert.alert(error.message) + setLoading(false) + } + + async function signUpWithEmail() { + setLoading(true) + const { + data: { session }, + error, + } = await supabase.auth.signUp({ + email: email, + password: password, + }) + + if (error) Alert.alert(error.message) + if (!session) Alert.alert('Please check your inbox for email verification!') + setLoading(false) + } + + return ( + + + setEmail(text)} + value={email} + placeholder="email@address.com" + autoCapitalize={'none'} + /> + + + setPassword(text)} + value={password} + secureTextEntry={true} + placeholder="Password" + autoCapitalize={'none'} + /> + + +