Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

javascript - Invalid csrf token after using router.push - next.js

//server.js
const csrf = require("csurf");

const csrfProtection = csrf({
  cookie: true,
});

app.get("/api/csrf-token", csrfProtection, (req, res) => {
  res.json({ csrfToken: req.csrfToken() });
});

app.post("/api/signup/stepTwo", csrfProtection, async (req, res) => {
   try {
     const savedCity = await knex("UserCity").insert([
       {
         Street: req.body.street,
         City: req.body.city,
       },
     ]);

     if (savedCity) {
       return res.json({
         message: "Answers saved!",
       });
     } else {
       return res.status(400).json({
         message: "There was a problem saving your answers",
       });
     }
   } catch (err) {
     return res.status(400).json({
       message: "There was a problem saving your answerst",
     });
   }
  }
 );
 
 
 //stepOne - submit credentials
 const submitCredentials = async (credentials) => {
     //api call to submit credentials...
     
 const router = useRouter();
  useEffect(() => {
    if (redirectOnLogin === true) {
      router.push("/signup/stepTwo");  
    }
  });
  
  return (...
  )
 }
 
 //stepTwo
 const submitAnswers = async (answers) => {
    try {
      const { data } = await fetchContext.authAxios.post(
        `signupStepTwo`,
        answers
      );
      setTimeout(() => {
        setRedirectOnLogin(true);
      }, 700);
    } catch (error) {
      const { data } = error.response;
    }
  };
  
 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...