0

I have connected to an API to make users log in if they have an account. However, I have other pages which I want them to be logged in in order to use. How would I check to make sure a certain user is logged in to use certain features? Is there any source someone can guide me to? Any help would be appreciated.

Thanks,

import React, { useState, useEffect } from "react";
import ForgotPassword from "./ForgotPassword";
import TermsOfService from "./TermsOfService";
import axios from "axios";

function Login() {
  const [user, setUser] = useState("");
  const [userName, setUserName] = useState("");
  const [password, setPassword] = useState("");

  function handleLogin() {
    var config = {
      method: "post",
      url: "http://localhost:8080/api/account/login",
      data: {
        username: userName,
        password: password,
      },
    };

    axios(config)
      .then(function (response) {
        setUser(response.data);
        localStorage.setItem("user", JSON.stringify(response.data));
        console.log(response.data);
      })
      .catch(function (error) {
        console.log(error);
      });
  }
  return (
    <>
      <Navbar />
      <div className="login_form">
        <input
          className="form_input"
          type="text"
          placeholder="Username or Email"
          onChange={(e) => setUserName(e.target.value)}
        />

        <input
          className="form_input"
          type="password"
          placeholder="Password"
          onChange={(e) => setPassword(e.target.value)}
        />

        <button type="submit" onClick={handleLogin} className="Signup_button">
          Login
        </button>
      </div>
    </>
  );
}

export default Login; 
coder75
  • 11
  • 2
  • `react-client-session` https://stackoverflow.com/a/65280102/5334486 – GrafiCode Jul 30 '22 at 08:45
  • @GrafiCode Sorry maybe I forgot to include this but other pages API doesn't check to make sure the user is logged in, So I would need to save to memory and on top of that have other pages verify on the front end that to use a certain feature, they must be logged in. – coder75 Jul 30 '22 at 09:00

0 Answers0