Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /var/www/html/beximco/mrp_process/src/pages/ drwxr-xr-x | |
| Viewing file: Select action/file-type: import "../index.css";
import { useRef, useState, useEffect, useContext } from "react";
import { Link, useNavigate, useLocation } from "react-router-dom";
import axios from "axios";
import swal from "sweetalert";
import AuthContext from "./AuthProvider";
import useAuth from "./useAuth";
// import axios from "../api/axios";
// const LOGIN_URL = "/auth";
const token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NCwiaWF0IjoxNjYyNTQ1NjY3LCJleHAiOjE5NzgxMjE2Njd9.5lVlCpiqLqBmdASH-06JubosdJ4jmHwqkepXIkaGr3E";
function Login() {
const { setAuth } = useAuth();
const navigate = useNavigate();
const location = useLocation();
const from = location.state?.from?.pathname || "/";
// const { setAuth } = useContext(AuthContext);
const userRef = useRef();
const errRef = useRef();
const [user, setUser] = useState("");
const [pwd, setPwd] = useState("");
const [errMsg, setErrMsg] = useState("");
const [success, setSuccess] = useState(false);
useEffect(() => {
userRef.current.focus();
}, []);
useEffect(() => {
setErrMsg("");
}, [user, pwd]);
const handleSubmit = async (e) => {
e.preventDefault();
try {
// const response = await axios.post(
// LOGIN_URL,
// JSON.stringify({ user, pwd }),
// {
// headers: { "Content-Type": "application/json" },
// withCredentials: true,
// }
// );
var response = await axios.get(
`http://127.0.0.1:1337/logins?user=${user}&pwd=${pwd}`,
{
headers: {
Authorization: "Bearer " + token,
},
}
);
if (response.data.length > 0) {
console.log(JSON.stringify(response?.data[0]));
//console.log(JSON.stringify(response));
const accessToken = response?.data[0]?.accessToken;
const roles = response?.data[0]?.roles;
setAuth({ user, pwd, roles, accessToken });
setUser("");
setPwd("");
// setSuccess(true);
navigate(from, { replace: true });
} else {
setErrMsg("Login Failed");
}
} catch (err) {
if (!err?.response) {
setErrMsg("No Server Response");
} else if (err.response?.status === 400) {
setErrMsg("Missing Username or Password");
} else if (err.response?.status === 401) {
setErrMsg("Unauthorized");
} else {
setErrMsg("Login Failed");
}
errRef.current.focus();
}
};
return (
<>
{success ? (
<section>
<h1>You are logged in!</h1>
<br />
<p>
<a href="#">Go to Home</a>
</p>
</section>
) : (
<section className="bg-home bg-circle-gradiant d-flex align-items-center">
<div className="bg-overlay bg-overlay-white"></div>
<div className="container">
<div className="row justify-content-center">
<div className="col-lg-5 col-md-8">
<div className="card login-page bg-white shadow rounded border-0">
<div className="card-body">
<p
ref={errRef}
className={errMsg ? "errmsg" : "offscreen"}
aria-live="assertive"
>
{errMsg}
</p>
<h4 className="card-title text-center">Login</h4>
<form className="login-form mt-4" onSubmit={handleSubmit}>
<div className="row">
<div className="col-lg-12">
<div className="form-group position-relative">
<label>
Your Email <span className="text-danger">*</span>
</label>
<i
data-feather="user"
className="fea icon-sm icons"
></i>
<input
className="form-control pl-5"
type="text"
id="username"
ref={userRef}
autoComplete="off"
onChange={(e) => setUser(e.target.value)}
value={user}
required
/>
</div>
</div>
<div className="col-lg-12">
<div className="form-group position-relative">
<label>
Password <span className="text-danger">*</span>
</label>
<i
data-feather="key"
className="fea icon-sm icons"
></i>
<input
className="form-control pl-5"
type="password"
id="password"
onChange={(e) => setPwd(e.target.value)}
value={pwd}
required
/>
</div>
</div>
<div className="col-lg-12">
<div className="d-flex justify-content-between">
<div className="form-group">
<div className="custom-control custom-checkbox">
<input
type="checkbox"
className="custom-control-input"
id="customCheck1"
/>
<label
className="custom-control-label"
htmlFor="customCheck1"
>
Remember me
</label>
</div>
</div>
<p className="forgot-pass mb-0">
<a
href="#"
className="text-dark font-weight-bold"
>
Forgot password ?
</a>
</p>
</div>
</div>
<div className="col-lg-12 mb-0">
<button className="btn btn-primary btn-block">
Sign in
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
)}
</>
);
}
export default Login;
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0053 ]-- |