|
| 1 | +# Authenticate with Firebase using Okta |
| 2 | + |
| 3 | +Sample app that demonstrates how to authenticate with Firebase using Okta. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This sample has two parts: |
| 8 | + |
| 9 | +- A Node.js backend that “exchanges” Okta access tokens for Firebase custom |
| 10 | + authentication tokens. The backend is intended to be deployed as a Cloud |
| 11 | + Function, but because its just an Express.js app, you can also run on it on |
| 12 | + your own infrastructure. |
| 13 | +- A web frontend that signs users in with Okta, gets a Firebase custom |
| 14 | + authentication token from your backend, and authenticates with Firebase using |
| 15 | + the custom token. |
| 16 | + |
| 17 | +## Setup |
| 18 | + |
| 19 | +Before you try the demo with the Firebase emulator suite or deploy it to |
| 20 | +Firebase Hosting and Cloud Functions, set up your Okta and Firebase projects, |
| 21 | +and install the Firebase CLI tool: |
| 22 | + |
| 23 | +1. On the [Okta Developer site](https://developer.okta.com/): |
| 24 | + |
| 25 | + 1. Sign in or sign up. |
| 26 | + 2. Take note of your **Org URL** (top-right of the dashboard) for later. |
| 27 | + 3. Create a user with a password in your Okta project. (This demo doesn't |
| 28 | + have a sign-up flow.) |
| 29 | + 4. On the Applications page, add a Single-Page App: |
| 30 | + 1. Set the **Base URIs** to `http://localhost:5000`. |
| 31 | + 2. Set the **Login redirect URIs** to `http://localhost:5000`. |
| 32 | + 3. Enable the **Authorization Code** grant type. |
| 33 | + 4. Click **Done**. Take note of the app's **Client ID** for later. |
| 34 | + 5. In **API > Trusted Origins**, confirm that `http://localhost:5000` is |
| 35 | + listed, with **CORS** and **Redirect** enabled. |
| 36 | + |
| 37 | +2. In the [Firebase console](https://console.firebase.google.com/): |
| 38 | + |
| 39 | + 1. Create a new Firebase project. Take note of your **project ID** for |
| 40 | + later. |
| 41 | + 2. On the Project Overview page, add a new web app. Be sure **Also set up |
| 42 | + Firebase Hosting for this app** is selected. |
| 43 | + 3. If you plan to try the demo in the emulator, [generate and download a |
| 44 | + service account key][svcacct]. |
| 45 | + |
| 46 | +3. If you don't already have a Node.js 10 (or newer) environment, |
| 47 | + [install Node.js](https://nodejs.org/). |
| 48 | + |
| 49 | +4. If you haven't already installed the Firebase CLI tool, do it now: |
| 50 | + |
| 51 | + ``` |
| 52 | + $ npm install --global firebase-tools |
| 53 | + ``` |
| 54 | +
|
| 55 | +[svcacct]: https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk |
| 56 | +
|
| 57 | +## Try the demo with the Firebase emulator suite |
| 58 | +
|
| 59 | +1. Make sure the Firebase CLI tool is set to use your Firebase project: |
| 60 | +
|
| 61 | + ``` |
| 62 | + $ cd functions-samples/okta-auth |
| 63 | + okta-auth$ firebase login |
| 64 | + okta-auth$ firebase use <YOUR_FIREBASE_PROJECT_ID> |
| 65 | + ``` |
| 66 | +
|
| 67 | +2. Run `setup.js` from the Firebase project directory: |
| 68 | +
|
| 69 | + ``` |
| 70 | + okta-auth$ node setup.js |
| 71 | + ``` |
| 72 | +
|
| 73 | + The script will prompt you for some of your Okta and Firebase configuration |
| 74 | + values and create configurations files from them. The script won't |
| 75 | + overwrite existing files. |
| 76 | +
|
| 77 | +3. Start the emulators: |
| 78 | +
|
| 79 | + ``` |
| 80 | + okta-auth$ firebase emulators:start |
| 81 | + ``` |
| 82 | +
|
| 83 | +4. Open the web app: [`http://localhost:5000`](http://localhost:5000). |
| 84 | +
|
| 85 | +## Deploy the demo to Firebase Hosting and Cloud Functions |
| 86 | +
|
| 87 | +1. [Upgrade your Firebase project to the Blaze (pay as you go) plan](https://console.firebase.google.com/project/_/overview?purchaseBillingPlan=metered). |
| 88 | + The Blaze plan is required to access external services (Okta) from Cloud |
| 89 | + Functions. |
| 90 | +
|
| 91 | +2. In the Google Cloud console: |
| 92 | +
|
| 93 | + 1. [Enable the IAM Service Account Credentials API](https://console.cloud.google.com/apis/api/iamcredentials.googleapis.com/overview?project=_). |
| 94 | + 2. On the [IAM](https://console.developers.google.com/iam-admin/iam?project=_) |
| 95 | + page, edit the account named **App Engine default service account** and |
| 96 | + add the **Service Account Token Creator** role. |
| 97 | +
|
| 98 | +3. Make sure the Firebase CLI tool is set to use your Firebase project: |
| 99 | +
|
| 100 | + ``` |
| 101 | + $ cd functions-samples/okta-auth |
| 102 | + okta-auth$ firebase login |
| 103 | + okta-auth$ firebase use <YOUR_FIREBASE_PROJECT_ID> |
| 104 | + ``` |
| 105 | +
|
| 106 | +4. Optional: If you have configuration files from local testing, delete them: |
| 107 | +
|
| 108 | + ``` |
| 109 | + okta-auth$ rm public/okta-config.js ; rm functions/.env ; rm .runtimeconfig.json |
| 110 | + ``` |
| 111 | +
|
| 112 | +5. Run `setup.js -d` from the Firebase project directory. The `-d` flag |
| 113 | + configures the web app and backend for deployment. |
| 114 | +
|
| 115 | + ``` |
| 116 | + okta-auth$ node setup.js -d |
| 117 | + ``` |
| 118 | +
|
| 119 | + The script will prompt you for some of your Okta and Firebase configuration |
| 120 | + values, create configurations files from them, and set some Cloud Funcions |
| 121 | + environment settings. The script won't overwrite existing files or Cloud |
| 122 | + Functions environment settings. |
| 123 | +
|
| 124 | +6. Deploy the project: |
| 125 | +
|
| 126 | + ``` |
| 127 | + okta-auth$ firebase deploy |
| 128 | + ``` |
| 129 | +
|
| 130 | +7. Open the web app at: `https://<YOUR_FIREBASE_PROJECT_ID>.web.app` |
0 commit comments