Coder Social home page Coder Social logo

terkea / firebase_react_auth Goto Github PK

View Code? Open in Web Editor NEW
0.0 4.0 0.0 1.08 MB

implementation of the firebase ecosystem in react with antd

Home Page: https://fir-react-auth-35f83.firebaseapp.com/

License: GNU General Public License v2.0

HTML 3.10% CSS 0.03% JavaScript 96.29% Dockerfile 0.57%
react firebase reactjs authentication boilerplate-template boilerplate firebase-database firebase-authentication firebase-storage

firebase_react_auth's Introduction

DEMO: https://fir-react-auth-35f83.firebaseapp.com/

Motivation

When starting a new project it can be incredibly time consuming to reach the milestone that will finally let you start implementing your ideas, and doing actual work.

The goal of this project is to be just that, a solid modern project template that you can easily pick up on and not worry too much about the boilerplate.

Installation

Option 1

> Docker

To install it with docker, you only need to run the following command:

dev port 3000

docker-compose up -d --build

prod port 80

docker-compose -f docker-compose.prod.yaml up -d --build

Option 2

> React

# Install the node packages
npm install
# Start the App in Development Mode
npm start

Configuration

Replace the environment variables from .env and enable authentication with email and password.

bucket rules

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

database rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

Documentation

Introduction

This documentation will assume a fair knowledge of React as well as some of the tools that we are used, including:

Routes

The App.js component (App.js) is being used as the main router and it is here that the main routes are defined. If you want to define other routes in your sub-components, you should not use <Router> again.

Customization - Antd Theming

antd theming ANTD is using Less as the development language for styling. A set of less variables are defined for each design aspect that can be customized to your needs. There are some major variables below, all less variables could be found in Default Variables.

Your custom changes should go in craco.config.js.

For all of the potential customizations don't hesitate to check their documentation which covers them all.

Layout Component

Layout Component (Layout.js)

In this template the layout component is to be used to display the main content of the webpage.

Navbar

To add more links you can just add more Menu.Item under Menu and make sure that the Link to is the same as the Menu.Item key without the / at the end, otherwise it won't show as selected, you can look at how this was done for the existing ones.

Tip: make sure that the Menu.Item is not nested in any other component and that it goes right under Menu.

Sessions

The session is managed by the Redux store.

When the page is loaded, an action actions.autoLogIn() is dispatched, from App.js, to the store actions which just checks for the existence of a token in localStorage and if there is no token in localStorage it logs the user out, otherwise it checks the token's validity and dispatches some other actions check autoLogIn() to see the code.

When the user is not logged in the store looks the following:

user store

When the user is logged in the store looks the following:

user store

Note: To visualize the above you need to get the Redux DevTools Extension.

  • user
    • loading - is a boolean that can used, for example, in the conditional rendering of elements, this is used in Login and My Profile.
    • error - is a boolen, that can be used, for example, to set a given element to show that it errored out, such as an icon.
    • email - note that email is moved inside profile once the user has logged in.

Custom user profile

By default when a user is registered a document is created with his uid. You can add as many custom attributes as you wish. user.js

try {
  firestore.collection("userProfile").doc(data.docId).set(
    {
      // CUSTOM PROFILE
      bio: data.bio,
    },
    { merge: true }
  );
} catch {}

My Profile

My Profile (MyProfile/index.js)

More options will be added in the future, these existing ones are just a proof of concept or example.

Notifications

Notifications (notificationHelpers.js)

The current notification system is based on the Ant Design notifications and it is implemented as a callback for actions that are dispatched to the store, this callback is optional but it is currently the way to run a notification when the axios request returns its promise.

A good example of how to use these notifications can be seen in Basic.js.

Login.js

import { runNotifications } from "../helpers/Notification";
// ...
// Inside the component:

const onFinishModals = (values) => {
  props.forgottenPassword(values.forgottenEmail, runNotifications);
};
// ...
const mapDispatchToProps = (dispatch) => {
  return {
    // map the callback just like a regular argument, in whatever action you want to dispatch
    signInUser: (email, password, callback) =>
      dispatch(actions.signInUser(email, password, callback)),
  };
};
// ...

user.js

// ...
// when you define your action make sure to define an empty anonymous function as the
// default function in case you don't want to call notifications on the given action
export const forgottenPassword = (email, notificationCallback) => (
  dispatch
) => {
  auth
    .sendPasswordResetEmail(email)
    .then((res) => {
      notificationCallback(
        `Thanks! Please check ${email} for a link to reset your password.`,
        "SUCCESS"
      );
    })
    .catch((error) => {
      notificationCallback(error.message, "ERROR");
    });
};
// ...

firebase_react_auth's People

Contributors

terkea avatar vadimstasiev avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.