Skip to content

Commit 5877294

Browse files
docs: update React Native auth quickstart to remove getUser (supabase#42196)
* Move old functions trouble shooting to new guides * Replace getUser, update, and switch to codeblocks * Revert "Move old functions trouble shooting to new guides" This reverts commit 229c581. * Prettier * Add env details * Fixes
1 parent dd0f832 commit 5877294

14 files changed

Lines changed: 8701 additions & 148 deletions

File tree

apps/docs/content/guides/auth/quickstarts/react-native.mdx

Lines changed: 17 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -66,48 +66,21 @@ hideToc: true
6666

6767
Create a helper file `lib/supabase.ts` that exports a Supabase client using your Project URL and key.
6868

69+
Create a `.env` file and populate with your Supabase connection variables:
70+
6971
<ProjectConfigVariables variable="url" />
7072
<ProjectConfigVariables variable="publishable" />
7173
<ProjectConfigVariables variable="anon" />
7274

7375
</StepHikeCompact.Details>
7476
<StepHikeCompact.Code>
7577

78+
<$CodeSample
79+
path="/auth/quickstarts/react-native/lib/supabase.ts"
80+
lines={[[1, -1]]}
81+
meta="name=lib/supabase.ts"
82+
/>
7683

77-
```ts name=lib/supabase.ts
78-
import { AppState, Platform } from 'react-native'
79-
import 'react-native-url-polyfill/auto'
80-
import AsyncStorage from '@react-native-async-storage/async-storage'
81-
import { createClient, processLock } from '@supabase/supabase-js'
82-
83-
const supabaseUrl = YOUR_REACT_NATIVE_SUPABASE_URL
84-
const supabaseAnonKey = YOUR_REACT_NATIVE_SUPABASE_PUBLISHABLE_KEY
85-
86-
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
87-
auth: {
88-
...(Platform.OS !== "web" ? { storage: AsyncStorage } : {}),
89-
autoRefreshToken: true,
90-
persistSession: true,
91-
detectSessionInUrl: false,
92-
lock: processLock,
93-
},
94-
})
95-
96-
// Tells Supabase Auth to continuously refresh the session automatically
97-
// if the app is in the foreground. When this is added, you will continue
98-
// to receive `onAuthStateChange` events with the `TOKEN_REFRESHED` or
99-
// `SIGNED_OUT` event if the user's session is terminated. This should
100-
// only be registered once.
101-
if (Platform.OS !== "web") {
102-
AppState.addEventListener('change', (state) => {
103-
if (state === 'active') {
104-
supabase.auth.startAutoRefresh()
105-
} else {
106-
supabase.auth.stopAutoRefresh()
107-
}
108-
})
109-
}
110-
```
11184
<$Partial path="api_settings_steps.mdx" variables={{ "framework": "exporeactnative", "tab": "mobiles" }} />
11285

11386
</StepHikeCompact.Code>
@@ -123,91 +96,11 @@ hideToc: true
12396
12497
<StepHikeCompact.Code>
12598
126-
```tsx name=components/Auth.tsx
127-
import React, { useState } from 'react'
128-
import { Alert, StyleSheet, View } from 'react-native'
129-
import { supabase } from '../lib/supabase'
130-
import { Button, Input } from '@rneui/themed'
131-
132-
export default function Auth() {
133-
const [email, setEmail] = useState('')
134-
const [password, setPassword] = useState('')
135-
const [loading, setLoading] = useState(false)
136-
137-
async function signInWithEmail() {
138-
setLoading(true)
139-
const { error } = await supabase.auth.signInWithPassword({
140-
email: email,
141-
password: password,
142-
})
143-
144-
if (error) Alert.alert(error.message)
145-
setLoading(false)
146-
}
147-
148-
async function signUpWithEmail() {
149-
setLoading(true)
150-
const {
151-
data: { session },
152-
error,
153-
} = await supabase.auth.signUp({
154-
email: email,
155-
password: password,
156-
})
157-
158-
if (error) Alert.alert(error.message)
159-
if (!session) Alert.alert('Please check your inbox for email verification!')
160-
setLoading(false)
161-
}
162-
163-
return (
164-
<View style={styles.container}>
165-
<View style={[styles.verticallySpaced, styles.mt20]}>
166-
<Input
167-
label="Email"
168-
leftIcon={{ type: 'font-awesome', name: 'envelope' }}
169-
onChangeText={(text) => setEmail(text)}
170-
value={email}
171-
placeholder="email@address.com"
172-
autoCapitalize={'none'}
173-
/>
174-
</View>
175-
<View style={styles.verticallySpaced}>
176-
<Input
177-
label="Password"
178-
leftIcon={{ type: 'font-awesome', name: 'lock' }}
179-
onChangeText={(text) => setPassword(text)}
180-
value={password}
181-
secureTextEntry={true}
182-
placeholder="Password"
183-
autoCapitalize={'none'}
184-
/>
185-
</View>
186-
<View style={[styles.verticallySpaced, styles.mt20]}>
187-
<Button title="Sign in" disabled={loading} onPress={() => signInWithEmail()} />
188-
</View>
189-
<View style={styles.verticallySpaced}>
190-
<Button title="Sign up" disabled={loading} onPress={() => signUpWithEmail()} />
191-
</View>
192-
</View>
193-
)
194-
}
195-
196-
const styles = StyleSheet.create({
197-
container: {
198-
marginTop: 40,
199-
padding: 12,
200-
},
201-
verticallySpaced: {
202-
paddingTop: 4,
203-
paddingBottom: 4,
204-
alignSelf: 'stretch',
205-
},
206-
mt20: {
207-
marginTop: 20,
208-
},
209-
})
210-
```
99+
<$CodeSample
100+
path="/auth/quickstarts/react-native/components/Auth.tsx"
101+
lines={[[1, -1]]}
102+
meta="name=components/Auth.tsx"
103+
/>
211104
212105
</StepHikeCompact.Code>
213106
@@ -222,35 +115,11 @@ hideToc: true
222115
223116
<StepHikeCompact.Code>
224117
225-
```tsx name=App.tsx
226-
import 'react-native-url-polyfill/auto'
227-
import { useState, useEffect } from 'react'
228-
import { supabase } from './lib/supabase'
229-
import Auth from './components/Auth'
230-
import { View, Text } from 'react-native'
231-
import { Session } from '@supabase/supabase-js'
232-
233-
export default function App() {
234-
const [session, setSession] = useState<Session | null>(null)
235-
236-
useEffect(() => {
237-
supabase.auth.getSession().then(({ data: { session } }) => {
238-
setSession(session)
239-
})
240-
241-
supabase.auth.onAuthStateChange((_event, session) => {
242-
setSession(session)
243-
})
244-
}, [])
245-
246-
return (
247-
<View>
248-
<Auth />
249-
{session && session.user && <Text>{session.user.id}</Text>}
250-
</View>
251-
)
252-
}
253-
```
118+
<$CodeSample
119+
path="/auth/quickstarts/react-native/App.tsx"
120+
lines={[[1, -1]]}
121+
meta="name=App.tsx"
122+
/>
254123
255124
</StepHikeCompact.Code>
256125
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files
2+
3+
# dependencies
4+
node_modules/
5+
6+
# Expo
7+
.expo/
8+
dist/
9+
web-build/
10+
11+
# Native
12+
*.orig.*
13+
*.jks
14+
*.p8
15+
*.p12
16+
*.key
17+
*.mobileprovision
18+
19+
# Metro
20+
.metro-health-check*
21+
22+
# debug
23+
npm-debug.*
24+
yarn-debug.*
25+
yarn-error.*
26+
27+
# macOS
28+
.DS_Store
29+
*.pem
30+
31+
# local env files
32+
.env*.local
33+
.env
34+
35+
# typescript
36+
*.tsbuildinfo
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'react-native-url-polyfill/auto'
2+
import { useState, useEffect } from 'react'
3+
import { supabase } from './lib/supabase'
4+
import Auth from './components/Auth'
5+
import { View, Text } from 'react-native'
6+
import { User } from '@supabase/supabase-js'
7+
8+
export default function App() {
9+
const [user, setUser] = useState<User | null>(null)
10+
11+
useEffect(() => {
12+
supabase.auth.getUser().then(({ data: { user } }) => {
13+
setUser(user)
14+
})
15+
16+
supabase.auth.onAuthStateChange((_event, session) => {
17+
setUser(session?.user ?? null)
18+
})
19+
}, [])
20+
21+
return (
22+
<View>
23+
<Auth />
24+
{user && <Text>{user.id}</Text>}
25+
</View>
26+
)
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Supabase Auth with React Native
2+
3+
This example demonstrates how to use Supabase Auth with React Native and Expo.
4+
5+
## Getting started
6+
7+
### 1. Create a Supabase project
8+
9+
[Launch a new project](https://supabase.com/dashboard) in the Supabase Dashboard.
10+
11+
### 2. Configure environment variables
12+
13+
Create a `.env` file and populate with your Supabase connection variables:
14+
15+
You can find these in your [Supabase Dashboard](https://supabase.com/dashboard/project/_/settings/api) under Settings > API.
16+
17+
### 3. Install dependencies
18+
19+
```bash
20+
npm install
21+
```
22+
23+
### 4. Start the app
24+
25+
```bash
26+
npm start
27+
```
28+
29+
Follow the instructions in the terminal to open the app on your device or emulator.
30+
31+
## Project structure
32+
33+
```
34+
├── App.tsx # Main app component
35+
├── components/
36+
│ └── Auth.tsx # Authentication form component
37+
├── lib/
38+
│ └── supabase.ts # Supabase client configuration
39+
├── app.json # Expo configuration
40+
├── package.json # Dependencies
41+
└── tsconfig.json # TypeScript configuration
42+
```
43+
44+
## Features
45+
46+
- Email/password sign up
47+
- Email/password sign in
48+
- Session persistence with AsyncStorage
49+
- Automatic token refresh
50+
51+
## Learn more
52+
53+
- [Supabase Auth Documentation](https://supabase.com/docs/guides/auth)
54+
- [React Native Quickstart](https://supabase.com/docs/guides/auth/quickstarts/react-native)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"expo": {
3+
"name": "Supabase Auth React Native",
4+
"slug": "supabase-auth-react-native",
5+
"version": "1.0.0",
6+
"orientation": "portrait",
7+
"icon": "./assets/icon.png",
8+
"userInterfaceStyle": "light",
9+
"splash": {
10+
"image": "./assets/splash.png",
11+
"resizeMode": "contain",
12+
"backgroundColor": "#ffffff"
13+
},
14+
"assetBundlePatterns": ["**/*"],
15+
"ios": {
16+
"supportsTablet": true
17+
},
18+
"android": {
19+
"adaptiveIcon": {
20+
"foregroundImage": "./assets/adaptive-icon.png",
21+
"backgroundColor": "#ffffff"
22+
}
23+
},
24+
"web": {
25+
"favicon": "./assets/favicon.png"
26+
}
27+
}
28+
}
17.1 KB
Loading
1.43 KB
Loading
21.9 KB
Loading
17.1 KB
Loading

0 commit comments

Comments
 (0)