Giter Club home page Giter Club logo

react-bootstrap-typescript-hooks's Introduction

Bootstrap + React in Typescript

Example of a real interface using react (hooks) for authentication / user state inside a bootstrap theeme with a responsive navbar. This serves as a

Auth / State Overview

Applications generally have three separate "auth-y" needs:

  • user data
  • user session
  • user requests

This application shows a flexable user authentication system that works for OAuth as well as plain user/password logins.

The idea is that on login (or successful OAuth completion) you pass 1) a token to the backend that will return a user object for the frontend. In addition, the request library (axios?) will probably need to send the token with each request (i.e. Authorization: Bearer ___).

If using regular username/password login then you can skip the TokenProvider.

Also make sure to have a CSRF protection setup which often involes a nonce value stored in a cookie that is sent back to the server as a request param or header.

Please look at https://github.com/Xeoncross/react-auth-providers for actual OAuth 2 implementations using hooks. Beside that are Firebase, Auth0, and site-specific SDK login libraries:

Login flow

  1. TokenProvider looks up token in useLocalStorage()
    • token found, continue
    • No existing VALID token found in localStorage
      • User completes OAuth (or HTTP Post of username/password)
      • Set TokenProvider token (and save to localStorage)
    • Server returns httpOnly session cookie + CSRF token over HTTPS
  2. token provided to RequestProvider so it can set it in the Authorization header.
  3. Request /me endpoint returns user object 3.1 on failure, erase localStorage token
  4. UserProvider.setUser() 4.1 components now have access to user data and all requests can use the token.

Bootstrap

Most websites use a responsive nav bar for mobile browsing. This theme uses the examples at https://getbootstrap.com/docs/4.4/examples/ as the starting point for a responsive base template.

react-bootstrap-typescript-hooks's People

Contributors

xeoncross avatar

Stargazers

 avatar  avatar

Watchers

 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.