Home > Software design >  Is there any way to ignore backslashes and forward slashes in link route?
Is there any way to ignore backslashes and forward slashes in link route?

Time:08-27

I am making a user Authentication system and it sends users a confirmation email when user registers, the problem I'm facing is that sometimes link contains / and \ .due to this any text after the / was considered as new route. Is there any way to ignore these slashes. btw I'm using hashing to generate the link.

The link is generated by this piece of code.

const hashedEmail = await bcrypt.hash(email,10);
        console.log(await bcrypt.compare(email,hashedEmail))
        const mailOptions = {
            from: '[email protected]',
            to: email,
            subject: 'Confirm your email',
            text : `Thanks for registering! Please click on the link to confirm your email: 
            http://localhost:3500/confirm/${hashedEmail}`
}

confirmation Route

const bcrypt = require('bcrypt');
const fsPromises = require('fs').promises;
const path = require('path');

// user data: 

const userData = {
    users: require('../model/users.json'),
    setUsersData : function(data) {this.users = data}
}

async function confirmationRoute(fastify,options,done){
    fastify.get('/confirm/:id',async(req,res) => {
        const HashedId = req.params.id;
        let foundUser;
        for (let i = 0; i < userData.users.length; i  ) {
            if (await bcrypt.compare(userData.users[i].email,HashedId)){
                foundUser = userData.users[i];
            }
        }
        
        if(foundUser){
            // change isverified to true
            foundUser.isVerified = true;
            // write the new user data to the file
            userData.setUsersData([...userData.users.filter(user => user.email !== foundUser.email),foundUser]);
            await fsPromises.writeFile(path.join(__dirname,'..','model','users.json'),
            JSON.stringify(userData.users)
            );
            res.send({"message":"Account Verified"})
        }
        else{
            res.send({"message":"Invalid Link"})
        }
    })
    done()
}

module.exports = confirmationRoute;

Example Link is : http://localhost:3500/confirm/$2b$10$bWUrZ6d0dQuZ8.e/Hqd..OEIaFN2By3nVE6x6H2sHCyLlA/nTriKO

CodePudding user response:

You need to encode the parameter and decode it before processing it:

const param = encodeURIComponent('hello/world?')
console.log(param)
const data = decodeURIComponent(param)
console.log(data)
  • Related