0

import React from 'react'
import styles from './LoginForm.module.css'
import ReactDOM from 'react-dom';
import { useRef } from 'react';


const Backdrop = () => {
    return (
        <div className={styles.backdrop} />

    )
}
const Login_Card = ({ showFormHandeler }) => {

    const emailRef = useRef();
    const passwordRef = useRef();

    // const C_passwordRef=useRef()
    const onLoginHandeler = async () => {
    const email =emailRef.current.value;
    const password =passwordRef.current.value;

        const user_data = {
            email:email,
            password:password,

        }
   const url = 'http://localhost:5000/login'
        
        const response = await fetch(url, {
            method: 'POST',
            body: JSON.stringify(user_data),
            headers: {
                "Content-type": "application/json; charset=UTF-8"
            }

        })
        const data= await response.json();
        console.log(data);
    }
    return (
        <div className={styles.overlay_container}>
            <div className={styles.form_card}>
                <h1>
                    This is the Login card
                </h1>
                <form>
                    <div className={styles.form_item_group}>
                        <label className={styles.label} ref={emailRef}>Email </label>
                        <input ref={emailRef} />
                    </div>

                    <div className={styles.form_item_group}>
                        <label className={styles.label}>Password  </label>
                        <input ref={passwordRef} />
                    </div>



                    <div  className={styles.form_item_group_btn_container}>
                        <span onClick={onLoginHandeler} className={styles.signup_form_button}>
                            Login
                        </span>
                        <span onClick={showFormHandeler} className={styles.signup_form_button}>
                            cancel
                        </span>
                    </div>


                </form>

            </div>

        </div>
    )
}
function LoginForm({ showFormHandeler }) {
    const overlayRoot = document.getElementById('overlay');
    return (
        <React.Fragment>
            {
                ReactDOM.createPortal(<Backdrop />, overlayRoot)
            }
            {
                ReactDOM.createPortal(<Login_Card showFormHandeler={showFormHandeler} />, overlayRoot)
            }

        </React.Fragment>
    )
}
export default LoginForm

**FRONT OF WEB APP **

I am Supposed to log in the user using React JS front end and Using my Own Node js backed API with JWT token, Now whenever User clicks the the LOGIN Button , our API( 'http://localhost:5000/login' ) will pe called , Which will be POST request , .

On Backend , i will compare email id and password with the encrybted password and get the user if it exsists with given credentials, and attched jwt token with userid .

exports.loginUser = async (req, resp) => {
    console.log("login Req body : ", req.body);

    //  user login
  try {
      const email=req.body.email;
      const password=req.body.password;
      const user= await User.login(email,password);
      const token=createToken(user._id)

      resp.cookie('jwt',token);
      
      resp.status('200').json({id:user._id})



  } catch (error) {
     if(error)
     {
         console.log(error.message);
        resp.status('400').json({message:error.message})
     }
  }

}

But I login user with my front end I didn't receive any JWT token But when i test my API using POSTMAN it shows JWT token Differences in the output of Front end and POSTMAN request

can anyone help me out with this why is this happening?

Gulab_786
  • 21
  • 3
  • See [this answer](https://stackoverflow.com/a/38935838/954940) on the duped question – Adam Apr 09 '22 at 01:58

0 Answers0