{"id":13692981,"url":"https://github.com/novum-insights/unlock-protocol-firebase","last_synced_at":"2025-05-02T21:30:56.496Z","repository":{"id":48056340,"uuid":"394315192","full_name":"novum-insights/unlock-protocol-firebase","owner":"novum-insights","description":"Lets Web3-enabled browsers authenticate with Firebase. Access roles are assigned based on the wallet's locks.","archived":false,"fork":false,"pushed_at":"2021-11-17T20:35:52.000Z","size":178,"stargazers_count":8,"open_issues_count":2,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-08-03T17:14:44.168Z","etag":null,"topics":["cyrptocurrency","firebase","web3"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/novum-insights.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-09T14:07:19.000Z","updated_at":"2022-10-03T04:05:52.000Z","dependencies_parsed_at":"2022-08-12T17:40:26.044Z","dependency_job_id":null,"html_url":"https://github.com/novum-insights/unlock-protocol-firebase","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/novum-insights%2Funlock-protocol-firebase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/novum-insights%2Funlock-protocol-firebase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/novum-insights%2Funlock-protocol-firebase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/novum-insights%2Funlock-protocol-firebase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/novum-insights","download_url":"https://codeload.github.com/novum-insights/unlock-protocol-firebase/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224337237,"owners_count":17294684,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cyrptocurrency","firebase","web3"],"created_at":"2024-08-02T17:01:04.134Z","updated_at":"2024-11-12T19:30:15.124Z","avatar_url":"https://github.com/novum-insights.png","language":"TypeScript","funding_links":[],"categories":["Integrations"],"sub_categories":[],"readme":"# README\n\n## Installation\n\nFunctions needed for cloud functions and in the browser are available via `npm` , browser capability can also be enabled via CDN for those not using a bundler.\n\n### Node.js\n```\n$ npm install @novuminsights/unlock-protocol-firebase\n```\n\n### CDN\n```markup\n\u003cscript type=\"text/javascript\"\n    src=\"https://unpkg.com/@novuminsights/unlock-protocol-firebase/lib/browser.js\"\u003e\u003c/script\u003e\n```\n\n## Authentication\n\nThis package allows wallet holders to log in to Firebase Authentication by verifying wallet ownership via signed message. No additional information is required. If you are using other authentication providers, I'd recommend merging user created in this way and using `AccountClaimReader` in `@novum/unlock-firebase-integration/server/helpers`  to maintain user claims in a custom manner. \n\n### Enabling Cloud Function authentication with for Wallet Users \n\nThat's all you need for the backend:\n\n```javascript\n// functions/src/index.js\n//initialize you app as you normally would\nimport * as admin from \"firebase-admin\";\nadmin.initializeApp();\n\n//expose getLockFirebaseToken and createMessageToken as you would any other function\nexport {getLockFirebaseToken, createMessageToken} from \"@novuminsights/unlock-protocol-firebase/lib/server\";\n```\n\nAdditionally, you need to set a secret:\n\n```bash\n$ firebase functions:config:set unlock.secret=\"Any String you can keep secret!\"\n```\n\nOn the front-end, just call `signInWithWeb3`. For example, using a button:\n\n#### Bundler\n```javascript\nimport \"firebase/auth\"\nimport \"firebase/functions\"\nimport {signInWithWeb3} from '@novum/unlock-firebase-integration/lib/browser'\n\nconst app = firebase.initializeApp(firebaseConfig);\nconst signInButton = document.querySelector('#MyButton')\nsignInButton.onclick = () =\u003e signInWithWeb3(app);\n```\n\n#### CDN\n```markup\n\u003cscript src=\"https://www.gstatic.com/firebasejs/8.8.1/firebase-app.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://www.gstatic.com/firebasejs/8.8.1/firebase-functions.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://www.gstatic.com/firebasejs/8.8.1/firebase-auth.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\"\n    src=\"https://unpkg.com/@novuminsights/unlock-protocol-firebase/lib/browser.js\"\u003e\u003c/script\u003e\n....\n\u003cscript\u003e\n    const app = firebase.initializeApp(firebaseConfig);\n    const signInButton = document.querySelector('#MyButton')\n    signInButton.onclick = async () =\u003e {\n        await unlockProtocolFirebaseBrowser.signInWithWeb3(app)\n    };\n\u003c/script\u003e\n```\n\n## Controlling Access Through Locks\n\nThis integration uses the Unlock Protocol locks held in a wallet to assign access roles to Firebase users via Firebase's custom claims \\(assessed on login\\). \n\n### Mapping locks to roles\n\nCustom claims are assigned to the user upon authentication based on the locks the wallet currently has. The mapping from lock addresses to claims is  defined in `unlock-integration-config.json`, like so:\n\n```javascript\n// functions/unlock-integration.config.json\n{\n    \"networks\": {\n        \"mainnet\": {\n            \"provider_url\": \"https://mainnet.infura.io/v3/678543fed855441b7b642730944ee4469\",\n            \"locks\": {\n                \"0x361Ddf540e27632D80dDE806EAa76AC42A0e15F6\": [\"basic_subscription\"]\n            }\n        },\n        \"rinkby\": {}\n    },\n  \"default_network\": \"mainnet\",\n  \"default_claims\": [\"wallet_owner\"]\n}\n```\n\nYou can define multiple networks, but currently only one network is used at a time. You can set which network will be used for a particular project via firebase functions config like so:\n\n```javascript\n$ firebase functions:config:set unlock.network=rinkby\n```\n\nOtherwise, `default_network` as defined in `unlock-integration-config.json` is used. This is useful for testing with Rinkby. Support for multiple active networks can be added in the future.\n\n### Using claims for role-based access\n\nYou can access these claims directly from the user's token --- for example, in your Firestore rules:\n\n```javascript\n// firestore.rules\nrules_version = '2';\nservice cloud.firestore {\n  match /databases/{database}/documents {\n      match /{document=**} {\n      allow read, write: if false;\n    }\n    function hasSubscription() {\n      return request.auth != null \u0026\u0026 request.auth.token.basic_subcriptionic_sub;\n    }\n    match /app_data/top_pairs {\n          allow read: if hasSubscription();\n    }\n}\n\n```\n\n As well as, your Firebase functions:\n\n```javascript\nasync function hasBasicSubscription(context) {\n    const auth = context.auth;\n    ...\n    return auth.token.basic_subscription === true;\n}\n```\n\nor in the browser:\n\n```javascript\nexport async function canViewDashboard(user) {\n    const result = await user.getIdTokenResult(true);\n    return (result.claims.basic_subscription === true);\n}\n```\n\n## Example Project\n\nAn example project can be found it `example/` . You will need to add your own project credentials as it uses Firebase Auth.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnovum-insights%2Funlock-protocol-firebase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnovum-insights%2Funlock-protocol-firebase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnovum-insights%2Funlock-protocol-firebase/lists"}