Building a PhotoShare App Using Auth0, Cloudinary, React.JS and Supabase.
Tags: auth0, cloudinary, react, supabase
Published: 2021-10-16 10:34:29

Photoshare is an application where users can login using Auth0 and share picture that can be viewed by other users using Cloudinary.

[Auth0](https://auth0.com/) is an easy to use authentication and authorization platform, it takes away the stress of authentication and authorization during the building process.

[Cloudinary](https://cloudinary.com/) is a service that makes life easy when it comes to working with images, you can upload images, resize images, crop  images and other cool stuffs without installing any complex software or going through heavy documentation.

[Supabase](https://supabase.io/) is a firebase alternative, it is very useful in setting up a backend service in few minutes.

**Perequisites Knowledge**


- React Js
- CSS

Lets Start Building


## Setup React

I am assuming we can setup react on our own. In the case when you are unable to set up react.js on your own, check out this tutorial by freecodecamp - [How to setup react js](https://www.freecodecamp.org/news/install-react-with-create-react-app/)

We need to flesh up our application to make it usable for the demo, i will drop some snippet, all you have to do is replace then in the appropriate files, i will explain where i need to.

index.html

{% gist
https://gist.github.com/Josh4324/2583459a0710bb141ea5170b515e83de
%}

The index.html file is inside the public folder.

Create a component folder inside your src folder, we will be creating few components.

main-nav.js
{% gist
https://gist.github.com/Josh4324/825c9ea663358a542386a5ca0281af98
%}

nav-bar.js
{% gist
https://gist.github.com/Josh4324/9ee5d91ad1c19de135628b049067fb86
%}

footer.js

{% gist
https://gist.github.com/Josh4324/f1a5a02b7bb9c6a28f8906afb85c0d07
%}

loading.js

{% gist
https://gist.github.com/Josh4324/345cbd5558572297ba866b5cca00934a
%}
index.js
{% gist
https://gist.github.com/Josh4324/73d03def32f9141a3480c57ccc2d63ff
%}

We are done with our components, now we need to create pages that will make use of the components.

Create a views folder inside the src folder.

The following pages will be inside the views folder

home.js

{% gist
https://gist.github.com/Josh4324/de303e15d1143a90be3bfc98e6c847a9
%}


profile.js

{% gist
https://gist.github.com/Josh4324/76a8f165ea3da15d49e9c2e48ac28081
%}


index.js
{% gist
https://gist.github.com/Josh4324/5481fc15636fcde792b545343fcd13e7
%}


We are done with the views folder for now. The only files remaining for us to fill up are index.js, app.js and app.css.

index.js
{% gist
https://gist.github.com/Josh4324/32a15577c4189eda05acc447d257bac2
%}

app.js
{% gist
https://gist.github.com/Josh4324/66ee6eca4e3852e344d86fcfc7a04c92
%}

app.css
{% gist
https://gist.github.com/Josh4324/2c923c83e9ca4f66b2aaa53bf6892dcd
%}


**Install Dependencies in react.**
@auth0/auth0-react,
@material-ui/core,
@material-ui/icons,
@supabase/supabase-js,
react-router-dom,
date-fns


## Add Auth0 to your React Application.

Sign up for a new account if you dont have one, once your signup is done Auth0 takes you to the dashboard, in the left side menu, click on applications.

On the applications page, click on the create application button.

You need to enter the name of the app and choose the application type.
You can use any name you want, we will select the single page web application, this is because we are using react.js.

When you are done, click on the Create button.

The next step is to add some urls on the application settings page.

Make sure you are on the settings page for the application, you just created.
The following fields needs to filled

- Allowed Callback URLs
- Allowed Logout URLs
- Allowed Web Origins

The base url of the application shoud be entered into the fields above i.e localhost:300 or appzone.com. Make sure you save the changes at the bottom of the page.

**Add the Auth0 configuration variables to React**

Create a .env inside the the src folder, populate the following fields

    REACT_APP_AUTH0_DOMAIN=
    REACT_APP_AUTH0_CLIENT_ID=

The values can be found on your Auth0 application settings page. 

The first one is the domain value from the settings.
The second one is the client value from the settings.

The react application can now be able to interact with the Auth0 authorization server.

**Set up the Auth0 React SDK**

The Auth0 react dependency has been installed - @auth0/auth0-react

We need to create a auth folder, where we would have all our authentication files.

We need to create a Auth0Provider file inside the auth folder to setup Auth0 for react.

src/auth/auth0-provider.js

{% gist
https://gist.github.com/Josh4324/93e0e146387f3feb830f7904eead14c1
%}


We need to integrate the Auth0 provider in the index.js, for that to happen, we need to edit out index.js inside the src folder.

src/index.js
{% gist
https://gist.github.com/Josh4324/04abd9433c74394677c0b3eff7205d65
%}


At this point, we can run npm start to start up the application to be sure everything is running fine.

Up next, we will start adding our Login, Signup and Logout button from Auth0.

We will create our login-button.js, signup-button.js and logout-button.js in the components folder.

login-button.js
{% gist
https://gist.github.com/Josh4324/eaf6773922935bf573857eecf7fe988b
%}


We made use of the useAuth0 hook, we got the loginWithRedirect from it, which is useful for our login button.

signup-button.js

{% gist
https://gist.github.com/Josh4324/1d90cfa77aab80ec21d0116b2d458a00
%}


logout-button.js

{% gist
https://gist.github.com/Josh4324/8fa12cc11b147cc6256692659d65a1e4
%}


We made use of the useAuth0 hook, we got the logout from it, which is useful for our logout button.

Up next, lets integrate our login and logout button, so that when we are logged in we see the logout button and when we are logged out, we see the login button.

authentication-button.js
{% gist
https://gist.github.com/Josh4324/97cae69c368f74bf91fa071c748571ab
%}

Here we got the isAuthenticated from our useAuth0 hook, this is needed to track when we are logged in or logged out.

Now, we need to create auth-nav.js which will contain our AuthenticationButton.

auth-nav.js
{% gist
https://gist.github.com/Josh4324/c2c9b6ef39d2404670dd764681e2c843
%}

To bring everything together, lets update the nav-bar.js

nav-bar.js
{% gist
https://gist.github.com/Josh4324/d1ebce543ca649b46196eddf7470acea
%}

At this point, we can test our application, you should be able to signup, login and logout using Auth0.

Upnext, we need to secure our routes and access some information from Auth0

We will create protected-route.js in the auth folder.

protected-route.js
{% gist
https://gist.github.com/Josh4324/f8127439d5f4eb28e8d82e578e98c0fe
%}

We can now protect all our routes in the app.js file.

app.js
{% gist
https://gist.github.com/Josh4324/a2eec76c0098653a127ae4c5e5545b7c
%}

At this point, we can test our application, you should not be able to access the home page and login page. it will redirect you to the an Auth0 login modal when you are not logged in.


## Setup Cloudinary

If you dont have a cloudinary account, signup on [cloudinary.com](https://cloudinary.com/)

First step, we need to add this script to the index.html in the public folder

<script src="https://upload-widget.cloudinary.com/global/all.js" type="text/javascript"></script> 

We need to create two function in the home.js file, we would make use of it inside the file.

{% gist
https://gist.github.com/Josh4324/9861abada01a9a04e3fdd7a7d002a2e3
%}

The cloudname can be gotten on cloudinary dashboard while the presetname can be gotten settings page, upload tab.

****
## SetUp Supabase

To create a supabase account, go to [supabase](https://app.supabase.io/) 

After signup is complete, click on new project

Chose the existing organisation.

Fill the create new project form.

Click on the create new project button to complete the form.

The setup process runs for a few minutes.

Once it is done, It will show you the project dashboard, you will see a card which is titled Database, click on the table editor in the card.

Click on create a new table.

Enter the table name and description.

You will also need to add columns to the table, two default columns are already added.

For the columns, you need to enter the name, type(i.e int) and the default value, you can also specify if you want the column to be the Primary Key.

What i choose for the Demo

Table name - Image
Columns (type)

- userId (varchar)
- image (text)
- likes (int)
- dislikes(int)
- desc(text)

Supabase is good to go and ready to be used.

**Intergrate Supabase with React**

We will create a client.js file in our src folder.

client.js
{% gist
https://gist.github.com/Josh4324/fd63526b8b6123bf6f6dff37bbcf211f
%}


To get these detail go the settings page of your supabase dashboard.

For the config_url, you will get it on the config card, the name of the card is config and the name of the detail you need is URL.

For the token, the name of the card is Project API keys and the name of the anon public.


## Finish Up App

Home.js

{% gist
https://gist.github.com/Josh4324/26b5f865fe36bb68e99d2153ae621775
%}


A couple of things is going on in this file but it basically brings together everything we have been working on.

Three important things to note.

1. we imported supabase from the client.js, this is what we would use to create post and get all the posts.
2. We are making use of formatDistanceToNow from fns date package to get relative date.
3. We are import useAuth0 form Auth0 package. This is where we get the user information such as name, unique id and picture.

We also added our two cloudinary powered functions, this is what is triggered when the image button is clicked and it pops up the cloudinary widget which we willl use to upload our image.

We also have two other functions powered by supabase, the first one is the create post, which is called when we submit our post, we also have the getAllImages function which triggers when the page reloads or when a post is created.

We also make use of useState to keep track of our states, useEffect to run functions when a page is reloaded and useRef to get data from elements.

We also obviously added some html and css to make it look a little bit nice.

I hope you have been able to learn a few things from the tutorial and the code snippets, in order to solidify your knowledge, you can complete the profile page, by displaying the user data and only the user’s posts on the page.

Thank You.

Link to the demo - https://jbwym.csb.app/

Content created for the [Hackmamba](https://content.hackmamba.io/) Jamstack Content Hackathon with [Auth0](https://auth0.com/) and [Cloudinary](https://cloudinary.com/).