In this challenge I will create a login page and request a token from the server that I'll use to send all other requests to the server. I will then be able to fetch the article data array, update data, and delete data.
- Run npm install to install the dependencies.
- Run npm start to run the frontend and backend code automatically.
- Note the backend code will run automatically when the run npm start. There is no need to seperately run a server.js file and no means to test the server through postman or the browser. Feel free to ignore any messages related to MSW or mock service workers.
In this project, I will demonstrate my mastery of these skills by creating a login page and basic CRUD application. I will implement basic security using token authentication and build private routes within the application.
-
[POST] * to
http://localhost:5000/api/login: returns a the current authenication information of the user. Pass in the following credentials as thebodyof the request:{ username: 'Fullstack-decal', password: 'Berkeley' }for a successful login. -
[POST] * to
http://localhost:5000/api/logout: returns the expired authentication information of the user. -
[GET] to
http://localhost:5000/api/articles: returns the all articles currently available. This endpoint can only be accessed by an authenticated user. -
[GET] to
http://localhost:5000/api/articles/:id: returns a single article with the id. This endpoint can only be accessed by an authenticated user. -
[POST] to
http://localhost:5000/api/articles: creates a article object. Returns all available articles. Pass the article as thebodyof the request. This endpoint can only be accessed by an authenticated user. -
[PUT] to
http://localhost:5000/api/articles/:id: updates the article using theidpassed as part of the URL. Returns all available articles. Send the updated article object as thebodyof the request. This endpoint can only be accessed by an authenticated user. -
[DELETE] to
http://localhost:5000/api/articles/:id: removes the article with theidreferenced. Returns all available articles. This endpoint can only be accessed by an authenticated user.
{
id: 'aMqwd', //unique article id
headline: "headline", //title of article
createdOn: '2021-08-09T18:02:38-04:00
', //timestamp of when article was added
summary: "summary", //short summary statement of article
body: "" //paragraph of article text
}
See reference materials at the bottom of the this document for API Reference Details.
Build the needed utilities to restrict access to private routes.
- Build a
Routecomponent that renders renderingLogin.jsto the path/. - Build a
Routecomponent that renders renderingLogin.jsto the path/login. - Build a
Routecomponent that renders renderingView.jsto the path/view. - Build a
Routecomponent that renders renderingLogout.jsto the path/logout.
Build a login form to authenticate Ir users along with all the components and utilities needed to support authentication.
- In
Login.js, build all UI and state functionality needed to capture a username and password. On a successful login, redirect user to theView.jscomponent. - Make sure that the input for your username and password includes the id="username" and id="password" attributes. Codegrade autotests will fail without them.
- Make sure that the submit button to your login form includes the id="submit" attribute. Codegrade autotests will fail without them.
- In
Login.js, add a p tag that will display an error if one occurs. Add in all state functionality needed to support error display. - Make sure your error p tag has an id="error" attribute attached. Codegrade autotests will fail without them.
- Construct an http request that retrieves an auth token from the server when the username
Lambdaand the passwordSchoolis passed into the request. Complete successful login auth flow and redirect toView.js. - Display an appropriate error message when login is not successful.
- Build a
PrivateRoutecomponent within the components folder. - Use the
PrivateRoutecomponent to build a route renderingView.jsto the path/view. - Use the
PrivateRoutecomponent to build a route renderingLogout.jsto the path/logout.
Complete the requests needed to execute all CRUD functions.
-
Build a
axiosWithAuthmodule within the utils folder to create an instance of axios with the authentication header. -
In
View.js, completehandleDeleteso that a http request is made that deletes the article with the included id. After successfully deleting the article on the api, update local state to reflect these changes. -
In
View.js, completehandleDeleteso that a http request is made that deletes the article with the included id. After successfully deleting the article on the api, update local state to reflect these changes. -
editIdis passed into theEditFormcomponent. InEditForm.js, make a http request on mount to get the article with the ideditId. Save the result in state. -
In
View.js, completehandleEditso that a http request is made that updates the passed in article. Set the editing state to false when the request is complete. After successfully deleting the article on the api, update local state to reflect these changes.
Add in the http requests needed to logout of the application.
- In
Logout.js, execute a http request to logout on mount. When the request is complete, the user's security token should be removed and the browser should redirect to the login page.