cannot store data to cookies storage

50 Views Asked by At

Can you check my code? i using Hapi , and Hapi cookie. i want to make regist 2FA , which is like this

heres my code

{
    method: "POST",
    path: "/verify",
    handler: async (request, h) => {
      try {
        const dataStorage = {
          firstName: request.payload.firstName,
          lastName: request.payload.lastName,
          username: request.payload.username,
          password: request.payload.username,
          email: request.payload.email,
        };
        const verificationCode = Math.floor(10000 + Math.random() * 90000);
       request.state.dataStorage = dataStorage;
        request.state.verificationCode = verificationCode;
        console.log(request.state.verificationCode)
        return h.file("verification.html")
} catch (error) {
        console.error("Error in route handler:", error);
        return h.response("Internal server error").code(500);
      }
options: {
      state: {
        parse: true,
        failAction: "error",
      },
      auth: {
        mode: "optional",
      },
    },
  },

{
    method: "POST",
    path: "/registrasi",
    handler: async (request, h) => {
      const { userVerificationCode } = request.payload;
      const dataStorage = request.state.dataStorage;
      console.log("dataStorage:", dataStorage);
      const { firstName, lastName, username, password, email } = dataStorage;
      console.log(firstName, lastName, username);
      const verificationCode = parseInt(request.state.verificationCode);
      console.log(verificationCode);
      const parsedUserVerificationCode = parseInt(userVerificationCode);
      console.log(parsedUserVerificationCode === verificationCode);
      if (parsedUserVerificationCode === verificationCode) {
        await createUser(dataStorage);
        request.cookieAuth.set({ username, password });
        return h.redirect("/welcome");
      } else {
        return h.response("Verification code does not match").code(400);
      }
    },
    options: {
      state: {
        parse: true,
        failAction: "error",
      },
    },
  },

the problem is I have a registration form. When I click the 'register' button, the data sent by the user is supposed to be sent to the storage cookie, and the verification code is also supposed to be sent to the cookie storage. But when I check the cookie storage after clicking the 'register' button, there is nothing there. Then, I also have a verification form that contains a verification code. I tried to input the code from the console log, and then an error like this appeared

dataStorage: undefined
    Debug: internal, implementation, error 
        TypeError: Cannot destructure property 'firstName' of 'dataStorage' as it is undefined.
        at handler (E:\coding\test_hapi\source\routes.js:131:15)
        at exports.Manager.execute (E:\coding\test_hapi\node_modules\@hapi\hapi\lib\toolkit.js:57:29)
        at Object.internals.handler (E:\coding\test_hapi\node_modules\@hapi\hapi\lib\handler.js:46:48)
        at exports.execute (E:\coding\test_hapi\node_modules\@hapi\hapi\lib\handler.js:31:36)
        at Request._lifecycle (E:\coding\test_hapi\node_modules\@hapi\hapi\lib\request.js:370:68)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
        at async Request._execute (E:\coding\test_hapi\node_modules\@hapi\hapi\lib\request.js:280:9)

Hopefully u guys understand what i say. sorry if my english bad

0

There are 0 best solutions below