Welcome back to Tutorial Tuesday!
In honor of Preptember, and in preparation for Hacktober, this week I'll show you how to create a serverless React app from scratch, add OAuth with IBM App ID and deploy to IBM Code Engine for FREE! I've included links to all the external resources, including the GitHub repo HERE. Happy Hacking!
IBM Cloud Serverless React App w App ID Google/Facebook OAuth
Create an IBM Cloud Pay-As-You-Go Account.
You will only be using the Free Lite Tier services.
1. Create an Instance of App ID
2. Create a React App
3. Add App ID to React App
4. Push Your Code to GitHub
5. Deploy a Serverless React App to IBM Code Engine
6. Login to the App w App ID OAuth
1. Create an Instance of App ID
Create an App ID Instance; Choose the Lite Tier Plan
From the menu on the right, select Applications.
Click Add Application.
Add a Name, and select Single-page application from the Type dropdown menu.
Expand and view the app credentials. Note the cliendID and the discoveryEndpoint. We'll need these later.
2. Create a React App
Create a React app below, clone this repo, or use the sample app.
Note: If it's been a while since you've used the create-react-app
command, it has been depricated. You will need to uninstall and re-rerun the command.
Uninstall create-react-app
command globally from both npm and yarn:
uninstall from npm
npm uninstall -g create-react-app
uninstall from yarn
yarn global remove create-react-app
Re-run the create-react-app
command to setup a frontend build pipeline; give your app a name:
npx create-react-app <APP_NAME>
npx create-react-app ibm-react-app
Move into your project directory:
cd <APP_NAME>
cd ibm-react-app
Add react-dotenv to the App
Load environment variables dynamically for your React applications created with CRA (Create-React-App).
Install the react-dotenv package:
npm install react-dotenv
Open your project's package.json file and: (already updated in the package.json)
- Add an .env file to your project root (or just load from the system environment variables).
- Add the react-dotenv NPM command to your start, build (and your serve commands).
- Add the react-dotenv.whitelist property to package.json to specify which variables you need exposed.
3. Add App ID to the App & Add App ID Credentials to .env
Install the IBM Cloud App ID SDK:
npm install ibmcloud-appid-js
In the /src
folder of the app, open App.js
in your text editor. (see sample app's App.js)
Import App ID by adding the following code:
import AppID from `ibmcloud-appid-js`;
In the main App() function, declare a new App ID instance.
Initialize App ID, and add error-handling. Add your cliendID and discoveryEndpoint, which can be found in the Applications tab, on the left of the App ID dashboard.
Create a login function that will execute after the login button is clicked.
After successfull authentication, the welcomeDisplayState
will be set to true, and the userName will be set to the name
value returned with the App ID token.
Add a welcome <div>
, a login <button>
that calls the login function(), and an error <div>
.
Start the application, and run it locally:
npm start
Update the redirect_uri in the App ID dashboard in the Authentication Settings under the Manage Authentication tab on the left.
DO NOT SKIP: Add web direct URLs
http://localhost:3000
View the Live Application
View your locally deployed application!
http://localhost:3000
4. Push Your Code to GitHub
Create a new GitHub repository; add a Repository name, and click Create repository.
Note: Initialize the repo with a name and an MIT license.
Clone the Starter-Kit repo, copy the ibm-react-app into a new directory, add the remote origin to the local initialized project, and set it upstream.
Push your code from the CLI to the new repository.
git remote add origin https://github.com/<PROFILE_NAME>/<REPO_NAME>.git
git branch -M main
git push -u origin main --allow-unrelated-histories
Now you're good to go!
5. Deploy a Serverless React App to IBM Code Engine
Login to Your IBM Cloud Account w the IBM Cloud CLI
Login to your IBM Cloud account.
ibmcloud login
View available resource groups.
ibmcloud resource groups
Assign a target resource group.
ibmcloud target -g Default
Update the Region to eu-gb.
ibmcloud target -r eu-gb
Create a Code Engine Project
Create a new Code Engine project and give it a name.
ibmcloud ce project create --name <NAME>
ibmcloud ce project create --name ufhacks
Verify you're in the newly created project.
ibmcloud ce project current
OPTIONAL: View your list of CE projects.
ibmcloud ce project list
OPTIONAL: Select the CE project you want to use.
ibmcloud ce project --name <NAME>
ibmcloud ce project --name ufhacks
Create a Code Engine Application from Source Code
Create a new Code Engine application from source code, and give it a name.
YOU CAN DO THIS FROM THE CODE ENGINE CONSOLE UI
OPTIONAL: Deploy the App to Code Engine from a Container Image
Deploy the app to CE from a container image. (by default, CE uses Dockerhub registries for repos containtining a Dockerfile unless specified otherwise)
ibmcloud ce application create --name ibm-react-app --image ibmcom/ibm-react-app
Get the app URL.
ibmcloud ce application get -n ibm-react-app -output url
View the Deployed Application
View your serverless app deployed to Code Engine!
<OUTPUT_URL>
DO NOT SKIP: Add the <OUTPUT_URL>
to package.json
Update the package.json react-dotenv.whitelist property URL to the Code Engine app URL.
"react-dotenv": {
"whitelist": ["<OUTPUT_URL>"]
}
DO NOT SKIP: Add the <OUTPUT_URL>
to App ID Redirect URIs
Update the redirect_uri in the App ID dashboard in the Authentication Settings under the Manage Authentication tab on the left.
Add web direct URL
<OUTPUT_URL>
6. Login to the App w App ID OAuth!
Login to your app with Facebook OAuth, Google OAuth, and IBM Cloud Directory!
<TEST_IT_OUT!>
NOW YOU'RE READY TO GET OUT THERE AND HACK TOGETHER SOMETHING AMAZING!
Connect w Me!
https://linktr.ee/jritten
Top comments (0)