Ad

How To Pass Data From Login Api To Profile Api?

I am a beginner in Nodejs and Mongo dB. I am creating a signup/login system with authentication and also I have to make a profile page. I have built a login API here is the code

router.post('/login',async(req,res)=>{
    try {
        const email = req.body.email;
        const password = req.body.password;
        const usermail = await UserSignup.findOne({email:email});
        const isMatch = bcrypt.compare(password,usermail.password);
        if(isMatch){
            const token = generatetoken(usermail._id)
            res.setHeader('JWT_TOKEN',token);
            // res.status(201).json({
            //     message:"User loged in",
            //     userdetail:{
            //         name:usermail.name,
            //         email:usermail.email,
            //         Phone:usermail.mobilenumber,
            //         Id:usermail._Id
            //     }
            // })
            res.redirect('/api/user/welcome',{useremail:email})
        }
        else{
            res.status(400).send("Invalid Credentials")
        }
    } catch (error) {
        console.log(error);
        res.status(500).send("Internal server error")
    }
})

I have redirected the User after logging in to the welcome page. so In the welcome page I have to display user name email. how can I do that?

router.get('/welcome',auth,(req,res)=>{


})

what should I write in the welcome API to get information about the logged user?

I have passed information of the user in 2nd argument of redirect is it correct or not because I can not able to access it in the welcome route.

Ad

Answer

You can use query strings. Modify you login route to redirect to:

res.redirect('/api/user/welcome?email=' + email)

And that you can access the query in the welcome route like so:

let email = req.query.email;
Ad
source: stackoverflow.com
Ad