GithubHelp home page GithubHelp logo

m-shihata / react-auth-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 2.0 170 KB

Authentication-based app boilerplate

Home Page: https://react-auth-boilerplate-55acc.web.app/

License: MIT License

JavaScript 93.77% HTML 5.90% CSS 0.33%
firebase-hosting react react-router redux redux-toolkit

react-auth-boilerplate's Introduction

Work in progress... ๐Ÿšง

React Authentication Boilerplate

This repo contains an authentication-based app boilerplate that follows SOLID principles and design patterns.

The app boilerplate provides a clean and organized structure for building authentication-based applications using React. It includes features such as user registration, login, and logout, as well as password reset and email verification.

The code is written in an extensible way, making it easy to add new features or modify existing ones. It also includes comprehensive documentation and unit tests to ensure code quality and maintainability.

Usage:

npm install
npm start

Main Dependencies:

  • react-redux (with redux-tookit) for state management
  • react-router-dom for routing
  • react-hook-form (with yup) for form validation

Dev Dependencies:

  • prettier for code formatting
  • @trivago/prettier-plugin-sort-imports for sorting imports

Notes:

  • Used kebab case for all file names including components files for imports consistency.
  • Added jsconfig.json file to allow absolute imports from src folder.
  • .prettierrc file is included in the repo, but you can use your own and run npm run format to format the code.
  • Used tailwindcss for styling the app but feel free to use any other styling library.

Highlights:

AuthObserver

Saves time and effort by providing a centralized location for managing the authentication state, localStorage, and auth-based redirections of the app.

import useLocalStorage from 'hooks/use-local-storage';
import { useCallback, useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { login } from 'store/auth';

function AuthObserver({ children }) {
  const [isInit, setIsInit] = useState(true);

  const { user, token } = useSelector(state => state.auth);
  const [savedUser, setSavedUser] = useLocalStorage('user', null);
  const [savedToken, setSavedToken] = useLocalStorage('token', null);

  const dispatch = useDispatch();
  const dispatchLoginFromLocalStorage = useCallback(() => {
    dispatch(login({ user: savedUser, token: savedToken }));
  }, [dispatch, savedUser, savedToken]);

  const inStoreNotInLocal = !!(user && token && !savedUser && !savedToken);
  const inLocalNotInStore = !!(savedUser && savedToken && !user && !token);

  useEffect(() => {
    // Auto login from local storage
    if (inLocalNotInStore && isInit) {
      dispatchLoginFromLocalStorage();
      setIsInit(false);
    }
  }, [inLocalNotInStore, isInit, dispatchLoginFromLocalStorage, setIsInit]);

  useEffect(() => {
    // Save to local storage from store
    if (inStoreNotInLocal && isInit) {
      setSavedUser(user);
      setSavedToken(token);
      setIsInit(false);
    }
  }, [
    inStoreNotInLocal,
    isInit,
    setSavedUser,
    user,
    setSavedToken,
    token,
    setIsInit,
  ]);

  useEffect(() => {
    // Logout
    if (!isInit && !user && !token) {
      setSavedUser(null);
      setSavedToken(null);
      setIsInit(true);
    }
  }, [isInit, user, token, setSavedUser, setSavedToken, setIsInit]);

  return children;
}

export default AuthObserver;

react-auth-boilerplate's People

Contributors

m-shihata avatar

Stargazers

 avatar

Watchers

 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.