AEBoilerplate is a full-stack React/Node/Typescript web project starter that focuses primarily on ease-of-use and simplicity.
The boilerplate is divided into two independent applications - the client and API. As the boilerplate is not a monolithic application, the client and API can be separated into their own repositories.
The client application is generated using create-react-app
and the API is built on top of Node and Express with a Postgres database.
Both applications come preconfigured with Docker and Docker Compose for running the applications locally in self-contained environments.
Here is a basic overview of the structure and technologies used:
- Client
- Generated using create-react-app with Typescript and SASS.
- Axios for the HTTP client.
- Redux using the ducks modular approach for state management.
- React Router 4 for routing. * Jest and Enzyme for testing.
- API
- Circle CI configuration for continuous integration.
- PassportJS and OAuth2 for authentication.
- Docker and docker compose for running applications locally.
- prettier, editorconfig, and tslint for consistent code formatting.
- Github templates for Github pull request templates.
The boilerplate comes packaged with a docker-compose file preconfigured for hot-reloading.
npm run dev
Running the command above in the root directory starts Docker-ized instances of the client, API, and database.
To see all available npm scripts, run:
npm run
The API and client configuration files share the same structure and can be found in their respective /src/config
folders. In a development environment, values are referenced inside the docker-compose configuration files.
- Create a new project from your Google developer page.
- After creating your project you are redirected to the project Dashboard. Enable Google+ API by clicking on the Library link on the left menu and searching for Google+ API or ENABLE APIS AND SERVICES on the dashboard screen.
- Go to the OAuth consent tab by clicking on the credentials link on the left menu. Click on CREATE CREDENTIAL:
- Which API are you using?
- Google+ API
- Where will you be calling the API from?
- Web server (e.g. node.js, Tomcat)
- What data will you be accessing?
- User data
- Which API are you using?
- After adding this information click on What credentials do I need?
- It'll open the second step to create an OAuth 2.0 client ID:
Name
: your application nameAuthorized JavaScript Origins
:http://localhost:3001
Authorized Redirect URIs
:http://localhost:3001/auth/google/callback
- Click on Create OAuth client ID.
- The third step will open for the consent screen configuration where you need to set your product name. Click on continue after setting the product name.
- In the last step, click on Done. You will be able to copy the client id on the next screen.
- Click on your project name. Copy the Client ID and Client Secret to
GOOGLE_ID
andGOOGLE_SECRET
in both the root level and API docker-compose configuration files.
- Create a new Facebook developer account.
- Click on My Apps and add a new app.
- Set a display name and your contact email.
- Click on the Set Up button under the Facebook Login card on the main dashboard.
- Go to Valid OAuth Redirect URIs and add
http://localhost:3001/api/auth/facebook/callback
. - Click on Save Changes
- Copy the client id and client secret from the left menu opening the Settings item and clicking on Basic
- Set these values to
FACEBOOK_ID
andFACEBOOK_SECRET
in both the root level and API docker-compose configuration files.
- Create a new application from your LinkedIn developer page.
- Set your
LINKEDIN_ID
andLINKEDIN_SECRET
in both the root level and API docker-compose configuration files. - Set the
LINKEDIN_CALLBACK_URL
tohttp://localhost:3001/api/auth/linkedin/callback
inside your LinkedIn application settings.
While the boilerplate’s API was built to use Google, Facebook and LinkedIn’s authentication strategies, it is also easy to integrate other strategies.
First, read the PassportJS documentation. Then, make the following changes:
The authentication process is using the boilerplate API and it should be easy to add other strategies changing the following files:
- passport-initializer
- Update the configuration file with API keys and other configurations required for your strategy.
- Update the
initializePassport
function to in initialize your new strategy.
- authenticate.ts (contains routing for your new strategy)
- Add routes for your service.
- Note: when following O Auth2’s authentication pattern, two paths are required - one for the initial authenticate call and another for the callback. Make sure to use the same name that was set in the passport-initializer file, e.g.
passport.authenticate('facebookProvider')
.
Once everything has been configured, the boilerplate should automatically persist user authentication data to the database and save the token in the local storage.
If you want to create a new protected route follow these steps:
API
Add the ensureAuthenticated
middleware when creating your route, e.g.
router.use('/', asyncHandler(ensureAuthenticated), (_, res) => res.send('AUTHENTICATED').status(200))
The asyncHandler
is a simple middleware for handling exceptions inside of async express routes and passing them to your express error handlers.
Client
Add your route to ProtectedRoutes.tsx
.
The boilerplate can be deployed in any cloud service provider of your choice. For simplicity and ease-of-use, we use Heroku as our cloud service.
Prerequisites:
- Heroku cli
- Heroku account
- This boilerplate relies on an authentication process to work properly. Make sure you have been through the authentication configuration.
- In your project directory, run
heroku login
and enter your credentials. - Run
heroku create APP_NAME
to create your Heroku application. Copy your application URL for later steps. - Navigate to your application in the heroku apps dashboard and go to the Resources tab. Under Add-ons, add a postgres database by searching for postgres in the search field. A
DATABASE_URL
configuration variable is generated upon creation. - Navigate to your application's Settings tab in your heroku dashboard. Click on Reveal Vars and set the following values:
GOOGLE_ID
: the client ID for your application'sGOOGLE_SECRET
: the client secret for your Google appGOOGLE_CALLBACK_URL
:https://[YOUR_HEROKU_APPLICATION_URL].herokuapp.com/api/auth/google/callback
using the URL from step 2.- If you missed your application URL on step two, go down in the Settings tab and look for the Domains and certificates item.
SUCCESS_LOGIN_REDIRECT_URL
:https://[YOUR_HEROKU_APPLICATION_URL].herokuapp.com/connect
Deploy your application to Heroku by running:
git push heroku master
The boilerplate uses CircleCI for automated deployment to Heroku when pushing to your Github master branch.
Prerequisites:
- Heroku application has been set up (Setting up your Heroku application)
- a Circle CI account linked to your GitHub account hosting your project's repository
- Uncomment the deploy step inside
.circleci/config.yml
. - Replace the placeholder
HEROKU_APP_NAME
inside.circleci/deploy-heroku.sh
with your Heroku application name. - Navigate to the Add Projects tab and click on Set Up Project for your project.
- Select Linux as the operating system and Node as language. Then start building the project to launch the project on CircleCI.
- Settings > Projects > Go to your followed project and select the gear icon
- Navigate to Checkout SSH Keys under Permissions in the left menu pane. Authorize your Github to add create a user key for your account.
- Navigate to Heroku Deployment under Continuous Deployment and enter your Heroku API key, which can be found in your Heroku account settings. Then, set the user to your Heroku account.
The boilerplate comes with tests for both the API and client. We highly encourage you to maintain them during development.
You can find API tests in the api/spec
folder and the client tests inside each React components folder.
To run client-side tests,
npm run client-test
To run server-side tests,
npm run api-test-watch