• Stars
    star
    1,610
  • Rank 29,064 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔑 A login/register flow built with React&Redux

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.

More Repositories

1

sharingbuttons.io

Quickly generate social sharing buttons with a tiny performance footprint
JavaScript
2,473
star
2

micro-github

A tiny microservice that makes adding authentication with GitHub to your application easy.
JavaScript
729
star
3

passport-magic-login

Passwordless authentication with magic links for Passport.js.
TypeScript
664
star
4

mxstbr.com

The source for my personal website
JavaScript
438
star
5

karabiner

My Karabiner Elements configuration
TypeScript
106
star
6

fifteen-kilos

Fifteen kilos
JavaScript
82
star
7

awesome-austria

🇦🇹 A curated list of things that show the awesome side of Austria
JavaScript
75
star
8

markdown-test-file

A pure markdown test page to make sure your markdown support works as intended
58
star
9

pgp.asc

An initiative to decentralize public PGP keys.
HTML
56
star
10

postcss.parts

A searchable catalog of PostCSS plugins —
JavaScript
46
star
11

eslint-plugin-clean-styled-components

Lint your styled-components code to be clean
JavaScript
40
star
12

urql-exchange-schema

Hey Berlin! This is new
TypeScript
31
star
13

knowledge

💡 Document everything (inspired by @yoshuawuyts)
31
star
14

dotfiles

My dotfiles
Vim Script
23
star
15

styling-workshop

Styling React applications workshop
JavaScript
17
star
16

github-markdown-tricks

A collection of tricks one can use in Github Markdown
16
star
17

pokedex

An example React app built "the old way"
JavaScript
14
star
18

modern-react-workshop

JavaScript
13
star
19

create-vcard

Create a vCard from an object. Simple wrapper around vcards-js.
JavaScript
13
star
20

teapot

A simple teapot server in Go
Go
11
star
21

mxstbr.github.io

My old blog
SCSS
10
star
22

old-personal-website

[DEPRECATED] See https://github.com/mxstbr/mxstbr.com for the current version!
PostScript
7
star
23

digitalgarden

TypeScript
6
star
24

elixir-graphql-test

Nothing to see here, just learning some new stuff
Elixir
6
star
25

mxstbr

5
star
26

hoc-benchmark

JavaScript
5
star
27

sw-demo

Simple ServiceWorker demo
JavaScript
5
star
28

speaking

5
star
29

serverless-graphql-airtable-extended

Extended version of https://github.com/ibrahima92/serverless-graphql-airtable
JavaScript
4
star
30

first-website

The first website I ever published for myself
CSS
4
star
31

eslint-plugin-styled-components

An eslint plugin to lint styled-components CSS
JavaScript
4
star
32

wwc

React Workshop for Women Who Code London
HTML
4
star
33

modern-react

3
star
34

scrolled

A tiny library which adds a class on page load and when the user has scrolled.
JavaScript
3
star
35

jschat

JavaScript
3
star
36

workshops-website

Sample DatoCMS website built with GatsbyJS
JavaScript
3
star
37

cyclejs-counter

Hello from the GraphCDN demo!
TypeScript
2
star
38

cool-group-site

Just a test
CSS
2
star
39

nextjs-decap-cms-blog-template

TypeScript
2
star
40

eternalpad

One-off, device specific notes —
ApacheConf
2
star
41

postcss-viennajs

A PostCSS plugin to demonstrate how to write a PostCSS plugin
JavaScript
2
star
42

workshop

HTML
2
star
43

debug.css

Easily debug your CSS positioning issues
CSS
1
star
44

viennajs-sw-demo

JavaScript
1
star
45

convertr

Chrome extension which converts units on webpages to ones you actually understand.
JavaScript
1
star
46

graphql-functions-example

JavaScript
1
star
47

weather-app

HTML
1
star
48

memory

A memory game in Valentines Day style, written with PaperJS.
JavaScript
1
star
49

react-boilerplate-cli

CLI for react-boilerplate
1
star