简体   繁体   English

在 react.js 中设置 bcryptjs 的问题

[英]Issues setting up bcryptjs in react.js

I was trying to setting up a single registration page in a react component but I wanted to hash the password before sending the response to my back-end.我试图在一个反应组件中设置一个注册页面,但我想在将响应发送到我的后端之前输入 hash 密码。

I am confused on why the code below does not work, it returns a promise but it should not as I am wrapping the login in an asynchronous function and gets also awaited.我对为什么下面的代码不起作用感到困惑,它返回一个 promise 但它不应该,因为我将登录包装在一个异步 function 中并且也被等待。

Any help would be greatly appreciated.任何帮助将不胜感激。

import React, { useState, useEffect } from 'react';
import { v4 as uuidv4 } from 'uuid';
/// Error when using bycript ?
var bcrypt = require('bcryptjs');


const Register = () => {

    const [name, setName] = useState('')
    const [email, setEmail] = useState('')
    const [address, setAddress] = useState('')
    const [password, setPassword] = useState('')
    /*
       async function hashPassword(password) {
           const hashedPassword = await bcrypt.hash(password, 10);
           return hashedPassword;
       }
   
       useEffect(() => {
           try {
               const hashedPassword = hashPassword(password);
               setPassword(hashedPassword)
           } catch (error) {
               console.log(error)
           }
           console.log(password)
       }, [password])
   
       TRIED TO USE BCRYPT BUT DID NOT WORK
        */



    async function register() {
        const url = 'http://localhost:5000/customers';
        const object = {
            customer_id: uuidv4(), 
            customer_name: name,
            address: address,
            email: email,
            customer_password: password
        }
        const result = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-type': 'application/json',
                'Accept': 'application/json'
            },
            body: JSON.stringify(object)
        });

        result = await result.json();
    }



    return (
        <>
            <div>
                <h1>Register</h1>
                <div>
                    <label for="customer_name">Name: </label>
                    <input type="text" name="customer_name" id="customer_name"
                        onChange={(e) => setName(e.target.value)} required />
                </div>
                <div>
                    <label for="email">Email: </label>
                    <input type="email" name="email" id="email"
                        onChange={(e) => setEmail(e.target.value)} required />
                </div>
                <div>
                    <label for="address">Address: </label>
                    <input type="text" name="address" id="address"
                        onChange={(e) => setAddress(e.target.value)} />
                </div>
                <div>
                    <label for="password">Create a new password: </label>
                    <input type="password" name="password" id="password"
                        onChange={(e) => setPassword(e.target.value)} required />
                </div>
                <button type="submit" onClick={register}>Register</button>
                <br />
                <p>Copyright 2022 E-Market</p>
            </div>
        </>
    );
};

export default Register;

hashPassword returns a promise that's why it doesn't work. hashPassword返回 promise 这就是它不起作用的原因。
But you shouldn't hash passwords to send to the back-end, the password should only be hashed upon database insertion.但是你不应该将 hash 密码发送到后端,密码应该只在数据库插入时被散列。 More info here .更多信息在这里

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

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