亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在單獨的輔助函數中定義 userData?

如何在單獨的輔助函數中定義 userData?

溫溫醬 2023-05-25 18:07:09
我是初學者。我想將加密函數 (encryption.js) 與 authentication.js 文件分開,但出現錯誤“ReferenceError: userData is not defined”。如何在單獨的輔助函數中定義 userData?authentiocation.js:const express = require('express');const router = express.Router();const User = require('../models/user');const jwt = require('jsonwebtoken');const encryption = require("../helpers/encryption").encryption;router.post('/register', (req, res) => { // rejestracja  const userData = req.body;  User.findOne({ email: userData.email }, (error, user) => {   (...)      else {                encryption(); // <-----------------        const user = new User({          firstname: userData.firstname,          email: userData.email,          surname: userData.surname,          password: encrypted        });        user.save((error, registeredUser) => {          if (error) {            res.status(401).send('B??d rejestracji!')          } else {            const firstname = user.firstname;            surname = user.surname;            email = user.email;            payload = { subject: registeredUser._id };            token = jwt.sign(payload, 'secretKey');            res.status(200).send({ token, firstname, surname, email });          }加密.js:const express = require('express');function encryption() {    const crypto = require('crypto');    const algorithm = 'aes-192-cbc';    // Key length is dependent on the algorithm. In this case for aes192, it is    // 24 bytes (192 bits).    // Use async `crypto.scrypt()` instead.    const key = crypto.scryptSync(userData.password, 'salt', 24);    // Use `crypto.randomBytes()` to generate a random iv instead of the static iv    // shown here.    const iv = Buffer.alloc(16, 0); // Initialization vector.    const cipher = crypto.createCipheriv(algorithm, key, iv);    let encrypted = '';    cipher.on('readable', () => {        let chunk;        while (null !== (chunk = cipher.read())) {            encrypted += chunk.toString('hex');        }    });
查看完整描述

1 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

您可以將其作為參數傳遞給加密函數(在encryption.js文件中):


function encryption(userData) {

? ? ...

? ? // at the end:

? ? return encrypted;

}

然后像這樣調用它authentiocation.js(你是說這里的身份驗證嗎?):


const encrypted = encryption(userData);

查看完整回答
反對 回復 2023-05-25
  • 1 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號