diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..5282678 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,12 @@ +# These are supported funding model platforms + +github: rwieruch +patreon: # rwieruch +open_collective: # Replace with a single Open Collective username +ko_fi: # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +custom: # Replace with a single custom sponsorship URL diff --git a/.travis.yml b/.travis.yml index 1968f21..1d8352c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,10 @@ language: node_js node_js: - - stable + - '10' install: - npm install script: - - npm test \ No newline at end of file + - npm test diff --git a/README.md b/README.md index fa729e9..2ea9f3b 100644 --- a/README.md +++ b/README.md @@ -6,16 +6,20 @@ Your minimal yet extensive authentication starter project in Gatsby.js with Fire * Powered by [Gatsby](https://github.com/gatsbyjs/gatsby) * [Tutorial](https://www.robinwieruch.de/complete-firebase-authentication-react-tutorial/) -* [Live Version of half of the Tutorial](https://react-firebase-authentication.wieruch.com/) + +## Variations + +* [Only React Version](https://github.com/the-road-to-react-with-firebase/react-firebase-authentication) +* [Redux Version](https://github.com/the-road-to-react-with-firebase/react-redux-firebase-authentication) +* [MobX Version](https://github.com/the-road-to-react-with-firebase/react-mobx-firebase-authentication) +* [Firestore Version](https://github.com/the-road-to-react-with-firebase/react-firestore-authentication) +* [Semantic UI Version](https://github.com/the-road-to-react-with-firebase/react-semantic-ui-firebase-authentication) ## Features * uses: - * only React (Gatsby.js) - * firebase 5 - * no Redux/MobX - * [Redux Version without Gatsby.js](https://github.com/the-road-to-react-with-firebase/react-redux-firebase-authentication) - * [MobX Version without Gatsby.js](https://github.com/the-road-to-react-with-firebase/react-mobx-firebase-authentication) + * only React (**Gatsby.js**) + * firebase * features: * Sign In * Sign Up @@ -30,26 +34,116 @@ Your minimal yet extensive authentication starter project in Gatsby.js with Fire * Auth Persistence with Local Storage * Database with Users and Messages -## Setup +## License + +### Commercial license + +If you want to use this starter project to develop commercial sites, themes, projects, and applications, the Commercial license is the appropriate license. With this option, your source code is kept proprietary. Purchase an commercial license for different team sizes: + +* [1 Developer](https://gum.co/react-with-firebase-starter-pack-developer) +* [Team of up to 8 Developers](https://gum.co/react-with-firebase-starter-pack-team) +* [Unlimited Developers of an Organization](https://gum.co/react-with-firebase-starter-pack-organization) + +It grants you also access to the other starter projects in this GitHub organization. + +### Open source license -* `git clone git@github.com:the-road-to-react-with-firebase/react-gatsby-firebase-authentication.git` +If you are creating an open source application under a license compatible with the [GNU GPL license v3](https://www.gnu.org/licenses/gpl-3.0.html), you may use this starter project under the terms of the GPLv3. + +## Installation + +* `git clone https://github.com/the-road-to-react-with-firebase/react-gatsby-firebase-authentication.git` * `cd react-gatsby-firebase-authentication` -* `yarn install` -* `gatsby develop` -* visit http://localhost:8000/ -* Use your own Firebase Credentials +* `npm install` +* save the [Firebase credentials](#firebase-configuration) +* setup the [security rules](#security-rules) +* `npm start` +* visit http://localhost:8000 + +Get an overview of Firebase, how to create a project, what kind of features Firebase offers, and how to navigate through the Firebase project dashboard in this [visual tutorial for Firebase](https://www.robinwieruch.de/firebase-tutorial/). + +### Firebase Configuration + +* copy/paste your configuration from your Firebase project's dashboard into one of these files + * *src/components/Firebase/firebase.js* file + * *.env* file + * *.env.development* and *.env.production* files + +The *.env* or *.env.development* and *.env.production* files could look like the following then: + +``` +GATSBY_API_KEY=AIzaSyBtxZ3phPeXcsZsRTySIXa7n33NtQ +GATSBY_AUTH_DOMAIN=react-firebase-s2233d64f8.firebaseapp.com +GATSBY_DATABASE_URL=https://react-firebase-s2233d64f8.firebaseio.com +GATSBY_PROJECT_ID=react-firebase-s2233d64f8 +GATSBY_STORAGE_BUCKET=react-firebase-s2233d64f8.appspot.com +GATSBY_MESSAGING_SENDER_ID=701928454501 +``` + +### Activate Sign-In Methods + + + +* Email/Password +* [Google](https://www.robinwieruch.de/react-firebase-social-login/) +* [Facebook](https://www.robinwieruch.de/firebase-facebook-login/) +* [Twitter](https://www.robinwieruch.de/firebase-twitter-login/) +* [Troubleshoot](https://www.robinwieruch.de/react-firebase-social-login/) + +### Activate Verification E-Mail + +* add a redirect URL for redirecting a user after an email verification into one of these files + * *src/components/Firebase/firebase.js* file + * *.env* file + * *.env.development* and *.env.production* files + +The *.env* or *.env.development* and *.env.production* files could look like the following then (excl. the Firebase configuration). + +**Development:** + +``` +GATSBY_CONFIRMATION_EMAIL_REDIRECT=http://localhost:3000 +``` + +**Production:** + +``` +GATSBY_CONFIRMATION_EMAIL_REDIRECT=https://mydomain.com +``` + +### Security Rules -### Use your own Firebase Credentials +In the Firebase console, go to Database, select "Realtime Database" -> Rules, and paste the rules below: -* visit https://firebase.google.com/ and create a Firebase App -* copy and paste your Credentials from your Firebase App into src/firebase/firebase.js -* activate Email/Password Sign-In Method in your Firebase App +``` +{ + "rules": { + ".read": false, + ".write": false, + "users": { + "$uid": { + ".read": "$uid === auth.uid || root.child('users/'+auth.uid).child('roles').hasChildren(['ADMIN'])", + ".write": "$uid === auth.uid || root.child('users/'+auth.uid).child('roles').hasChildren(['ADMIN'])" + }, + ".read": "root.child('users/'+auth.uid).child('roles').hasChildren(['ADMIN'])", + ".write": "root.child('users/'+auth.uid).child('roles').hasChildren(['ADMIN'])" + }, + "messages": { + ".indexOn": ["createdAt"], + "$uid": { + ".write": "data.exists() ? data.child('userId').val() === auth.uid : newData.child('userId').val() === auth.uid" + }, + ".read": "auth != null", + ".write": "auth != null", + }, + } +} +``` ## Setup via Gatsby CLI -* `gatsby new react-gatsby-firebase-authentication git@github.com:the-road-to-react-with-firebase/react-gatsby-firebase-authentication.git` +* `gatsby new react-gatsby-firebase-authentication https://github.com/the-road-to-react-with-firebase/react-gatsby-firebase-authentication.git` * `cd react-gatsby-firebase-authentication` -* `yarn install` +* `npm install` * `gatsby develop` -* visit http://localhost:8000/ -* Use your own Firebase Credentials +* visit http://localhost:8000 diff --git a/gatsby-config.js b/gatsby-config.js new file mode 100644 index 0000000..6990517 --- /dev/null +++ b/gatsby-config.js @@ -0,0 +1,3 @@ +require("dotenv").config({ + path: `.env.${process.env.NODE_ENV}`, +}) diff --git a/package.json b/package.json index e383d13..cfa8979 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,11 @@ "serve": "gatsby serve" }, "dependencies": { - "firebase": "^5.6.0", - "gatsby": "^2.0.33", - "react": "^16.5.1", - "react-dom": "^16.5.1", - "recompose": "^0.30.0" + "firebase": "^6.0.2", + "gatsby": "^2.4.3", + "react": "^16.8.6", + "react-dom": "^16.8.6", + "recompose": "^0.30.0", + "core-js": "^2.6.5" } } diff --git a/src/components/Firebase/firebase.js b/src/components/Firebase/firebase.js index 872d6d9..c9affba 100644 --- a/src/components/Firebase/firebase.js +++ b/src/components/Firebase/firebase.js @@ -1,10 +1,10 @@ const config = { - apiKey: process.env.REACT_APP_API_KEY, - authDomain: process.env.REACT_APP_AUTH_DOMAIN, - databaseURL: process.env.REACT_APP_DATABASE_URL, - projectId: process.env.REACT_APP_PROJECT_ID, - storageBucket: process.env.REACT_APP_STORAGE_BUCKET, - messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID, + apiKey: process.env.GATSBY_API_KEY, + authDomain: process.env.GATSBY_AUTH_DOMAIN, + databaseURL: process.env.GATSBY_DATABASE_URL, + projectId: process.env.GATSBY_PROJECT_ID, + storageBucket: process.env.GATSBY_STORAGE_BUCKET, + messagingSenderId: process.env.GATSBY_MESSAGING_SENDER_ID, }; class Firebase { @@ -51,7 +51,7 @@ class Firebase { doSendEmailVerification = () => this.auth.currentUser.sendEmailVerification({ - url: process.env.REACT_APP_CONFIRMATION_EMAIL_REDIRECT, + url: process.env.GATSBY_CONFIRMATION_EMAIL_REDIRECT, }); doPasswordUpdate = password => @@ -69,7 +69,7 @@ class Firebase { // default empty roles if (!dbUser.roles) { - dbUser.roles = []; + dbUser.roles = {}; } // merge auth and db user diff --git a/src/components/Messages/MessageItem.js b/src/components/Messages/MessageItem.js index 78a9bb2..b92f954 100644 --- a/src/components/Messages/MessageItem.js +++ b/src/components/Messages/MessageItem.js @@ -28,7 +28,7 @@ class MessageItem extends Component { }; render() { - const { message, onRemoveMessage } = this.props; + const { authUser, message, onRemoveMessage } = this.props; const { editMode, editText } = this.state; return ( @@ -41,29 +41,31 @@ class MessageItem extends Component { /> ) : ( - - {message.user.username || message.user.userId} - {' '} - {message.text} {message.editedAt && (Edited)} + {message.userId} {message.text} + {message.editedAt && (Edited)} )} - {editMode ? ( + {authUser.uid === message.userId && ( - - - - ) : ( - - )} + {editMode ? ( + + + + + ) : ( + + )} - {!editMode && ( - + {!editMode && ( + + )} + )} ); diff --git a/src/components/Messages/MessageList.js b/src/components/Messages/MessageList.js index 4e0c49b..fb18585 100644 --- a/src/components/Messages/MessageList.js +++ b/src/components/Messages/MessageList.js @@ -3,6 +3,7 @@ import React from 'react'; import MessageItem from './MessageItem'; const MessageList = ({ + authUser, messages, onEditMessage, onRemoveMessage, @@ -10,6 +11,7 @@ const MessageList = ({