Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

React Native file upload with Supabase Storage

BackBlog post2023-08-01•21 minute readIf you want to Upload files from your React Native app, you need a backend to store the files, Supabase Storage is a great choice for this as it provides a simple API to upload files, and we can easily combine this with authentication to build a powerful app.This means you can quickly build your own image-sharing app, a file-sharing app, or any other app that needs to upload files to a backend!In this tutorial you will learn to:You can also directly check out the full source code on Github so you can get started with Supabase fast!Before we get into the app, let's quickly start a new Supabase project.To use authentication and storage we need a new Supabase project. If you don't have a Supabase account yet, you can get started for free!In your dashboard, click "New Project" and leave it to the default settings, but make sure you keep a copy o your Database password!After a minute your project should be ready, and we can configure the authentication and storage.Authentication will be enabled by default, but we want to turn off email confirmation for this tutorial.Select Authentication from the menu, go to the Providers section, and expand Email.Here you can disable the confirmation email, and apply other changes later if you want to!Now we want to create a bucket under storage where we will upload our files, and also add some security rules to protect the files of a user.First, select Storage from the menu, then click New bucket and call it files.Make sure that this is not a public bucket, otherwise, even unauthenticated users can upload or read the files!To protect that bucket and allow users only access to their own folder, we need to add some Storage policies.You can either do this through the UI and pick from examples, or simply run my SQL script in the SQL Editor which you can select from the menu:This will allow users to only access their own folder, and not any other files in the bucket.Now that we have our Supabase project ready, we can start building the React Native app!Get started by setting up a new Expo app with the tabs template and install some dependencies:We will use the Expo Secure Store to store the Supabase session, and the Expo Image Picker to select images from the device. We also need the Expo File System to read the image from the device and upload its data.You can now already run your project with npx expo and then select a platform to run on.However, the tabs template contains a lot of code that we don't need, so to simplify things we can remove the app, constants and components folder.This gives us a much cleaner project structure.To use Supabase we need to initialize the client with our project URL and the public key, which you can find in the Settings of your project under API.You can put both of them in a .env file at the root of your project:We can now simply read those values from the environment variables and initialize the Supabase client, so create a file at config/initSupabase.ts and add the following code:We are using the SecureStore from Expo to handle the session of our Supabase client and add in the createClient function.Later we can import the supabase client from this file and use it in our app whenever we need to access Supabase.Currently, the app won't work as we have no entry point. Because we are using the Expon Router and file-based routing, we can create a new file at app/index.tsx which will be the first page that comes up in our app.On this page we will handle both login and registration, so let's start by creating a simple form with a few inputs and buttons inside the app/index.tsx file:There's nothing fancy going on here, but this is all we need to use Supabase Authentication in our app!You can try it out right now and create a new user account or sign in with an existing one and log the values to the console to see what's going on.However, we are not handling the authentication state yet so let's create a Context to listen to changes.We will wrap a Provider around our app, which will use the onAuthStateChange function from Supabase to listen to changes in the authentication state and accordingly set our state.For this, create a new file at provider/AuthProvider.tsx and add the following code:To use the context we can now wrap it around our app, and while we do this we can also take care of the navigation:In the topmost layout file we can check whether a user has an active session or not, and either directly sign the user into the inside area (that we will create soon) or automatically bring her back to the login screen.To make this work with the Expo Router we can create a file at app/_layout.tsx and add the following code:Whenever the initialized or session state changes, we check if the user is authenticated and redirect her to the correct page.This also means we don't have to worry about the authentication state in our pages anymore, we can just assume that the user is authenticated and use the useAuth hook to access the user and session data later on.Your app might show an error right now because the /list route doesn't exist yet, but we will create it in the next step.Now that we have the authentication set up, we can start working on the file upload.First, let's define another layout for this inside area so create a file at /app/(auth)/_layout.tsx and add the following code:This defines a simple stack navigation and adds a button to trigger the logout, so we can now also fully test the authentication flow.Next, we create the page for uploading and displaying all files of a user from Supabase Storage.You won't have any files to show yet, but loading the files of a user is as easy as calling list() on the storage bucket and passing the user id as the folder name.Additionally, we add a little FAB (floating action button) to trigger the file picker, so create a file at /app/(auth)/list.tsx and add the following code:This should give us a nice and clean UI.Now we can implement the image picker and upload the selected image to Supabase Storage.Using the image picker from Expo gives us a URI, which we can use to read the file from the file system and convert it to a base64 string.We can then use the upload() method from the storage client to upload the file to the storage bucket. Life can be easy.At this point, you should be able to upload files to Supabase Storage, and you can already see them in your UI (or log them to the console).To finally display them we will add a ScrollView component, which will render one item for every file of a user.Let's start by creating those component rows in another file, so create a components/ImageItem.tsx file and add the following code:This component will display the image from Supabase Storage, the name of the file and a delete button.To display the image we use the download() method from the storage client, which returns a FileObject with the file data. We can then use the FileReader to convert the file data to a base64 string, which we can use as the image source.Now let's use this component in our list.tsx file to render the list of images by updating the return statement:Don't forget to also include the import to the ImageItem component!Finally, we can also include the delete functionality by adding the following code to the list.tsx:We are handling the deletion here so we can accordingly update the state of the files list after removing an item.And with that in place, you have a fully functional image gallery app with React Native and Supabase Storage!Initially, I wanted to include resumable uploads in this tutorial, but apparently, the Uppy client didn't work 100% for React Native yet.You can still see an initial implementation of resumable downloads with Supabase and React Native in the repository of this tutorial.However, ultimately the uploaded file was always 0 bytes, so I decided to leave it out for now.The Supabas team is investigating this issue, so I'm very sure that we will have resumable uploads working with React Native soon.It's almost too easy to use Supabase Storage, and it's a great way to store files for your apps.You now have a fully functional image gallery app with React Native and Supabase Storage including user authentication without writing a line of backend code!You can find the full code of this tutorial on Github where you just need to insert your own Supabase URL and API key.If you enjoyed the tutorial, you can learn React Native on Galaxies.dev where I help developers build awesome React Native apps.Until next time and happy coding with Supabase!Next post25 July 2023On this pageRelated articlesReact Native file upload with Supabase StorageSupabase Launch Week 8 HackathonGetting started with Flutter authenticationpgvector 0.4.0 performanceWhat is new in PostgREST v11.1?



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

React Native file upload with Supabase Storage

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×