简体   繁体   English

请帮助我获取哈希密码

[英]Please help me get the hashed password

I'm trying to get the hashed password for the admin. 我正在尝试获取管理员的哈希密码。

I've tried to hash the password but I'm failing to insert it into the user object. 我尝试对密码进行哈希处理,但是无法将其插入到用户对象中。

import encryptor from '../helpers/password';

let hashed_pswd = 'john123';
const hashPassword= async () => {
    const adminPwd = await encryptor.encryptPassword(hashed_pswd, 10);
    console.log(adminPwd);
}
hashPassword();

export default [
    {
        id: 1,
        first_name: 'john',
        last_name: 'doe',
        email: 'john@gmail.com',
        password: adminPwd,
        address: 'kigali',
        is_admin: true
    }
]

I'm being able to log the hashed password in the console but when I try to send a POST request, I'm getting that the adminPwd is not defined. 我能够在控制台中记录哈希密码,但是当我尝试发送POST请求时,却发现未定义adminPwd

Thank you in advance. 先感谢您。

Since you're using async/await for the hashPassword function you may as well wrap your whole code in one and remove that function. 由于您对hashPassword函数使用了async/await ,因此您最好将整个代码包装在一个代码中并删除该函数。 You should rename your hashed_pswd variable to something more meaningful because it's not hashed at that stage. 您应该将您的hashed_pswd变量重命名为更有意义的名称,因为在该阶段还没有对它进行哈希处理。 I've called it password . 我称它为password

import encryptor from '../helpers/password';

export default async () => {

  const password = 'john123';
  const hashedPassword = await encryptor.encryptPassword(password, 10);

  return [{
    id: 1,
    first_name: 'john',
    last_name: 'doe',
    email: 'john@gmail.com',
    password: hashedPassword,
    address: 'kigali',
    is_admin: true
  }];

};

And to import that module you'd have to use async to wrap your code because you're returning a promise from that async in getUserData . 并要import该模块,您必须使用async包装代码,因为您将从getUserDataasync返回一个承诺。

import getUserData from './getUserData.mjs';

(async () => {

  console.log(await getUserData());

})();

In order to solve this you need to declare adminPwd using let instead of const in global scope or in scope where your export default section can access it . 为了解决这个问题,您需要在全局范围内或导出默认部分可以访问它的范围内使用let而不是const声明adminPwd

Refer to modified code snippet:

import encryptor from '../helpers/password';

let hashed_pswd = 'john123';
let adminPwd;
const hashPassword = async () => {
    adminPwd = await encryptor.encryptPassword(hashed_pswd, 10);
            console.log(adminPwd);
            return [
                {
                    id: 1,
                    first_name: 'john',
                    last_name: 'doe',
                    email: 'john@gmail.com',
                    password: adminPwd,
                    address: 'kigali',
                    is_admin: true
                }
            ]
}

export default hashPassword;

Wherever you were importing this, call exported function before using it. 无论您将其导入到哪里,都请在使用它之前调用导出的函数。

In order to Consume this response you have two options:

  1. In a async function using await keyword 在使用await关键字的异步函数中

  2. If not consuming within async function use .then method of promise. 如果不使用异步函数,请使用.then方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM