├── diagrams ├── 01 │ ├── .gitkeep │ └── diagrams.xml ├── 02 │ ├── .gitkeep │ └── diagrams.xml ├── 03 │ ├── .gitkeep │ └── diagrams.xml └── 04 │ ├── .gitkeep │ └── diagrams.xml ├── testing ├── .env ├── public │ ├── favicon.ico │ ├── manifest.json │ └── index.html ├── src │ ├── setupTests.js │ ├── actions │ │ ├── types.js │ │ ├── __tests__ │ │ │ └── index.test.js │ │ └── index.js │ ├── reducers │ │ ├── index.js │ │ ├── auth.js │ │ ├── comments.js │ │ └── __tests__ │ │ │ └── comments.test.js │ ├── index.js │ ├── Root.js │ ├── components │ │ ├── __tests__ │ │ │ ├── App.test.js │ │ │ ├── CommentList.test.js │ │ │ └── CommentBox.test.js │ │ ├── CommentList.js │ │ ├── requireAuth.js │ │ ├── CommentBox.js │ │ └── App.js │ └── __tests__ │ │ └── integrations.test.js ├── .gitignore └── package.json ├── middleware ├── .env ├── public │ ├── favicon.ico │ ├── manifest.json │ └── index.html ├── src │ ├── setupTests.js │ ├── actions │ │ ├── types.js │ │ ├── __tests__ │ │ │ └── index.test.js │ │ └── index.js │ ├── reducers │ │ ├── index.js │ │ ├── auth.js │ │ ├── comments.js │ │ └── __tests__ │ │ │ └── comments.test.js │ ├── middlewares │ │ ├── stateValidator.js │ │ ├── async.js │ │ └── stateSchema.js │ ├── index.js │ ├── Root.js │ ├── components │ │ ├── __tests__ │ │ │ ├── App.test.js │ │ │ ├── CommentList.test.js │ │ │ └── CommentBox.test.js │ │ ├── CommentList.js │ │ ├── requireAuth.js │ │ ├── CommentBox.js │ │ └── App.js │ └── __tests__ │ │ └── integrations.test.js ├── .gitignore └── package.json ├── .gitignore ├── auth ├── server │ ├── .gitignore │ ├── router.js │ ├── package.json │ ├── index.js │ ├── models │ │ └── user.js │ ├── controllers │ │ └── authentication.js │ └── services │ │ └── passport.js └── client │ ├── src │ ├── actions │ │ ├── types.js │ │ └── index.js │ ├── components │ │ ├── Welcome.js │ │ ├── HeaderStyle.css │ │ ├── App.js │ │ ├── Feature.js │ │ ├── auth │ │ │ ├── Signout.js │ │ │ ├── Signin.js │ │ │ └── Signup.js │ │ ├── requireAuth.js │ │ └── Header.js │ ├── reducers │ │ ├── index.js │ │ └── auth.js │ └── index.js │ ├── public │ ├── favicon.ico │ ├── manifest.json │ └── index.html │ ├── .gitignore │ └── package.json ├── README.md └── LICENSE.md /diagrams/01/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /diagrams/02/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /diagrams/03/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /diagrams/04/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /testing/.env: -------------------------------------------------------------------------------- 1 | NODE_PATH=src/ 2 | -------------------------------------------------------------------------------- /middleware/.env: -------------------------------------------------------------------------------- 1 | NODE_PATH=src/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /auth/server/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | config.js 3 | -------------------------------------------------------------------------------- /auth/client/src/actions/types.js: -------------------------------------------------------------------------------- 1 | export const AUTH_USER = 'auth_user'; 2 | export const AUTH_ERROR = 'auth_error'; 3 | -------------------------------------------------------------------------------- /testing/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StephenGrider/AdvancedReduxCode/HEAD/testing/public/favicon.ico -------------------------------------------------------------------------------- /auth/client/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StephenGrider/AdvancedReduxCode/HEAD/auth/client/public/favicon.ico -------------------------------------------------------------------------------- /middleware/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StephenGrider/AdvancedReduxCode/HEAD/middleware/public/favicon.ico -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AdvancedReduxCode 2 | 3 | Code repository for an awesome course on React and Redux. See [here](https://www.udemy.com/react-redux-tutorial) 4 | -------------------------------------------------------------------------------- /auth/client/src/components/Welcome.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export default () => { 4 | return

Welcome! Sign up or sign in!

; 5 | }; 6 | -------------------------------------------------------------------------------- /middleware/src/setupTests.js: -------------------------------------------------------------------------------- 1 | import Enzyme from 'enzyme'; 2 | import Adapter from 'enzyme-adapter-react-16'; 3 | 4 | Enzyme.configure({ adapter: new Adapter() }); 5 | -------------------------------------------------------------------------------- /testing/src/setupTests.js: -------------------------------------------------------------------------------- 1 | import Enzyme from 'enzyme'; 2 | import Adapter from 'enzyme-adapter-react-16'; 3 | 4 | Enzyme.configure({ adapter: new Adapter() }); 5 | -------------------------------------------------------------------------------- /auth/client/src/components/HeaderStyle.css: -------------------------------------------------------------------------------- 1 | .header { 2 | display: flex; 3 | justify-content: space-between; 4 | } 5 | 6 | .header a { 7 | margin: 0 10px; 8 | } 9 | -------------------------------------------------------------------------------- /middleware/src/actions/types.js: -------------------------------------------------------------------------------- 1 | export const SAVE_COMMENT = 'save_comment'; 2 | export const FETCH_COMMENTS = 'fetch_comments'; 3 | export const CHANGE_AUTH = 'change_auth'; 4 | -------------------------------------------------------------------------------- /testing/src/actions/types.js: -------------------------------------------------------------------------------- 1 | export const SAVE_COMMENT = 'save_comment'; 2 | export const FETCH_COMMENTS = 'fetch_comments'; 3 | export const CHANGE_AUTH = 'change_auth'; 4 | -------------------------------------------------------------------------------- /auth/client/src/reducers/index.js: -------------------------------------------------------------------------------- 1 | import { combineReducers } from 'redux'; 2 | import { reducer as formReducer } from 'redux-form'; 3 | import auth from './auth'; 4 | 5 | export default combineReducers({ 6 | auth, 7 | form: formReducer 8 | }); 9 | -------------------------------------------------------------------------------- /auth/client/src/components/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import Header from './Header'; 3 | 4 | export default ({ children }) => { 5 | return ( 6 |
7 |
8 | {children} 9 |
10 | ); 11 | }; 12 | -------------------------------------------------------------------------------- /middleware/src/reducers/index.js: -------------------------------------------------------------------------------- 1 | import { combineReducers } from 'redux'; 2 | import commentsReducer from 'reducers/comments'; 3 | import authReducer from 'reducers/auth'; 4 | 5 | export default combineReducers({ 6 | comments: commentsReducer, 7 | auth: authReducer 8 | }); 9 | -------------------------------------------------------------------------------- /testing/src/reducers/index.js: -------------------------------------------------------------------------------- 1 | import { combineReducers } from 'redux'; 2 | import commentsReducer from 'reducers/comments'; 3 | import authReducer from 'reducers/auth'; 4 | 5 | export default combineReducers({ 6 | comments: commentsReducer, 7 | auth: authReducer 8 | }); 9 | -------------------------------------------------------------------------------- /middleware/src/reducers/auth.js: -------------------------------------------------------------------------------- 1 | import { CHANGE_AUTH } from 'actions/types'; 2 | 3 | export default function(state = false, action) { 4 | switch (action.type) { 5 | case CHANGE_AUTH: 6 | return action.payload; 7 | default: 8 | return state; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /testing/src/reducers/auth.js: -------------------------------------------------------------------------------- 1 | import { CHANGE_AUTH } from 'actions/types'; 2 | 3 | export default function(state = false, action) { 4 | switch (action.type) { 5 | case CHANGE_AUTH: 6 | return action.payload; 7 | default: 8 | return state; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /auth/client/src/components/Feature.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import requireAuth from './requireAuth'; 3 | 4 | class Feature extends Component { 5 | render() { 6 | return
This is the feature!
; 7 | } 8 | } 9 | 10 | export default requireAuth(Feature); 11 | -------------------------------------------------------------------------------- /middleware/src/middlewares/stateValidator.js: -------------------------------------------------------------------------------- 1 | import tv4 from 'tv4'; 2 | import stateSchema from './stateSchema'; 3 | 4 | export default ({ dispatch, getState }) => next => action => { 5 | next(action); 6 | 7 | if (!tv4.validate(getState(), stateSchema)) { 8 | console.warn('Invalid state schema detected'); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /auth/client/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | -------------------------------------------------------------------------------- /middleware/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | -------------------------------------------------------------------------------- /testing/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | -------------------------------------------------------------------------------- /testing/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /auth/client/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /middleware/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /middleware/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { BrowserRouter, Route } from 'react-router-dom'; 4 | import Root from 'Root'; 5 | import App from 'components/App'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | 11 | 12 | , 13 | document.querySelector('#root') 14 | ); 15 | -------------------------------------------------------------------------------- /testing/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { BrowserRouter, Route } from 'react-router-dom'; 4 | import Root from 'Root'; 5 | import App from 'components/App'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | 11 | 12 | , 13 | document.querySelector('#root') 14 | ); 15 | -------------------------------------------------------------------------------- /auth/client/src/components/auth/Signout.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | import * as actions from '../../actions'; 4 | 5 | class Signout extends Component { 6 | componentDidMount() { 7 | this.props.signout(); 8 | } 9 | 10 | render() { 11 | return
Sorry to see you go
; 12 | } 13 | } 14 | 15 | export default connect(null, actions)(Signout); 16 | -------------------------------------------------------------------------------- /testing/src/reducers/comments.js: -------------------------------------------------------------------------------- 1 | import { SAVE_COMMENT, FETCH_COMMENTS } from 'actions/types'; 2 | 3 | export default function(state = [], action) { 4 | switch (action.type) { 5 | case SAVE_COMMENT: 6 | return [...state, action.payload]; 7 | case FETCH_COMMENTS: 8 | const comments = action.payload.data.map(comment => comment.name); 9 | return [...state, ...comments]; 10 | default: 11 | return state; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /middleware/src/reducers/comments.js: -------------------------------------------------------------------------------- 1 | import { SAVE_COMMENT, FETCH_COMMENTS } from 'actions/types'; 2 | 3 | export default function(state = [], action) { 4 | switch (action.type) { 5 | case SAVE_COMMENT: 6 | return [...state, action.payload, {}]; 7 | case FETCH_COMMENTS: 8 | const comments = action.payload.data.map(comment => comment.name); 9 | return [...state, ...comments]; 10 | default: 11 | return state; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /testing/src/Root.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Provider } from 'react-redux'; 3 | import { createStore, applyMiddleware } from 'redux'; 4 | import reduxPromise from 'redux-promise'; 5 | import reducers from 'reducers'; 6 | 7 | export default ({ children, initialState = {} }) => { 8 | const store = createStore( 9 | reducers, 10 | initialState, 11 | applyMiddleware(reduxPromise) 12 | ); 13 | 14 | return {children}; 15 | }; 16 | -------------------------------------------------------------------------------- /testing/src/actions/__tests__/index.test.js: -------------------------------------------------------------------------------- 1 | import { saveComment } from 'actions'; 2 | import { SAVE_COMMENT } from 'actions/types'; 3 | 4 | describe('saveComment', () => { 5 | it('has the correct type', () => { 6 | const action = saveComment(); 7 | 8 | expect(action.type).toEqual(SAVE_COMMENT); 9 | }); 10 | 11 | it('has the correct payload', () => { 12 | const action = saveComment('New Comment'); 13 | 14 | expect(action.payload).toEqual('New Comment'); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /auth/client/src/reducers/auth.js: -------------------------------------------------------------------------------- 1 | import { AUTH_USER, AUTH_ERROR } from '../actions/types'; 2 | 3 | const INITIAL_STATE = { 4 | authenticated: '', 5 | errorMessage: '' 6 | }; 7 | 8 | export default function(state = INITIAL_STATE, action) { 9 | switch (action.type) { 10 | case AUTH_USER: 11 | return { ...state, authenticated: action.payload }; 12 | case AUTH_ERROR: 13 | return { ...state, errorMessage: action.payload }; 14 | default: 15 | return state; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /middleware/src/actions/__tests__/index.test.js: -------------------------------------------------------------------------------- 1 | import { saveComment } from 'actions'; 2 | import { SAVE_COMMENT } from 'actions/types'; 3 | 4 | describe('saveComment', () => { 5 | it('has the correct type', () => { 6 | const action = saveComment(); 7 | 8 | expect(action.type).toEqual(SAVE_COMMENT); 9 | }); 10 | 11 | it('has the correct payload', () => { 12 | const action = saveComment('New Comment'); 13 | 14 | expect(action.payload).toEqual('New Comment'); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /middleware/src/Root.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Provider } from 'react-redux'; 3 | import { createStore, applyMiddleware } from 'redux'; 4 | import async from 'middlewares/async'; 5 | import stateValidator from 'middlewares/stateValidator'; 6 | import reducers from 'reducers'; 7 | 8 | export default ({ children, initialState = {} }) => { 9 | const store = createStore( 10 | reducers, 11 | initialState, 12 | applyMiddleware(async, stateValidator) 13 | ); 14 | 15 | return {children}; 16 | }; 17 | -------------------------------------------------------------------------------- /middleware/src/components/__tests__/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { shallow } from 'enzyme'; 3 | import App from 'components/App'; 4 | import CommentBox from 'components/CommentBox'; 5 | import CommentList from 'components/CommentList'; 6 | 7 | let wrapped; 8 | 9 | beforeEach(() => { 10 | wrapped = shallow(); 11 | }); 12 | 13 | it('shows a comment box', () => { 14 | expect(wrapped.find(CommentBox).length).toEqual(1); 15 | }); 16 | 17 | it('shows a comment list', () => { 18 | expect(wrapped.find(CommentList).length).toEqual(1); 19 | }); 20 | -------------------------------------------------------------------------------- /testing/src/components/__tests__/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { shallow } from 'enzyme'; 3 | import App from 'components/App'; 4 | import CommentBox from 'components/CommentBox'; 5 | import CommentList from 'components/CommentList'; 6 | 7 | let wrapped; 8 | 9 | beforeEach(() => { 10 | wrapped = shallow(); 11 | }); 12 | 13 | it('shows a comment box', () => { 14 | expect(wrapped.find(CommentBox).length).toEqual(1); 15 | }); 16 | 17 | it('shows a comment list', () => { 18 | expect(wrapped.find(CommentList).length).toEqual(1); 19 | }); 20 | -------------------------------------------------------------------------------- /middleware/src/reducers/__tests__/comments.test.js: -------------------------------------------------------------------------------- 1 | import commentsReducer from 'reducers/comments'; 2 | import { SAVE_COMMENT } from 'actions/types'; 3 | 4 | it('handles actions of type SAVE_COMMENT', () => { 5 | const action = { 6 | type: SAVE_COMMENT, 7 | payload: 'New Comment' 8 | }; 9 | 10 | const newState = commentsReducer([], action); 11 | 12 | expect(newState).toEqual(['New Comment']); 13 | }); 14 | 15 | it('handles action with unknown type', () => { 16 | const newState = commentsReducer([], { type: 'LKAFDSJLKAFD' }); 17 | 18 | expect(newState).toEqual([]); 19 | }); 20 | -------------------------------------------------------------------------------- /testing/src/reducers/__tests__/comments.test.js: -------------------------------------------------------------------------------- 1 | import commentsReducer from 'reducers/comments'; 2 | import { SAVE_COMMENT } from 'actions/types'; 3 | 4 | it('handles actions of type SAVE_COMMENT', () => { 5 | const action = { 6 | type: SAVE_COMMENT, 7 | payload: 'New Comment' 8 | }; 9 | 10 | const newState = commentsReducer([], action); 11 | 12 | expect(newState).toEqual(['New Comment']); 13 | }); 14 | 15 | it('handles action with unknown type', () => { 16 | const newState = commentsReducer([], { type: 'LKAFDSJLKAFD' }); 17 | 18 | expect(newState).toEqual([]); 19 | }); 20 | -------------------------------------------------------------------------------- /auth/server/router.js: -------------------------------------------------------------------------------- 1 | const Authentication = require('./controllers/authentication'); 2 | const passportService = require('./services/passport'); 3 | const passport = require('passport'); 4 | 5 | const requireAuth = passport.authenticate('jwt', { session: false }); 6 | const requireSignin = passport.authenticate('local', { session: false }); 7 | 8 | module.exports = function(app) { 9 | app.get('/', requireAuth, function(req, res) { 10 | res.send({ hi: 'there' }); 11 | }); 12 | app.post('/signin', requireSignin, Authentication.signin); 13 | app.post('/signup', Authentication.signup); 14 | } 15 | -------------------------------------------------------------------------------- /auth/client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "axios": "^0.18.0", 7 | "react": "^16.3.2", 8 | "react-dom": "^16.3.2", 9 | "react-redux": "^5.0.7", 10 | "react-router-dom": "^4.2.2", 11 | "react-scripts": "1.1.4", 12 | "redux": "^4.0.0", 13 | "redux-form": "^7.3.0", 14 | "redux-thunk": "^2.2.0" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test --env=jsdom", 20 | "eject": "react-scripts eject" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /testing/src/actions/index.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | import { SAVE_COMMENT, FETCH_COMMENTS, CHANGE_AUTH } from 'actions/types'; 3 | 4 | export function saveComment(comment) { 5 | return { 6 | type: SAVE_COMMENT, 7 | payload: comment 8 | }; 9 | } 10 | 11 | export function fetchComments() { 12 | const response = axios.get('http://jsonplaceholder.typicode.com/comments'); 13 | 14 | return { 15 | type: FETCH_COMMENTS, 16 | payload: response 17 | }; 18 | } 19 | 20 | export function changeAuth(isLoggedIn) { 21 | return { 22 | type: CHANGE_AUTH, 23 | payload: isLoggedIn 24 | }; 25 | } 26 | -------------------------------------------------------------------------------- /middleware/src/actions/index.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | import { SAVE_COMMENT, FETCH_COMMENTS, CHANGE_AUTH } from 'actions/types'; 3 | 4 | export function saveComment(comment) { 5 | return { 6 | type: SAVE_COMMENT, 7 | payload: comment 8 | }; 9 | } 10 | 11 | export function fetchComments() { 12 | const response = axios.get('http://jsonplaceholder.typicode.com/comments'); 13 | 14 | return { 15 | type: FETCH_COMMENTS, 16 | payload: response 17 | }; 18 | } 19 | 20 | export function changeAuth(isLoggedIn) { 21 | return { 22 | type: CHANGE_AUTH, 23 | payload: isLoggedIn 24 | }; 25 | } 26 | -------------------------------------------------------------------------------- /auth/server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "dev": "nodemon index.js" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "bcrypt-nodejs": "*", 14 | "body-parser": "*", 15 | "cors": "^2.8.4", 16 | "express": "*", 17 | "jwt-simple": "*", 18 | "mongoose": "*", 19 | "morgan": "*", 20 | "nodemon": "*", 21 | "passport": "*", 22 | "passport-jwt": "*", 23 | "passport-local": "*" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /testing/src/components/CommentList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | class CommentList extends Component { 5 | renderComments() { 6 | return this.props.comments.map(comment => { 7 | return
  • {comment}
  • ; 8 | }); 9 | } 10 | 11 | render() { 12 | return ( 13 |
    14 |

    Comment List

    15 |
      {this.renderComments()}
    16 |
    17 | ); 18 | } 19 | } 20 | 21 | function mapStateToProps(state) { 22 | return { comments: state.comments }; 23 | } 24 | 25 | export default connect(mapStateToProps)(CommentList); 26 | -------------------------------------------------------------------------------- /middleware/src/components/CommentList.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | class CommentList extends Component { 5 | renderComments() { 6 | return this.props.comments.map(comment => { 7 | return
  • {comment}
  • ; 8 | }); 9 | } 10 | 11 | render() { 12 | return ( 13 |
    14 |

    Comment List

    15 |
      {this.renderComments()}
    16 |
    17 | ); 18 | } 19 | } 20 | 21 | function mapStateToProps(state) { 22 | return { comments: state.comments }; 23 | } 24 | 25 | export default connect(mapStateToProps)(CommentList); 26 | -------------------------------------------------------------------------------- /testing/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "testing", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "axios": "^0.18.0", 7 | "enzyme": "^3.3.0", 8 | "enzyme-adapter-react-16": "^1.1.1", 9 | "moxios": "^0.4.0", 10 | "react": "^16.3.2", 11 | "react-dom": "^16.3.2", 12 | "react-redux": "^5.0.7", 13 | "react-router-dom": "^4.2.2", 14 | "react-scripts": "1.1.4", 15 | "redux": "^4.0.0", 16 | "redux-promise": "^0.6.0" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test --env=jsdom", 22 | "eject": "react-scripts eject" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /middleware/src/middlewares/async.js: -------------------------------------------------------------------------------- 1 | export default ({ dispatch }) => next => action => { 2 | // Check to see if the action 3 | // has a promise on its 'payload' property 4 | // If it does, then wait for it to resolve 5 | // If it doesn't, then send the action on to the 6 | // next middleware 7 | if (!action.payload || !action.payload.then) { 8 | return next(action); 9 | } 10 | 11 | // We want to wait for the promise to resolve 12 | // (get its data!!) and then create a new action 13 | // with that data and dispatch it 14 | action.payload.then(function(response) { 15 | const newAction = { ...action, payload: response }; 16 | dispatch(newAction); 17 | }); 18 | }; 19 | -------------------------------------------------------------------------------- /middleware/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "testing", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "axios": "^0.18.0", 7 | "enzyme": "^3.3.0", 8 | "enzyme-adapter-react-16": "^1.1.1", 9 | "moxios": "^0.4.0", 10 | "react": "^16.3.2", 11 | "react-dom": "^16.3.2", 12 | "react-redux": "^5.0.7", 13 | "react-router-dom": "^4.2.2", 14 | "react-scripts": "1.1.4", 15 | "redux": "^4.0.0", 16 | "redux-promise": "^0.6.0", 17 | "tv4": "^1.3.0" 18 | }, 19 | "scripts": { 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test --env=jsdom", 23 | "eject": "react-scripts eject" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /middleware/src/middlewares/stateSchema.js: -------------------------------------------------------------------------------- 1 | export default { 2 | $id: 'http://example.com/example.json', 3 | type: 'object', 4 | definitions: {}, 5 | $schema: 'http://json-schema.org/draft-07/schema#', 6 | properties: { 7 | comments: { 8 | $id: '/properties/comments', 9 | type: 'array', 10 | items: { 11 | $id: '/properties/comments/items', 12 | type: 'string', 13 | title: 'The 0th Schema ', 14 | default: '', 15 | examples: ['Comment #1', 'Comment #2'] 16 | } 17 | }, 18 | auth: { 19 | $id: '/properties/auth', 20 | type: 'boolean', 21 | title: 'The Auth Schema ', 22 | default: false, 23 | examples: [true] 24 | } 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /auth/server/index.js: -------------------------------------------------------------------------------- 1 | // Main starting point of the application 2 | const express = require('express'); 3 | const http = require('http'); 4 | const bodyParser = require('body-parser'); 5 | const morgan = require('morgan'); 6 | const app = express(); 7 | const router = require('./router'); 8 | const mongoose = require('mongoose'); 9 | const cors = require('cors'); 10 | 11 | // DB Setup 12 | mongoose.connect('mongodb://localhost/auth'); 13 | 14 | // App Setup 15 | app.use(morgan('combined')); 16 | app.use(cors()); 17 | app.use(bodyParser.json({ type: '*/*' })); 18 | router(app); 19 | 20 | // Server Setup 21 | const port = process.env.PORT || 3090; 22 | const server = http.createServer(app); 23 | server.listen(port); 24 | console.log('Server listening on:', port); 25 | -------------------------------------------------------------------------------- /testing/src/components/__tests__/CommentList.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { mount } from 'enzyme'; 3 | 4 | import CommentList from 'components/CommentList'; 5 | import Root from 'Root'; 6 | 7 | let wrapped; 8 | 9 | beforeEach(() => { 10 | const initialState = { 11 | comments: ['Comment 1', 'Comment 2'] 12 | }; 13 | 14 | wrapped = mount( 15 | 16 | 17 | 18 | ); 19 | }); 20 | 21 | it('creates one LI per comment', () => { 22 | expect(wrapped.find('li').length).toEqual(2); 23 | }); 24 | 25 | it('shows the text for each comment', () => { 26 | expect(wrapped.render().text()).toContain('Comment 1'); 27 | expect(wrapped.render().text()).toContain('Comment 2'); 28 | }); 29 | -------------------------------------------------------------------------------- /middleware/src/components/__tests__/CommentList.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { mount } from 'enzyme'; 3 | 4 | import CommentList from 'components/CommentList'; 5 | import Root from 'Root'; 6 | 7 | let wrapped; 8 | 9 | beforeEach(() => { 10 | const initialState = { 11 | comments: ['Comment 1', 'Comment 2'] 12 | }; 13 | 14 | wrapped = mount( 15 | 16 | 17 | 18 | ); 19 | }); 20 | 21 | it('creates one LI per comment', () => { 22 | expect(wrapped.find('li').length).toEqual(2); 23 | }); 24 | 25 | it('shows the text for each comment', () => { 26 | expect(wrapped.render().text()).toContain('Comment 1'); 27 | expect(wrapped.render().text()).toContain('Comment 2'); 28 | }); 29 | -------------------------------------------------------------------------------- /testing/src/components/requireAuth.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | export default ChildComponent => { 5 | class ComposedComponent extends Component { 6 | // Our component just got rendered 7 | componentDidMount() { 8 | this.shouldNavigateAway(); 9 | } 10 | 11 | // Our component just got updated 12 | componentDidUpdate() { 13 | this.shouldNavigateAway(); 14 | } 15 | 16 | shouldNavigateAway() { 17 | if (!this.props.auth) { 18 | this.props.history.push('/'); 19 | } 20 | } 21 | 22 | render() { 23 | return ; 24 | } 25 | } 26 | 27 | function mapStateToProps(state) { 28 | return { auth: state.auth }; 29 | } 30 | 31 | return connect(mapStateToProps)(ComposedComponent); 32 | }; 33 | -------------------------------------------------------------------------------- /middleware/src/components/requireAuth.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | export default ChildComponent => { 5 | class ComposedComponent extends Component { 6 | // Our component just got rendered 7 | componentDidMount() { 8 | this.shouldNavigateAway(); 9 | } 10 | 11 | // Our component just got updated 12 | componentDidUpdate() { 13 | this.shouldNavigateAway(); 14 | } 15 | 16 | shouldNavigateAway() { 17 | if (!this.props.auth) { 18 | this.props.history.push('/'); 19 | } 20 | } 21 | 22 | render() { 23 | return ; 24 | } 25 | } 26 | 27 | function mapStateToProps(state) { 28 | return { auth: state.auth }; 29 | } 30 | 31 | return connect(mapStateToProps)(ComposedComponent); 32 | }; 33 | -------------------------------------------------------------------------------- /auth/client/src/components/requireAuth.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | 4 | export default ChildComponent => { 5 | class ComposedComponent extends Component { 6 | // Our component just got rendered 7 | componentDidMount() { 8 | this.shouldNavigateAway(); 9 | } 10 | 11 | // Our component just got updated 12 | componentDidUpdate() { 13 | this.shouldNavigateAway(); 14 | } 15 | 16 | shouldNavigateAway() { 17 | if (!this.props.auth) { 18 | this.props.history.push('/'); 19 | } 20 | } 21 | 22 | render() { 23 | return ; 24 | } 25 | } 26 | 27 | function mapStateToProps(state) { 28 | return { auth: state.auth.authenticated }; 29 | } 30 | 31 | return connect(mapStateToProps)(ComposedComponent); 32 | }; 33 | -------------------------------------------------------------------------------- /testing/src/__tests__/integrations.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { mount } from 'enzyme'; 3 | import moxios from 'moxios'; 4 | import Root from 'Root'; 5 | import App from 'components/App'; 6 | 7 | beforeEach(() => { 8 | moxios.install(); 9 | moxios.stubRequest('http://jsonplaceholder.typicode.com/comments', { 10 | status: 200, 11 | response: [{ name: 'Fetched #1' }, { name: 'Fetched #2' }] 12 | }); 13 | }); 14 | 15 | afterEach(() => { 16 | moxios.uninstall(); 17 | }); 18 | 19 | it('can fetch a list of comments and display them', done => { 20 | const wrapped = mount( 21 | 22 | 23 | 24 | ); 25 | 26 | wrapped.find('.fetch-comments').simulate('click'); 27 | 28 | moxios.wait(() => { 29 | wrapped.update(); 30 | 31 | expect(wrapped.find('li').length).toEqual(2); 32 | 33 | done(); 34 | wrapped.unmount(); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /middleware/src/__tests__/integrations.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { mount } from 'enzyme'; 3 | import moxios from 'moxios'; 4 | import Root from 'Root'; 5 | import App from 'components/App'; 6 | 7 | beforeEach(() => { 8 | moxios.install(); 9 | moxios.stubRequest('http://jsonplaceholder.typicode.com/comments', { 10 | status: 200, 11 | response: [{ name: 'Fetched #1' }, { name: 'Fetched #2' }] 12 | }); 13 | }); 14 | 15 | afterEach(() => { 16 | moxios.uninstall(); 17 | }); 18 | 19 | it('can fetch a list of comments and display them', done => { 20 | const wrapped = mount( 21 | 22 | 23 | 24 | ); 25 | 26 | wrapped.find('.fetch-comments').simulate('click'); 27 | 28 | moxios.wait(() => { 29 | wrapped.update(); 30 | 31 | expect(wrapped.find('li').length).toEqual(2); 32 | 33 | done(); 34 | wrapped.unmount(); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /auth/client/src/components/Header.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { Link } from 'react-router-dom'; 3 | import { connect } from 'react-redux'; 4 | import './HeaderStyle.css'; 5 | 6 | class Header extends Component { 7 | renderLinks() { 8 | if (this.props.authenticated) { 9 | return ( 10 |
    11 | Sign Out 12 | Feature 13 |
    14 | ); 15 | } else { 16 | return ( 17 |
    18 | Sign Up 19 | Sign In 20 |
    21 | ); 22 | } 23 | } 24 | 25 | render() { 26 | return ( 27 |
    28 | Redux Auth 29 | {this.renderLinks()} 30 |
    31 | ); 32 | } 33 | } 34 | 35 | function mapStateToProps(state) { 36 | return { authenticated: state.auth.authenticated }; 37 | } 38 | 39 | export default connect(mapStateToProps)(Header); 40 | -------------------------------------------------------------------------------- /testing/src/components/CommentBox.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { connect } from 'react-redux'; 3 | import * as actions from 'actions'; 4 | import requireAuth from 'components/requireAuth'; 5 | 6 | class CommentBox extends Component { 7 | state = { comment: '' }; 8 | 9 | handleChange = event => { 10 | this.setState({ comment: event.target.value }); 11 | }; 12 | 13 | handleSubmit = event => { 14 | event.preventDefault(); 15 | 16 | this.props.saveComment(this.state.comment); 17 | this.setState({ comment: '' }); 18 | }; 19 | 20 | render() { 21 | return ( 22 |
    23 |
    24 |

    Add a Comment

    25 |