Skip to content

Commit

Permalink
moralis auth firebase extension + demo.
Browse files Browse the repository at this point in the history
* moralis auth firebase extension + demo. poc.
  • Loading branch information
b4rtaz authored Sep 21, 2022
1 parent c4f9297 commit 67a9984
Show file tree
Hide file tree
Showing 39 changed files with 1,217 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"@moralisweb3/evm-utils",
"@moralisweb3/sol-utils",
"@moralisweb3/evm-api",
"@moralisweb3/sol-api"
"@moralisweb3/sol-api",
"@moralisweb3/client-firebase-auth"
]
],
"linked": [],
Expand Down
5 changes: 5 additions & 0 deletions .changeset/shaggy-pears-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/client-firebase-auth': patch
---

Add client package for Firebase authentication.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
id: cache
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C5
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C6
- name: Install packages
if: steps.cache.outputs.cache-hit != 'true'
run: yarn install
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
id: cache
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C5
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C6
- name: Install packages
if: steps.cache.outputs.cache-hit != 'true'
run: yarn install
Expand All @@ -55,7 +55,7 @@ jobs:
id: cache
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C5
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C6
- name: ESLint
run: yarn lint
continue-on-error: true
Expand Down Expand Up @@ -87,7 +87,7 @@ jobs:
id: cache
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C5
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C6
- name: Prettier
run: yarn format:check

Expand All @@ -111,7 +111,7 @@ jobs:
id: cache
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C5
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}_C6
- name: Build
run: yarn build:clean
- name: Test
Expand Down
5 changes: 5 additions & 0 deletions demos/firebase-auth-ext/.firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {},
"targets": {},
"etags": {}
}
5 changes: 5 additions & 0 deletions demos/firebase-auth-ext/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
lib/
.firebase/
*.env
*-debug.log
41 changes: 41 additions & 0 deletions demos/firebase-auth-ext/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Authenticate with Moralis Extension for Firebase Demo

This project contains a simple authentication demo for the Firebase by a Web3 wallet. It uses the Authenticate with Moralis Auth extension for the Firebase.

Required Google Cloud services:

- [Firebase Hosting](https://firebase.google.com/docs/hosting)
- [Firebase Authentication](https://firebase.google.com/docs/auth)
- [Firebase Cloud Functions](https://firebase.google.com/docs/functions) (only in the Blaze plan, check the [pricing](https://firebase.google.com/pricing)).
- [Secret Manager](https://cloud.google.com/secret-manager/) (check the [pricing](https://cloud.google.com/secret-manager/pricing))

## 🚀 How to Start

1. Clone this repo.
2. [Install Firebase CLI](https://firebase.google.com/docs/cli) globally: `npm install -g firebase-tools`
3. Login to your account: `firebase login`
4. Get list of your projects: `firebase projects:list`. If this list is empty you should add a new project. You can do it by the Firebase Console.
5. Set your project ID: `firebase use <PROJECT_ID>`
6. Generate a certificate for the [Service Account](https://firebase.google.com/support/guides/service-accounts). You will need it in the next step.
7. Convert the certificate to extension variables by [this online converter](https://moralisweb3.github.io/firebase-extensions/service-account-converter/). You will use these variables in the next step.
8. Install the Authenticate with Moralis Web3 extension: `firebase ext:install moralis/moralis-auth`.
9. Copy `hosting/.env.example` to `hosting/.env` and set all variables.
10. Build the functions project:
* `cd functions`
* `npm install`
* `npm run build`
11. Build the hosting project:
* `cd hosting`
* `npm install`
* `npm run build`

### 🔌 Run Locally

1. [Install Java](https://www.oracle.com/java/technologies/javase/jdk18-archive-downloads.html) on your computer. Functions Emulator requires Java.
2. Run emulators: `firebase emulators:start`
3. Open `http://localhost:5555/` in your browser.

### 🔥 Deploy to Production

1. Deploy: `firebase deploy`
2. If you have any problem with the CORS on the production, probably you should allow unauthenticated HTTP function invocation. To allow unauthenticated invocation you must specify this at or after deployment. [Here](https://cloud.google.com/functions/docs/securing/managing-access-iam#allowing_unauthenticated_http_function_invocation) you can read more about it.
25 changes: 25 additions & 0 deletions demos/firebase-auth-ext/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"functions": {
"ignore": ["node_modules"],
"source": "functions",
"predeploy": [
"yarn --cwd \"$RESOURCE_DIR\" install",
"yarn --cwd \"$RESOURCE_DIR\" run lint",
"yarn --cwd \"$RESOURCE_DIR\" run build"
]
},
"hosting": {
"ignore": ["node_modules"],
"public": "hosting/build",
"predeploy": [
"yarn --cwd \"$RESOURCE_DIR\" install",
"yarn --cwd \"$RESOURCE_DIR\" run lint",
"yarn --cwd \"$RESOURCE_DIR\" run build"
]
},
"emulators": {
"hosting": {
"port": 5555
}
}
}
4 changes: 4 additions & 0 deletions demos/firebase-auth-ext/functions/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
extends: ['@moralisweb3'],
ignorePatterns: ['**/lib/**/*'],
};
9 changes: 9 additions & 0 deletions demos/firebase-auth-ext/functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Compiled JavaScript files
lib/**/*.js
lib/**/*.js.map

# TypeScript v1 declaration files
typings/

# Node.js dependency directory
node_modules/
26 changes: 26 additions & 0 deletions demos/firebase-auth-ext/functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "demo-firebase-auth-ext-functions",
"version": "1.0.0",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"build:watch": "tsc --watch"
},
"engines": {
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-functions": "^3.18.0",
"firebase-admin": "^10.3.0"
},
"devDependencies": {
"@moralisweb3/eslint-config": "^1.0.0",
"typescript": "^4.5.4",
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "^5.36.2",
"eslint-plugin-etc": "^2.0.2",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
12 changes: 12 additions & 0 deletions demos/firebase-auth-ext/functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as functions from 'firebase-functions';

// ~/getSecretData

export const getSecretData = functions.https.onCall(async (data, context) => {
if (!context.auth?.uid) {
throw new functions.https.HttpsError('permission-denied', 'You are not authorized to call this function');
}

const secretValue = 'I am a secret';
return { secretValue };
});
3 changes: 3 additions & 0 deletions demos/firebase-auth-ext/functions/tsconfig.dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["./.eslintrc.js"]
}
16 changes: 16 additions & 0 deletions demos/firebase-auth-ext/functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017",
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true
},
"compileOnSave": true,
"include": ["src"]
}
11 changes: 11 additions & 0 deletions demos/firebase-auth-ext/hosting/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get the configuration of your Firebase app:
# open the Firebase Console > Project Settings > General > Your Apps and click Add app > Web app
# or
# execute `firebase apps:sdkconfig` command in the project directory

REACT_APP_FIREBASE_API_KEY=replace_me
REACT_APP_FIREBASE_AUTH_DOMAIN=replace_me
REACT_APP_FIREBASE_PROJECT_ID=replace_me
REACT_APP_FIREBASE_STORAGE_BUCKET=replace_me
REACT_APP_FIREBASE_MESSAGING_SENDER_ID=replace_me
REACT_APP_FIREBASE_APP_ID=replace_me
4 changes: 4 additions & 0 deletions demos/firebase-auth-ext/hosting/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
extends: ['@moralisweb3'],
ignorePatterns: ['**/lib/**/*'],
};
23 changes: 23 additions & 0 deletions demos/firebase-auth-ext/hosting/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
46 changes: 46 additions & 0 deletions demos/firebase-auth-ext/hosting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "demo-firebase-auth-ext-hosting",
"version": "1.0.0",
"private": true,
"dependencies": {
"@ethersproject/providers": "^5.7.1",
"@moralisweb3/client-firebase-auth": "^2.5.2",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/node": "^16.11.58",
"@types/react": "^18.0.19",
"@types/react-dom": "^18.0.6",
"bs58": "^5.0.0",
"firebase": "^9.9.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"typescript": "^4.8.3",
"web-vitals": "^2.1.4"
},
"scripts": {
"lint": "eslint --ext .js,.ts ./src",
"build": "react-scripts build",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
22 changes: 22 additions & 0 deletions demos/firebase-auth-ext/hosting/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Moralis Auth Demo</title>

<style>
body {
font: 14px/1.4em Arial, Verdana, Tahoma, Serif;
text-align: center;
}
button {
margin: 0 5px;
}
</style>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>

<div id="root"></div>
</body>
</html>
3 changes: 3 additions & 0 deletions demos/firebase-auth-ext/hosting/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
Loading

0 comments on commit 67a9984

Please sign in to comment.