Home > Blockchain >  Post requesting to another route, while redirecting the client there. express
Post requesting to another route, while redirecting the client there. express

Time:10-02

I want to create a page where the register and login button post to one url, this url then verifies if the account exists, if not register, but after the registration i want to post to /login so he also instantly gets logged in.

This is my code right now, but idk how to post to /login using the user response so he also gets logged in using passport, i tried axios post requesting, but that didn't work.

app.post('/auth', (req, res) => {
   const table    = new quickdb.table('Users');
   const allUsers = table.all()
   if (allUsers.filter(userTmp => userTmp.email == req.body.email && userTmp.username == req.body.username)).length == 0) {
       // Register in database etc
       // TODO: Post to /login to log the user in using passport
   } else {
       // TODO: Post to /login to log the user in using passport
   }
})

app.post('/login', passport.authenticate('local', {
    failureFlash: true,
    failureRedirect: '/',
    successRedirect: '/',
}));

CodePudding user response:

Write a function to handle Login process, and in the routes just call that function.

function myLoginHandler() {
  return passport.authenticate('local', {
    failureFlash: true,
    failureRedirect: '/',
    successRedirect: '/',
  })
}
app.post('/auth', (req, res) => {
   const table    = new quickdb.table('Users');
   const allUsers = table.all()
   if (allUsers.filter(userTmp => userTmp.email == req.body.email && userTmp.username == req.body.username)).length == 0) {
       // Register in database etc
   }
   return myLoginHandler();
   }
})


app.post('/login', myLoginHandler);
  • Related