Giter VIP home page Giter VIP logo

reactjsnodejsauthrouterv4's Introduction

ReactJSNodejsAuthRouterv4

ReactJS + Nodejs + React Router V4 user authentication, based on the code from tutorial: https://vladimirponomarev.com/blog/authentication-in-react-apps-jwt, and the routing/redirect idea comes from: https://reacttraining.com/react-router/web/example/auth-workflow

Comment 10/10/2017, 3:18pm:

Tested multi language(Eng/Chinese) setup, basically the idea is to store the current langugage setting with localStorage, and render header/main accordingly, same idea as how the user token is stored.

Spent a long time figuring out how to render the Main under the Header: with the app structure from yesterday, the problem is, when the langugage switch button, which is in the Header, is clicked, only the header will update the states and render in another langugage, and because there's no state update in Main, it can rely only on the change of its props, which are passed from the start page, and won't happen until the Main page is refreshed/reached again. Eventually the solution turns out to be quite simple: replace the "button" with "Link", and apply the onClick event to the Link, which will both update the Header states, and redirect to the Main page again, which will cause the Main page to be refreshed and rendered accordingly.

However the above solution feels a bit dodgy/lazy, since most of the multilingual websites usually handle different languages with different URLs, I think i'm doing it this way because of the influence of conditional rendering in ReactJS. May change to using different URLs such as /EN, /CH in the future if necessary.

Comment 09/10/2017, 9:00pm:

The original tutorial is not working with the latest versions of ReactJS, React-Router-Rom, webpack, and Material-UI. The version here is what I came up with after

Basically what have to be changed:

  1. The way the proptype is specified/imported;
  2. The top level element needs to be BrowserRouter instead of Router;
  3. The app needs a MuiThemeProvider so that the Mateiral-UI can work properly;
  4. Router can no longer do: this.context.router.replace, it will give an error saying replace does not exist. Instead use Redirect from React-Router-dom to do redirect after authentication succeed;
  5. The structure of the app has to be changed, all the page renderings are now conditional based on the user authentication state from Auth.js;
  6. Since this version is not using routes to define what happens when an URL is entered, onClick of Link on logout de-auth an user.

This version is tested on Amazon EC2 with a mongodb on mlab, and I will try build another version of it with mysql/msSQL if possible.

The package versions:

├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] └── [email protected]

reactjsnodejsauthrouterv4's People

Contributors

xbldev avatar

Watchers

James Cloos 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.