Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading