Giter Club home page Giter Club logo

login-flow's Introduction

Login Flow

This application demonstrates what a React.js based register/login workflow might look like on the Frontend. I used my react-boilerplate as a starting point — the app thus uses Redux, PostCSS, react-router, ServiceWorker, AppCache, bcrypt and lots more.

The default username is AzureDiamond and the default password is hunter2, but feel free to register new users! The registered users are saved to localStorage, so they'll persist across page reloads.


Authentication

Everything authentication related is collected in the js/utils folder. The actual auth happens in auth.js, using fakeRequest.js and fakeServer.js.

fakeRequest is a fake XMLHttpRequest wrapper with a syntax similar to request.js. It simulates network latency too, so loading states are visible. fakeServer responds to the fake HTTP requests and pretends to be a real server, storing the current users in localStorage with the passwords encrypted using bcrypt.

To change it to real authentication, you’d only have to import request.js instead of fakeRequest.js and it should work! (Provided you have a server somewhere and the endpoints configured)

Features

  • Using react-hot-loader, your changes in the CSS and JS get reflected in the app instantly without refreshing the page. That means that the current application state persists even when you change something in the underlying code! For a very good explanation and demo watch Dan Abramov himself talking about it at react-europe.

  • Redux is a much better implementation of a flux–like, unidirectional data flow. Redux makes actions composable, reduces the boilerplate code and makes hot–reloading possible in the first place. For a good overview of redux check out the talk linked above or the official documentation!

  • PostCSS is like Sass, but modular and capable of much more. PostCSS is, in essence, just a wrapper for plugins which exposes an easy to use, but very powerful API. While it is possible to replicate Sass features with PostCSS, PostCSS has an ecosystem of amazing plugins with functionalities Sass cannot even dream about having.

  • react-router is used for routing in this application. react-router makes routing really easy to do and takes care of a lot of the work.

  • ServiceWorker and AppCache make it possible to use the application offline. As soon as the website has been opened once, it is cached and available without a network connection. manifest.json is specifically for Chrome on Android. Users can add the website to the homescreen and use it like a native app!

Getting started

  1. Clone this repo using git clone [email protected]:mxstbr/login-flow.

  2. Run npm install to install the dependencies.

  3. Run npm start to start the local web server.

  4. Go to http://localhost:3000 and you should see the app running!

CSS

The CSS modules found in the css subfolders all get imported into the main.css file, which get inlined and minified into the compiled.css file. To add/change the styling, either write the CSS into the appropriate module or make a new one and @import it in the main.css file at the appropriate place.

PostCSS Plugins

The boilerplate uses PostCSS, and includes a few plugins by default:

  • postcss-import: Inlines @imported stylesheets to create one big stylesheet.

  • postcss-simple-vars: Makes it possible to use `$variables in your CSS.

  • postcss-focus: Adds a :focus selector to every :hover.

  • autoprefixer-core: Prefixes your CSS automatically, supporting the last two versions of all major browsers and IE 8 and up.

  • cssnano: Optimizes your CSS file. For a full list of optimizations check the official website.

  • postcss-reporter: Makes warnings by the above plugins visible in the console.

For a full, searchable catalog of plugins go to postcss.parts.

Folder Structure

The boilerplate comes with a basic folder structure to keep the CSS files organised. This is what the folders are for:

  • base: Global styling, e.g. setting the box–model for all elements

  • components: Component specific styling, e.g. buttons, modals,...

  • layout: Global layouts, e.g. article, homepage,...

  • utils: Utility files, e.g. variables, mixins, functions,...

  • vendor: External files, e.g. a CSS reset

JS

All files that are imported/required somewhere get compiled into one big file at build time. (build/bundle.js) Webpack automatically optimizes your JavaScript with UglifyJS, so you do not have to worry about that.

Folder Structure

The folder structure of the JS files reflects how Redux works, so if you are not familiar with Redux check out the official documentation.

  • actions: Actions get dispatched with this/these utility module(s)

  • components: The main JS folder. All the React components are in this folder, with pages (routes) saved in the pages subfolder. E.g. a navigation component Nav.react.js

  • constants: Action constants are defined in this/these utility module(s)

  • reducers: Reducers manage the state of this app, basically a simplified implementation of Stores in Flux. For an introduction to reducers, watch this talk by @gaearon.

  • utils: Utility files.

Authentication

Authentication happens in js/utils/auth.js, using fakeRequest.js and fakeServer.js. fakeRequest is a fake XMLHttpRequest wrapper with a similar syntax to request.js which simulates network latency. fakeServer responds to the fake HTTP requests and pretends to be a real server, storing the current users in localStorage with the passwords encrypted using bcrypt. To change it to real authentication, you'd only have to import request.js instead of fakeRequest.js and have a server running somewhere.

Opinionated features

Web Fonts

If you simply use web fonts in your project, the page will stay blank until these fonts are downloaded. That means a lot of waiting time in which users could already read the content.

FontFaceObserver adds a js-<font-name>-loaded class to the body when the fonts have loaded. You should specify an initial font-family with save fonts, and a .js-<font-name>-loaded font-family which includes your web font.

Adding a new font

  1. Add the @font-face declaration to base/_fonts.css.

  2. In base/_base.css, specify your initial font-family in the body tag with only save fonts. In the body.js-<font-name>-loaded tag, specify your font-family stack with your web font.

  3. In js/app.js add a <font-name>Observer for your font.

Offline access

Using a ServiceWorker and the AppCache, the application is cached for offline usage. To cache a file, add it to the urlsToCache variable in the serviceworker.js file.

Once you run locally you will need to terminate the serviceworker when running another app in the same localhost port, to terminate the serviceworker visit chrome://inspect/#service-workers find the path to your localhost and terminate the worker. You might need to stop the worker if terminating wasn't enough chrome://serviceworker-internals then find the path to your localhost and stop/unregister.

Add To Homescreen

On Chrome for Android (soon hopefully more browsers), users can add a webpage to the homescreen. Combined with offline caching, this means the app can be used exactly like a native application.

Gotchas

These are some things to be aware of when using this boilerplate.

Images in the HTML file(s)

Adding images to the HTML is a bit of a pain right now as webpack only goes through the JavaScript file. Add the image to your HTML file how you always would:

<!-- Normal Image -->
<img src="img/yourimg.png" />
<!-- Meta tags -->
<meta property="og:image" content="img/yourimg.png" />
<!-- ... -->

If you simply do this, webpack will not transfer the images to the build folder. To get webpack to transfer them, you have to import them with the file loader in your JavaScript somewhere, e.g.:

import 'file?name=[name].[ext]!../img/yourimg.png';

Then webpack will correctly transfer the image to the build folder.

License

This project is licensed under the MIT license, Copyright (c) 2015 Maximilian Stoiber. For more information see LICENSE.md.

login-flow's People

Contributors

areiterer avatar carlmungazi avatar davidwells avatar geakstr avatar jdm29 avatar mxstbr avatar readmecritic avatar ryanflorence avatar weblancaster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

login-flow's Issues

PR: Add flow support

Using Flow along with react can provide for a great workflow. Many popular boilerplate projects, like create-react-app are now supporting it. Would a PR for adding it be welcome?

blank page

After npm start hitting localhost:3000 will render a blank page with Login Flow by Max Stoiber as title..
bummer 😞
Could you please create a login project using the generator of react-boilerplate ?
because the folders tree of this project is not the same as the former !
thanks

'notsup' error during npm install

I don't understand what the problem is, but here's the log after I try to run npm install immediately after cloning the repo:

Debug log: npm-debug.zip

> npm install
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue

> [email protected] postinstall /home/webserver/stuff/login-flow/node_modules/react-router
> node ./npm-scripts/postinstall.js

sh: 1: node: not found
[email protected] /home/webserver/stuff/login-flow

<-- removed a bunch of file/tree stuff --> #bandtank comment

npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: [email protected]
npm ERR! Linux 4.4.0-31-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! node v4.2.6
npm ERR! npm  v3.5.2
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn

npm ERR! [email protected] postinstall: `node ./npm-scripts/postinstall.js`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the [email protected] postinstall script 'node ./npm-scripts/postinstall.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the react-router package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./npm-scripts/postinstall.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs react-router
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls react-router
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/webserver/stuff/login-flow/npm-debug.log

I know this is shown above, but just in case:

webserver@PSEWS1:~/stuff/login-flow$ nodejs --version
v4.2.6

webserver@PSEWS1:~/stuff/login-flow$ npm --version
3.5.2

And finally:

webserver@PSEWS1:~/stuff/login-flow$ uname -a
Linux PSEWS1 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

webserver@PSEWS1:~/stuff/login-flow$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:    16.04
Codename:   xenial

I have no idea what to do. Any information would be helpful.

Edit: I thought of something rather important that seems to be one of the primary problems. I'm running this on a VMWare instance. The host OS is Windows Storage Server 2012 R2 64 bit and the guest OS is Ubuntu 16.04.1 LTS 64 bit as seen above.

Tutorial on how to replace fakeServer.js?

If I want to deploy to PaaS, e.g. Heroku, then how to do it?

Is there any tutorial/guides on how to connect this web app to server that store the registered users?

Thank you.

Social logins?

Hi Max! Coming here from react-boilerplate! I am wondering if you have previously had, or plan to add social login mock functions? If not, where do you suggest starting off incorporating Facebook, Twitter or any other OAuth provider in this setup?

Thank you!

Upgrade to latest react-boilerplate

Hi, I have downloaded both react boiler plate and this application.
I can't recognize the boilerplate in login-flow, is this code compatible with the new app folder design / boot ?

Perhaps it is not so long to upgrade this project to demonstrate the integration using react-boilerplate OR add this example directly in the sample project of react boilerplate (before the clean)

Remove some responsibilities from action

Hi :)

I think that would be really nice if keep the handling errors in the component itself and remove it from the actions https://github.com/mxstbr/login-flow/blob/master/js/actions/AppActions.js#L195

I'm working on a PR for that, and what I'd like to propose is something like:

  • Dispatch and action like 'invalid_login' or something. ( I'll think a better name )
  • Subscribe to reducer and update the component based on the reducer state(in this case 'invalid_login')

The main idea is remove the DOM manipulation from the action.

Does it make sense?

Indirect logout case

Once I logged in I am able to see dashboard and if i change the route to /login it redirects me to dashboard as you are already logged in and your token is present in cookie.

Now, instead of hitting logout say support I delete the token cookie or clean local storage and then after if I refresh the browser it should redirect me to login instead of dashboard.

Well, I am trying to do some thing as below, in my router file where i have defined my routes:

...
function shouldRedirectToLogin(nextState, replace) {
  console.log('nextState: ', nextState);

  if (!auth.isLoggedIn()) {
    replace({
      pathname: '/login',
      state: { nextPathname: nextState.location.pathname }
    })
  }
}
...

I am getting bellow error, I am using React, Redux and React-Router.

browser.js:49 Warning: You cannot use state without a queryKey it will be dropped

How to set a real URL?

Hi,

I may have missed this- seems a basic question. I would like to use this with an actual server. So how would i change the it so it passes the username/password to a URL instead? Thanks

Looks complicated?

I just want to grab a login react jsx form but this seems complicated. Could you indicate what is the minimum files needed for me to login (no registration form needed) as I will have just a single username and hashed password. Thanks

Localhost for other react apps is always redirecting to your homepage

I had forked this project and run some tests. When i try to 'npm start' other projects , i get the following:
`
Compiled successfully!

You can now view my-app in the browser.

Local: http://localhost:3000/
On Your Network: http://192.168.0.3:3000/

Note that the development build is not optimized.
To create a production build, use yarn build.
`

The first link which i use usually always directs to your login homepage.
The second link is directing towards my desired project.

Any suggestions for this?

Why not onEnter in the route path

Hi @mxstbr thanks for sharing this repo I got say was a big time consuming to find the best way to do a login flow so I'm sure this repo will be very useful for a lot of people.

In this case we can use onEnter which will remove the logic from component dashboard that way we keeping it more clean.

let me know your thoughts.

I can't find the request.js

Thank's bro for this! it's awesome.
Could you help me to correctly setup this for my purpose?

I wold like to make some changes:

  • the only page visible to non-logged-in user is /login
  • make a real HTTP req (with request.js missing file) store the session variable for calling the API
  • redirect to /dashboard

Pleaseee 👍

Remove client side bcrypt.

bcrypt.hash(password, salt, (err, hash) => {

I would modify this example to not hash the password on the client side (unless it is also being done on the server). Briefly, if you are using a secured communication protocol (SSL, for example), then there is no need to hash this here.

If anything, it's better to leave it on the server side - see this post for more info.

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.