最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use base64 encode in node? - Stack Overflow

programmeradmin2浏览0评论

So I wanted to have email confirmation and stuff on page but putting userid as parameter is not the best idea so I tried to encode this in base64, but I am dumb and something went wrong and even though I don't get any errors, my base64 encoded string looks like this: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA(Here's proof: 1), I get Deprecation warning: (node:32320) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead., and for no reason my db doesn't get new account. Here's encode

app.get('/account/email_confirmation', async (req, res) => {
  let temporary_user_id = req.session.temporary_user_id;
  let buffer = new Buffer(temporary_user_id);
  let base64 = buffer.toString('base64');
  console.log(temporary_user_id, base64);
  let email = {
    from: process.env.EMAIL,
    to: req.session.email,
    subject: 'Your .whiteboard account confirmation',
    text: `http://127.0.0.1:8000/confirm/${base64}`
  };
  transporter.sendMail(email, async (err, info) => {
    if(err) throw err;
    console.log(info.response);
    let theme = await get_theme(req)
    res.render('send_email', {theme: theme})
  })
})

And here's decode:

app.get('/confirm/:userID', async (req,res) => {
  let userID = req.params.userID;
  let buffer = new Buffer(userID, 'base64');
  let str = buffer.toString('ascii');
  let query = "SELECT verified FROM verified WHERE id = ?;";
  con.query(query, [str], async (err, result) => {
    console.log('aaaaaaaaaaaa')
    if(err) throw err;
    console.log(result[0].verified);
    if(result[0].verified == 0){
      console.log('bbbbbbbbbbbbbbb')
      query = 'UPDATE verified SET verified = 1 WHERE id = ?'
      let theme = await get_theme(req);
      con.query(query, [str], (err) => {
        if(err) throw err;
        res.render('confirm', {theme: theme});
      })
    }else{
      res.redirect('/account/login');
    }
  })
})

So I wanted to have email confirmation and stuff on page but putting userid as parameter is not the best idea so I tried to encode this in base64, but I am dumb and something went wrong and even though I don't get any errors, my base64 encoded string looks like this: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA(Here's proof: 1), I get Deprecation warning: (node:32320) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead., and for no reason my db doesn't get new account. Here's encode

app.get('/account/email_confirmation', async (req, res) => {
  let temporary_user_id = req.session.temporary_user_id;
  let buffer = new Buffer(temporary_user_id);
  let base64 = buffer.toString('base64');
  console.log(temporary_user_id, base64);
  let email = {
    from: process.env.EMAIL,
    to: req.session.email,
    subject: 'Your .whiteboard account confirmation',
    text: `http://127.0.0.1:8000/confirm/${base64}`
  };
  transporter.sendMail(email, async (err, info) => {
    if(err) throw err;
    console.log(info.response);
    let theme = await get_theme(req)
    res.render('send_email', {theme: theme})
  })
})

And here's decode:

app.get('/confirm/:userID', async (req,res) => {
  let userID = req.params.userID;
  let buffer = new Buffer(userID, 'base64');
  let str = buffer.toString('ascii');
  let query = "SELECT verified FROM verified WHERE id = ?;";
  con.query(query, [str], async (err, result) => {
    console.log('aaaaaaaaaaaa')
    if(err) throw err;
    console.log(result[0].verified);
    if(result[0].verified == 0){
      console.log('bbbbbbbbbbbbbbb')
      query = 'UPDATE verified SET verified = 1 WHERE id = ?'
      let theme = await get_theme(req);
      con.query(query, [str], (err) => {
        if(err) throw err;
        res.render('confirm', {theme: theme});
      })
    }else{
      res.redirect('/account/login');
    }
  })
})
Share Improve this question asked Apr 6, 2021 at 11:49 user15501498user15501498 311 gold badge2 silver badges6 bronze badges 2
  • what's the output of console.log(temporary_user_id)? – jps Commented Apr 6, 2021 at 12:00
  • Number higher than 1 – user15501498 Commented Apr 6, 2021 at 12:38
Add a ment  | 

1 Answer 1

Reset to default 5

so first of all you have used new Buffer which is already deprecated, so i suggest you to use this.

 const base64data = Buffer.from('someText').toString('base64')

and to decode it just use

Buffer.from(base64data, 'base64').toString('ascii')

As far as it goes to your code, its not wrong base64 conversion is right, may be there can be issue in your temporary_user_id, because of that you are getting some awkward text but conversion is done right afaik.

Just do this and it will solve the issue.

JSON.stringify(temporary_user_id)

The way you are telling error is because it expect a string so you have to convert it to a string. This will solve your issue.

发布评论

评论列表(0)

  1. 暂无评论