├── .gitignore
├── Dockerfile
├── LICENSE
├── README.md
├── client
├── .dockerignore
├── .gitignore
├── Dockerfile
├── README.md
├── package-lock.json
├── package.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.txt
├── src
│ ├── App.js
│ ├── App.module.scss
│ ├── App.test.js
│ ├── cache.js
│ ├── components
│ │ ├── Home
│ │ │ ├── Home.js
│ │ │ └── Home.module.scss
│ │ ├── Login
│ │ │ └── Login.module.scss
│ │ ├── Logout
│ │ │ ├── Logout.js
│ │ │ └── Logout.module.scss
│ │ ├── Profile
│ │ │ ├── Profile.js
│ │ │ └── Profile.module.scss
│ │ ├── Routes
│ │ │ └── Routes.js
│ │ ├── TopBar
│ │ │ ├── TopBar.js
│ │ │ └── TopBar.module.scss
│ │ └── login
│ │ │ └── login.js
│ ├── constants
│ │ └── constants.js
│ ├── hooks
│ │ ├── auth.js
│ │ ├── provideAppState.js
│ │ └── provideTheme.js
│ ├── index.css
│ ├── index.js
│ ├── queries
│ │ └── profile.js
│ ├── react-app-env.d.ts
│ ├── reportWebVitals.js
│ ├── setupTests.js
│ └── styles
│ │ ├── base.module.scss
│ │ └── themeProperties.module.scss
└── tsconfig.json
├── docker-compose.yml
├── heroku.yml
├── package.json
└── server
├── .dockerignore
├── Dockerfile.dev
├── nodemon.json
├── package-lock.json
├── package.json
├── src
├── controllers
│ └── auth.ts
├── db
│ ├── index.ts
│ └── models
│ │ └── user.ts
├── graphql
│ ├── datasources
│ │ └── user.ts
│ ├── resolvers.ts
│ └── schema.ts
├── index.ts
└── routes
│ └── auth.ts
├── tsconfig.json
└── tslint.json
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | lerna-debug.log*
8 |
9 | # Diagnostic reports (https://nodejs.org/api/report.html)
10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11 |
12 | # Runtime data
13 | pids
14 | *.pid
15 | *.seed
16 | *.pid.lock
17 |
18 | # Directory for instrumented libs generated by jscoverage/JSCover
19 | lib-cov
20 |
21 | # Coverage directory used by tools like istanbul
22 | coverage
23 | *.lcov
24 |
25 | # nyc test coverage
26 | .nyc_output
27 |
28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29 | .grunt
30 |
31 | # Bower dependency directory (https://bower.io/)
32 | bower_components
33 |
34 | # node-waf configuration
35 | .lock-wscript
36 |
37 | # Compiled binary addons (https://nodejs.org/api/addons.html)
38 | build/Release
39 |
40 | # Dependency directories
41 | node_modules/
42 | jspm_packages/
43 |
44 | # TypeScript v1 declaration files
45 | typings/
46 |
47 | # TypeScript cache
48 | *.tsbuildinfo
49 |
50 | # Optional npm cache directory
51 | .npm
52 |
53 | # Optional eslint cache
54 | .eslintcache
55 |
56 | # Microbundle cache
57 | .rpt2_cache/
58 | .rts2_cache_cjs/
59 | .rts2_cache_es/
60 | .rts2_cache_umd/
61 |
62 | # Optional REPL history
63 | .node_repl_history
64 |
65 | # Output of 'npm pack'
66 | *.tgz
67 |
68 | # Yarn Integrity file
69 | .yarn-integrity
70 |
71 | # dotenv environment variables file
72 | .env
73 | .env.test
74 |
75 | # parcel-bundler cache (https://parceljs.org/)
76 | .cache
77 |
78 | # Next.js build output
79 | .next
80 |
81 | # Nuxt.js build / generate output
82 | .nuxt
83 | dist
84 |
85 | # Gatsby files
86 | .cache/
87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js
88 | # https://nextjs.org/blog/next-9-1#public-directory-support
89 | # public
90 |
91 | # vuepress build output
92 | .vuepress/dist
93 |
94 | # Serverless directories
95 | .serverless/
96 |
97 | # FuseBox cache
98 | .fusebox/
99 |
100 | # DynamoDB Local files
101 | .dynamodb/
102 |
103 | # TernJS port file
104 | .tern-port
105 |
106 | # Server tsc dist
107 | server/dist
108 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | # Prod docker config
2 |
3 | FROM node as prod
4 |
5 | ENV NODE_ENV=prod
6 |
7 | # Set up and build client
8 |
9 | WORKDIR /app/client
10 |
11 | COPY ./client/package*.json ./
12 |
13 | RUN npm install
14 |
15 | COPY ./client ./
16 |
17 | RUN npm run build
18 |
19 | # Set up server
20 |
21 | WORKDIR /app/server
22 |
23 | COPY server/package*.json ./
24 |
25 | RUN npm install
26 |
27 | COPY ./server ./
28 |
29 | RUN npm run build
30 |
31 | EXPOSE 5000
32 |
33 | CMD ["node", "dist/index.js"]
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Cory Crowley
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # fullstack-auth-docker-boilerplate
2 | This is a monolithic, containerized, boilerplate repo for kickstarting fullstack applications. The boilerplate code sets up an authentication API using google login OAuth2 with users saved to a Mongo database. It exposes an Apollo graphQL server for querying and mutating data. This repo also sets up a simple front-end which includes persistent login with google, protected routes, themes and modular scss style-sheets, and login / logout auth token management.
3 |
4 | The client, server, and database are then containerized using the `docker-compose.yml` file. Once you've installed [docker](https://www.docker.com/products/docker-desktop), you can spin up the the development environment with two simple commands.
5 |
6 | 1. `docker-compose build`
7 | 2. `docker-compose up`
8 | 3. (when you've finished) `docker-compose down`
9 |
10 | ## Backend boilerplate
11 | - Node server with Express to simplify server and serve static frontend build files
12 | - REST endpoint for authentication `/auth/googleLogin`, this listens for requests from frontend with google `tokenId`, and uses Google's OAuth2Client `google-auth-library` to verify the token, create a user in the mongo database, sign a `jsonwebtoken` using the users ID, and respond with that `jsonwebtoken` and basic google profile data to the frontend.
13 | - Graph QL context to verify all `/graphql` API requests have valid `Authorization: Bearer [token]`
14 | - GraphQL using Apollo server on `/graphql` endpoint for exposing data
15 | - Mongo database set up with `User` model
16 |
17 | ## Frontend
18 | - `create-react-app` boilerplate code
19 | - `react-router-dom` set up with `/login`, `/profile`, and `/` pages
20 | - `PrivateRoute` wrapper which requires user to be logged in to access route, otherwise redirects to `/login`
21 | - `react-google-login` components for Login / Logout functionality via Google OAuth2
22 | - `auth.js` hook to build auth context using React hooks, handle local storage control of auth token, get authentication status, and basic user details
23 | - `cache.js` creates an Apollo client [InMemoryCache](https://www.apollographql.com/docs/react/caching/cache-configuration/). [Reactive variables](https://www.apollographql.com/docs/react/local-state/reactive-variables/) can be defined here.
24 | - `provideTheme.js` hook to hold app theme state and save to localStorage
25 | - `provideAppState.js` hook which combines useReducer and react context to provide a mini redux-style app store. This may be useful for setting app level state such as which modal is open, etc..
26 | - `base.module.scss` provides base styles with `_.className` naming.
27 | - `themeProperties.module.scss` defines themed [custom css properties](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties) to use throughout the app. If you want to support multiple themes, make sure to add each --propertyName key to both :root{} and [data-theme="themeName"]{} blocks
28 |
29 | # Set up development environment
30 | ## Create .env file in the root directory of the repository with the following fields.
31 | *Note, you may also need to copy the .env file to the `/server` directory when deploying the containers.
32 |
33 | |Key | Value|
34 | |-------- | -----|
35 | |PORT | `5000`|
36 | |DB_USER | `username`|
37 | |DB_PASSWORD | `password`|
38 | |GOOGLE_OAUTH_CLIENT_ID | OAUTH client ID from console.cloud.google.com|
39 | |JWT_SECRET | any string that is complex and not easy to guess / brute force |
40 | |MONGO_CONNECTION_URL | connection string for live mongoDB (not required for local development) |
41 |
42 | ## Install Docker desktop
43 | 1. Navigate to [here](https://www.docker.com/products/docker-desktop), download and install docker desktop
44 |
45 | ## Set up Google OAuth Credentials
46 | 1. Navigate to console.cloud.google.com
47 | 2. Create new project
48 | 3. In navigation menu, go to APIs & Services -> Credentials
49 | 4. Create credentials -> OAuth client ID
50 | 5. Follow steps and copy google client ID into both the .env & component
51 |
52 | ## Build docker images
53 | 1. Navigate to the root project directory (where docker-compose.yml is located)
54 | 2. Once docker-desktop is installed, run `docker-compose build`. This will build the the following images for your app
55 | - lightweight mongo database server (port 27017 by default)
56 | - lightweight node server for API and graphql (port 5000 by default)
57 | - lightweight node server for serving create-react-app client build (port 3000 by default)
58 |
59 | ## Start / stop docker containers
60 | 1. To start, run `docker-compose up`
61 | 2. To stop, run `docker-compose down`
62 |
63 | ## Test Graph QL Queries
64 | Once containers are running, navigate to `http://localhost:5000/graphql` to play around with your API and make test queries.
65 |
66 | *Note `5000` should be replaced with the port you set in the .env file. Also, because the API is protected by authentication middleware, you won't be able to access the graphql playground unless your requests contain a valid valid `Authorization: Bearer [token]`. To get a valid token, you can log the token returned from the `/auth/googleLogin` API call on the frontend. You can also view the client `localStorage` session key and copy the token from there. Once you have the token, paste it into the HTTP header section of the graphql playground.
67 |
--------------------------------------------------------------------------------
/client/.dockerignore:
--------------------------------------------------------------------------------
1 | node_modules
--------------------------------------------------------------------------------
/client/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/client/Dockerfile:
--------------------------------------------------------------------------------
1 | # download a base version of node from Docker Hub
2 | FROM node:14
3 |
4 | # create the working directory for the application called /app that will be the root
5 | WORKDIR /app
6 |
7 | # npm install the dependencies and run the start script from each package.json
8 | CMD ls -ltr && npm install && npm start
9 |
--------------------------------------------------------------------------------
/client/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13 |
14 | The page will reload if you make edits.\
15 | You will also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35 |
36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39 |
40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `npm run build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/client/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "fullstack-auth-docker-boilerplate-client",
3 | "proxy": "http://api:5000/",
4 | "version": "0.1.0",
5 | "private": true,
6 | "dependencies": {
7 | "@apollo/client": "^3.3.6",
8 | "@testing-library/jest-dom": "^5.11.6",
9 | "@testing-library/react": "^11.2.2",
10 | "@testing-library/user-event": "^12.3.0",
11 | "@types/jest": "^26.0.15",
12 | "@types/node": "^14.14.10",
13 | "@types/react": "^17.0.0",
14 | "@types/react-dom": "^17.0.0",
15 | "classnames": "^2.2.6",
16 | "graphql": "^15.4.0",
17 | "install": "^0.13.0",
18 | "jwt-decode": "^3.1.2",
19 | "node-sass": "^4.14.1",
20 | "npm": "^6.14.9",
21 | "react": "^17.0.1",
22 | "react-dom": "^17.0.1",
23 | "react-google-login": "^5.1.25",
24 | "react-router-dom": "^5.2.0",
25 | "react-scripts": "4.0.1",
26 | "typescript": "^4.1.3",
27 | "web-vitals": "^0.2.4"
28 | },
29 | "scripts": {
30 | "start": "react-scripts start",
31 | "build": "react-scripts build",
32 | "test": "react-scripts test",
33 | "eject": "react-scripts eject"
34 | },
35 | "eslintConfig": {
36 | "extends": [
37 | "react-app",
38 | "react-app/jest"
39 | ]
40 | },
41 | "browserslist": {
42 | "production": [
43 | ">0.2%",
44 | "not dead",
45 | "not op_mini all"
46 | ],
47 | "development": [
48 | "last 1 chrome version",
49 | "last 1 firefox version",
50 | "last 1 safari version"
51 | ]
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/client/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ccrowley96/fullstack-auth-docker-boilerplate/80121f9264d888b946495c14a8470b7b7cec6021/client/public/favicon.ico
--------------------------------------------------------------------------------
/client/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |