项目作者: epilande

项目描述 :
🔥 A Gatsby Theme for adding Firebase to your application.
高级语言: TypeScript
项目地址: git://github.com/epilande/gatsby-theme-firebase.git
创建时间: 2019-08-04T07:11:44Z
项目社区:https://github.com/epilande/gatsby-theme-firebase

开源协议:MIT License

下载




gatsby-theme-firebase 🔥



A Gatsby Theme for adding Firebase to your application.

GitHub
npm
Netlify Status

Why?

You want to add Firebase to your Gatsby application. You want a login page that “Just Works”. You want to make life easier with React Hooks and you want a solution that’s simple and extendable.

This Gatsby Theme gives you all of that and more! Take full advantage of Firebase + Gatsby without the hassle of setting it up!

What’s in the box?

DEMO

Installation

  1. $ npm install --save gatsby-theme-firebase

Usage

  1. // gatsby-config.js
  2. module.exports = {
  3. plugins: [
  4. {
  5. resolve: "gatsby-theme-firebase",
  6. options: {
  7. credentials: {
  8. apiKey: process.env.FIREBASE_API_KEY,
  9. authDomain: process.env.FIREBASE_AUTH_DOMAIN,
  10. databaseURL: process.env.FIREBASE_DATABASE_URL,
  11. projectId: process.env.FIREBASE_PROJECT_ID,
  12. storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
  13. messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
  14. appId: process.env.FIREBASE_APP_ID,
  15. },
  16. loginPath: "/login",
  17. loginRedirectPath: "/dashboard",
  18. socialLogins: ["google", "twitter", "facebook", "github"],
  19. },
  20. },
  21. ],
  22. };

Theme options

Key Default Required Description
credentials undefined true Configure Firebase credentials.
loginPath undefined false Set login page path. If undefined, no login page will be created.
loginRedirectPath / false On successful login, redirect to this path.
socialLogins [] false Enable social logins in the login form. e.g. ['google', 'twitter', 'facebook', 'github']

Just want the login form?

Don’t like the login page layout?

No problem! Don’t set the loginPath theme option (this will prevent the page from being created). Then use the <FormState.Provider ></FormState.Provider> and <Form ></Form> component and embed it in any page/layout.

  1. import { Form, FormState } from "gatsby-theme-firebase";
  2. const CustomLogin = () => (
  3. <Layout>
  4. <h1>Custom Login</h1>
  5. <FormState.Provider>
  6. <Form
  7. onLoginSuccess={user => {
  8. navigate("/profile");
  9. }}
  10. onSignUpSuccess={user => {
  11. saveUserToDatabase(user).then(() => {
  12. navigate("/welcome");
  13. });
  14. }}
  15. onResetSuccess={() => {
  16. setMessage("Email sent!");
  17. }}
  18. />
  19. </FormState.Provider>
  20. </Layout>
  21. );

To see an example, check out the login modal: demo site | demo/src/components/LoginModal.tsx

Just want the social login buttons?

Don’t need a full login form and only need social logins?

No problem! Use the <SocialLogins ></SocialLogins> component:

  1. import { SocialLogins } from "gatsby-theme-firebase";
  2. <SocialLogins
  3. onSuccess={user => {
  4. doSomethingWith(user);
  5. }}
  6. />;

To see an example, check out social logins: demo site | demo/src/pages/social-logins.tsx

Hooks

useAuth

  1. const { isLoading, isLoggedIn, profile } = useAuth();

Example:

  1. import { auth, useAuth } from "gatsby-theme-firebase";
  2. export default () => {
  3. const { isLoading, isLoggedIn, profile } = useAuth();
  4. return (
  5. <div>
  6. {isLoading && <p>Loading..</p>}
  7. {profile && <p>Hello {profile.displayName}</p>}
  8. {isLoggedIn && <button onClick={() => auth.signOut()}>Sign Out</button>}
  9. </div>
  10. );
  11. };

source: gatsby-theme-firebase/src/hooks/useAuth.ts

useFirestoreDoc

  1. const [data, isLoading, error] = useFirestoreDoc(docRef);

Example:

  1. import { firestore, useFirestoreDoc } from "gatsby-theme-firebase";
  2. export default () => {
  3. const [data, isLoading] = useFirestoreDoc(
  4. firestore.collection("tasks").doc("abc"),
  5. );
  6. return (
  7. <div>
  8. {isLoading && <p>Loading..</p>}
  9. {data && <div>{data.task}</div>}
  10. </div>
  11. );
  12. };

source: gatsby-theme-firebase/src/hooks/useFirestoreDoc.ts

useFirestoreQuery

  1. const [data, isLoading, error] = useFirestoreQuery(query);

Example:

  1. import { firestore, useFirestoreQuery } from "gatsby-theme-firebase";
  2. export default () => {
  3. const [tasks, isLoading] = useFirestoreQuery(
  4. firestore.collection("tasks").orderBy("priority", "asc"),
  5. );
  6. return (
  7. <div>
  8. {isLoading ? (
  9. <p>Loading...</p>
  10. ) : (
  11. <ol>
  12. {tasks.map(task => task && <li key={task._id}>{task.task}</li>)}
  13. </ol>
  14. )}
  15. </div>
  16. );
  17. };

source: gatsby-theme-firebase/src/hooks/useFirestoreQuery.ts

Shadowing

Gatsby Themes has a concept called Shadowing, which allow users to override a file in a gatsby theme.

To start shadowing, create a folder with the theme name gatsby-theme-firebase in your project’s src directory.

Now you’re able to override any file in the theme.

For example, if you want to override the handleSignUpSuccess function, create a file:

  1. src/gatsby-theme-firebase/firebase/auth/handleSignUpSuccess.js

Then do whatever you want in that file (i.e. save the user to the database).
Just make sure the return type is the same as the original, which in this case is a function.

  1. // Shadowing handleSignUpSuccess.js
  2. import { navigate } from "gatsby";
  3. export default async ({ user, loginRedirectPath, setErrorMessage }) => {
  4. try {
  5. await saveUserToDatabase(user);
  6. navigate(loginRedirectPath);
  7. } catch (error) {
  8. setErrorMessage(error.message);
  9. }
  10. };

Now the login page will pick up the shadowed file and use that handler instead of the default one.

Here’s a demo of handleLoginSuccess being shadowed: demo/src/gatsby-theme-firebase/firebase/auth/handleLoginSuccess.js

Demos

Dev

Set up env variables

Go to the demo application directory, copy the .env.example -> .env.development. Fill in the required environment variables before starting up the client dev server.

Quick start

This project uses yarn workspaces. Once you’ve set up the env variables, you can run the following to start the dev server.

  1. $ yarn && yarn dev

Available Scripts

$ yarn dev

This will run the demo app in development mode.

Navigate to http://localhost:8000 to view it in the browser.

$ yarn build

This will build the demo app for production.

Outputs to the demo/public folder.

Credits