Code Monkey home page Code Monkey logo

authentication-react-lab-solutions's Introduction

Authentication in React Lab

Getting started

  • server

    • git clone https://github.com/thoughtworks-jumpstart/express_passport_mongoose_example.git
    • cd express_passport_mongoose_example
    • yarn install
    • mongod
    • yarn start (this will run on port 3000)
  • client

    • fork and clone repo
    • cd authentication-react-lab
    • yarn install
    • yarn start (this will run on port 3001)

Component tree

|__App
   |__BrowserRouter
      |__MyApp
         |__TopNavBar
         |__Route --> WelcomeComponent
         |__Route --> <AuthenticationForm title="sign up" />
         |__Route --> <AuthenticationForm title="sign in" />
         |__PrivateRoute
            |__ if isAuthenticated === true
               |___PrivilegedComponent
            |__ if isAuthenticated !== true
               |___<Redirect to="/" />

Key concepts

  • fetch docs

  • sessionStorage docs

    • sessionStorage.setItem("someKey", "someValue")
    • sessionStorage.getItem("someKey")
    • sessionStorage.removeItem("someKey")
  • authentication service - just a set of helper functions to tell you authentication-related stuff (e.g. is the user logged in?) (see utils/AuthService.js)

  • react-router-dom

    • how to redirect (when you're inside render()): <Redirect to="/somepath">
    • how to redirect (when you're outside render()): [this.]props.history.push("/somepath")
    • how to pass props from a Route to SomeComponent (using ...props) (see Routes inside MyApp.js) (this is useful when we need to access properties added by react-router, e.g. props.history, props.match, or props.location)
  • The practice of wrapping the component which requires the user to be logged in (e.g. PrivilegedComponent in this example) in a component which contains conditional logic - if user is logged in, display <PrivilegedComponent />, otherwise <Redirect to="/somewhere">

    • see PrivateRoute.js as an example

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.