Use Supabase Auth with React Native
Learn how to use Supabase Auth with React Native
Create a new Supabase project
Launch a new project 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.
_10 select * from auth.users;
Create a React app
Create a React app using the create-expo-app
command.
_10npx create-expo-app -t expo-template-blank-typescript my-app
Install the Supabase client library
Install supabase-js
and the required dependencies.
_10cd my-app && npx expo install @supabase/supabase-js @react-native-async-storage/async-storage @rneui/themed react-native-url-polyfill
_29import { AppState } from 'react-native'
_29import 'react-native-url-polyfill/auto'
_29import AsyncStorage from '@react-native-async-storage/async-storage'
_29import { createClient } from '@supabase/supabase-js'
_29const supabaseUrl = YOUR_REACT_NATIVE_SUPABASE_URL
_29const supabaseAnonKey = YOUR_REACT_NATIVE_SUPABASE_ANON_KEY
_29export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
_29 storage: AsyncStorage,
_29 autoRefreshToken: true,
_29 persistSession: true,
_29 detectSessionInUrl: false,
_29// Tells Supabase Auth to continuously refresh the session automatically
_29// if the app is in the foreground. When this is added, you will continue
_29// to receive `onAuthStateChange` events with the `TOKEN_REFRESHED` or
_29// `SIGNED_OUT` event if the user's session is terminated. This should
_29// only be registered once.
_29AppState.addEventListener('change', (state) => {
_29 if (state === 'active') {
_29 supabase.auth.startAutoRefresh()
_29 supabase.auth.stopAutoRefresh()
Create a login component
Let's set up a React Native component to manage logins and sign ups.
_83import React, { useState } from 'react'
_83import { Alert, StyleSheet, View } from 'react-native'
_83import { supabase } from '../lib/supabase'
_83import { Button, Input } from '@rneui/themed'
_83export default function Auth() {
_83 const [email, setEmail] = useState('')
_83 const [password, setPassword] = useState('')
_83 const [loading, setLoading] = useState(false)
_83 async function signInWithEmail() {
_83 const { error } = await supabase.auth.signInWithPassword({
_83 if (error) Alert.alert(error.message)
_83 async function signUpWithEmail() {
_83 } = await supabase.auth.signUp({
_83 if (error) Alert.alert(error.message)
_83 if (!session) Alert.alert('Please check your inbox for email verification!')
_83 <View style={styles.container}>
_83 <View style={[styles.verticallySpaced, styles.mt20]}>
_83 leftIcon={{ type: 'font-awesome', name: 'envelope' }}
_83 onChangeText={(text) => setEmail(text)}
_83 placeholder="email@address.com"
_83 autoCapitalize={'none'}
_83 <View style={styles.verticallySpaced}>
_83 leftIcon={{ type: 'font-awesome', name: 'lock' }}
_83 onChangeText={(text) => setPassword(text)}
_83 secureTextEntry={true}
_83 placeholder="Password"
_83 autoCapitalize={'none'}
_83 <View style={[styles.verticallySpaced, styles.mt20]}>
_83 <Button title="Sign in" disabled={loading} onPress={() => signInWithEmail()} />
_83 <View style={styles.verticallySpaced}>
_83 <Button title="Sign up" disabled={loading} onPress={() => signUpWithEmail()} />
_83const styles = StyleSheet.create({
_83 alignSelf: 'stretch',
Add the Auth component to your app
Add the Auth
component to your App.tsx
file. If the user is logged in, print the user id to the screen.
_27import 'react-native-url-polyfill/auto'
_27import { useState, useEffect } from 'react'
_27import { supabase } from './lib/supabase'
_27import Auth from './components/Auth'
_27import { View, Text } from 'react-native'
_27import { Session } from '@supabase/supabase-js'
_27export default function App() {
_27 const [session, setSession] = useState<Session | null>(null)
_27 supabase.auth.getSession().then(({ data: { session } }) => {
_27 supabase.auth.onAuthStateChange((_event, session) => {
_27 {session && session.user && <Text>{session.user.id}</Text>}
Start the app
Start the app, and follow the instructions in the terminal.