In repo react-starter branch auth0 I showed how can be used Auth0 for authentication. In the case you prefer AWS Cognito you can check here how can be done.
Features:
- AWS Amplify to comunicate with Cognito
AmplifyAuthenticator
component from library@aws-amplify/ui-react
React.useContext
to hold authentication state and data- Components
AuthenticatedRoute
andUnauthenticatedRoute
protect routes according user authentication state - Redirect: If an unauthenticated user tries to go to an
AuthenticatedRoute
, first is directed to login and once logged in, it redirects to the intended page
-
Use Node 14 version, using nvm you can:
# set Node 14 in current terminal nvm use 14 # set Node 14 as default (new terminals will use 14) nvm alias default 14
-
Install dependencies
npm ci
-
Deploy your Cognito user and identity pool
dev
stage as in this repo. Once that is deployed you will need to set their values in your.env.local
file:REACT_APP_STAGE=dev REACT_APP_dev_COGNITO_REGION=<your_data> REACT_APP_dev_COGNITO_USER_POOL_ID=<your_data> REACT_APP_dev_COGNITO_APP_CLIENT_ID=<your_data> REACT_APP_dev_COGNITO_IDENTITY_POOL_ID=<your_data>
In the case you change these env vars, you have to restart your React server and refresh the page in order to have them refreshed.
-
Run the app
npm start