- Read the following steps in full before writing any code.
- Most of the code for the UI has already been built, but is missing functionality with the backend.
- Go through the code in the
src folderto get an idea of the task and project structure. - Once you are ready, go ahead and solve the following steps.
- Open
Register.jscomponent in the pages folder. - import the
registerfunction from theapi/auth.js. - Use the
useMutationhook with theregisterfunction by passinguserInfo. - go to the
api/auth.jsfile and inside theregisterfunction add the formData.
const formData = new FormData();
for (const key in userInfo) formData.append(key, userInfo[key]);- then pass the
formDatainstead of userInfo to the api request - check that the register is working by going to the users tap
- Open
Login.jscomponent in the pages folder. - import the
loginfound in theapi/auth.jsUse theuseMutationhook with theloginfunction and pass it the user info.
- you should see the token in the response
- In
auth.jsfile create a function setUser that saves your token in thelocalStorage - In your
api/index.js, use yourinstanceto create an interceptor using theinterceptors.request.use()method with an anonymous function. We will check if the response has a property calledtoken. Save the the data with to a variable calledtoken. Save the token usinglocalStorage.setItemwith the keytokenand the valuetoken
- go to
auth.jsin the api folder - create a function
logoutthat resets the user to null and deletes the token from the instance header.