Exercise: Get JWT
Instructions
-
Set up your React application.
Start by setting up a new React application using Create React App or another tool of your choice.
-
Create a Login Component.
This component should contain a form with fields for a username and password, and a button to submit the form.
-
Submit Form Data.
When the user submits the form, send the form data in a POST request to the
/user
endpoint. -
Handle Server Response.
If the username and password are correct, the server will respond with a JWT. Save this token in your application. This could be in the component state or a global state management system like Redux or Context.
-
Store the JWT.
Consider using a browser storage system like LocalStorage or SessionStorage to save the JWT. This way, if the user refreshes the page, the token won't be lost.
-
Authenticated Requests.
Use the stored JWT to authenticate future requests to your server. You will need to include the token in the
Authorization
header of your HTTP requests. -
Create Protected Routes.
Use React Router to create protected routes. These routes should only be accessible if a user is authenticated (i.e. if a JWT exists).
-
Logout Functionality.
Add a logout button or link that, when clicked, removes the JWT from storage and un-authenticates the user.
-
Test Your Application.
Test your application thoroughly. Make sure that only authenticated users can access protected routes and that logging out properly un-authenticates the user.