In this lesson, we will learn how to create components for authentication on the front end. The end result, will be similar to the react-auth-template you will be provided for your third and fourth projects.
By the end of this, developers should be able to:
- Create SignUp, SignIn, ChangePassword, and SignOut auth components.
- Prevent viewing components until the user is signed in.
- Style components using React Bootstrap.
- Make axios requests using async & await.
- Fork and clone this repository. FAQ
- Create and checkout to a new branch,
training
, for your work. - Install dependencies with
npm install
. - Start application with
npm start
.
To make our application unique, but still look good we will use a color palette.
Explore these color palettes and choose one you like.
In index.scss
, set the $primary
, $secondary
, and $info
bootstrap colors to colors from your palette.
Here's an example:
// Add BEFORE Bootstrap is imported
$primary: #f4a261;
$secondary: #e9c46a;
$info: #2a9d8f;
Note: If your color has a green, you might also set
$success
. If your palette has a red, you might set$danger
- In
movies/Home.js
create aHome
function component. - Make it display the following text
"The best movie website around"
. - In
App.js
add a Route show theHome
component at the path/
We will create a handful of front-end routes that display different components for user actions.
Endpoint | Component | Must Be Signed In? |
---|---|---|
/sign-up |
SignUp |
No |
/sign-in |
SignIn |
No |
/change-password |
ChangePassword |
Yes |
/sign-out |
SignOut |
Yes |
There is no HTTP verb listed because these are all front-end routes handled by
React. Some of these routes should not be available unless a user is signed in,
so they will redirect to the /
page if not signed in.
Together we will add a Header
component to our app.
It will show the appropriate navigation links when we are signed out or signed in.
Now it's your turn to write a SignUp component!
As a team:
- Create a
function
component calledSignUp
inauth/SignUp.js
(not a class component) - in
App.js
add a route to show theSignUp
component at the path/sign-up
- Set up three pieces of state for the
email
,password
, andpasswordConfirmation
- Create a form to enter the
email
,password
, andpasswordConfirmation
- When the form is submitted make a POST /sign-up request using axios
Bonus:
- After a user is created, navigate to the home page.
Together let us refactor our axios call into the api/auth.js
file.
We'll use named exports, so that we can export multiple auth functions from the same file.
Now we will style our SignUp component using a React Bootstrap form.
To simplify our code, we will refactor our promises into the async
& await
syntax.
Now it's your turn to write a SignIn component!
As a team:
- Create a
function
component calledSignIn
inauth/SignIn.js
(not a class component) - in
App.js
add a route to show theSignIn
component at the path/sign-in
- Set up two pieces of state for the
email
andpassword
- Create a bootstrap
Form
to enter theemail
andpassword
- When the form is submitted make a POST /sign-in request using axios
- After a user is signed in, navigate to the home page.
Bonus:
- After signing in, use the
setUser
function to store theuser
.
Hint: You'll need to make sure to pass down the
setUser
function as a prop toSignIn
inApp.js
In SignUp.js
, we will automatically sign in the user after signing up.
Now it's your turn to write a ChangePassword component!
As a team:
- Create a
function
component calledChangePassword
inauth/ChangePassword.js
(not a class component) - in
App.js
add a route to show theChangePassword
component at the path/change-password
.⚠️ Note: Make sure to pass down theuser
as a prop for theirtoken
. - Set up two pieces of state for the
oldPassword
andnewPassword
- Create a bootstrap
Form
to enter theoldPassword
andnewPassword
- When the form is submitted make a PATCH /change-password request using axios
- After the password is changed, navigate to the home page.
Bonus:
- If the user isn't signed in, navigate to the home page.
Together we will create a SignOut component that makes an axios call to POST /sign-out.
We will create a component that displays user messages.
This component will be written in
src/components/AutoDismissAlert/AutoDismissAlert.js
. A single
component instance is used to manage all alerts application-wide.
To display messages, we will create an alertMsg
function together. The alert can be used by passing the alertMsg
method to a rendered route. The
alertMsg
method expects an object with a heading
, message
, and a variant
property.
Use this component in conjunction with the messages.js
file in the same
directory to create and manage all of your application messages in one place.
The variant
property must be a Bootstrap alert variant, as this component is merely a
wrapper around the react-bootstrap Alert
component. The types it
will accept are: 'primary'
, 'secondary'
, 'success'
, 'danger'
, 'warning'
, 'info'
,
'light'
, and 'dark'
.
- Browser
- cURL / postman
- React
- Browser
- cURL / postman
- React
Note: Don't worry about editing or deleting yet. You can only edit/delete movies you have created.
- cURL
- React
- cURL
- React
- cURL
- React
Developers should run these often!
npm run start
: generates bundles, watches, and livereloads.npm run deploy
: builds and deploys main branchnpm run nag
: runs code quality analysis tools on your code and complains.npm run make-standard
: reformats all your code in the JavaScript Standard Style.
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.