convert php session to react

Viewed 372

I trying to make a secure react login system but I not sure how do I have to do it so I thought this might work if I ask this question.

I want to convert this simple PHP code to react :

<?php
    session_start();
    if(isset($_SESSION['client'])){
        echo 1;
    }
?>

I kind of did it but I think its not secure or good at all this is my react :

import { useState } from "react";

function App() {
    const [session, setSession] = useState(false)

    const loginToggler = async (username,password) => {
        // Some Fetch with api and check the username and password and get the response of true or false
        const res = !session // const res = await fetch("http://localhost:5000/users", ....
        setSession(res)
    }
    return (
        <div>
            {session ? "You are logged in ":"You aren't logged in "}
            {!session ? <button onClick={loginToggler}>Login</button>:<button onClick={loginToggler}>Logout</button>}
        </div>
    );
}

export default App;

If you know anything better or any suggestion about it please let me know and ty.

1 Answers

Why do you think this isn't safe? It should be safe, however there are better ways to do it.

I would use context.

I recommend you to check this project by WebDevSimplified. He also has a youtube video explaining all process of the development. (He used firebase as his backend)

Related