Skip to main content

Command Palette

Search for a command to run...

Day 24/25 (30 Days of Code)

Updated
2 min read
Day 24/25 (30 Days of Code)
N

I am Nirbhay Singh , I am starting this blog to document my coding journey of becoming a software developer and to get my first $ 100k offer .

28 July 2023

I am about to complete my first MERN project. I am just having some issues on the client side. I am trying to solve them but I have been stuck for a while. I am unable to store jwt token in the cookies which is causing me issues with the authentication of users for a specific page.

What did I learn?

Don't follow old tutorials.

Progress as of today

Image1

Image2

Image3

As you can see I am able to send data to the database via client and I am also able to generate token. But somehow I am having trouble storing it on cookies.

Image4


How to use Fetch in React to make a Post request to a Server

const handleSubmit = async (event) =>{
        event.preventDefault();

        const res = await fetch("http://localhost:5000/register", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            credentials: "include",
            body: JSON.stringify({
                fname, lname, email, password, cpassword
            })
        });

        const data = await res.json();

        if(res.status===422 || !data){
            window.alert("Invalid Registration")
        }else{
            window.alert("Registration succesfull");
            navigate("/login");
        }
    }

Full Code

import React,{useState} from "react";
import "../styles/Form.css";
import {Link, Navlink, useNavigate} from "react-router-dom";

const Signup = () =>{

    const navigate = useNavigate();
    const [user, setUser] = useState({
        fname: "",
        lname: "",
        email: "",
        password: "",
        cpassword: ""
    });


    const handleChange = (event) =>{
        const { name, value} = event.target;

        setUser(prevValue => {
            return (
                {...prevValue,
                [name]: value}
            )

        })  
    }

    const handleSubmit = async (event) =>{
        event.preventDefault();

        const { fname, lname, email, password, cpassword } = user;

        const res = await fetch("http://localhost:5000/register", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            credentials: "include",
            body: JSON.stringify({
                fname, lname, email, password, cpassword
            })
        });

        const data = await res.json();

        if(res.status===422 || !data){
            window.alert("Invalid Registration")
        }else{
            window.alert("Registration succesfull");
            navigate("/login");
        }
    }

    return (
        <div className="form-container">
            <h1>
                Start Your Journey !
            </h1>
            <form method="POST" onSubmit={handleSubmit}>
                <input type="text" name="fname" id="" placeholder="First Name" value={user.fname} onChange={handleChange}/><br></br>
                <input type="text" name="lname" id="" placeholder="Last Name" value={user.lname} onChange={handleChange}/><br></br>
                <input type="email" name="email" id="" placeholder="Email" value={user.email} onChange={handleChange}/><br></br>
                <input type="password" name="password" id="" placeholder="Password" value={user.password} onChange={handleChange}/><br></br>
                <input type="password" name="cpassword" id="" placeholder="Confirm Password" value={user.cpassword} onChange={handleChange}/><br></br>
                <button tupe="submit">Register</button>
                <p>Already registerd ?</p>
                <Link to="/login">Login</Link>
            </form>
        </div>
    )
}

export default Signup;

30 Days of Code

Part 6 of 28

I created this series to document my regressive learning of 30 days of my summer breaks of 2023. Contents of this series will not follow any pattern. It will a mixture of DSA and web dev.

Up next

Day 23 (30 Days of Code)

26 July 2023 I am still working on the project I have started recently. I am facing some issues but I am doing some progress. Apart from that I solved some DSA problems. Minimum Path Sum My approach: As usual, my very first approach was a brute fo...

More from this blog

Daily Code by Nirbhay

74 posts

Hey, this is Nirbhay. I started this blog to document my journey of learning to code and get my first $100k offer. I'll be sharing the things related to DSA, backend development, devops and many more.