Ниже приведена базовая логика кода.
Код: Выделить всё
// Login.js
const Login = (props) => {
const [credentials, setCredentials] = useState({loginusername: "", loginpassword: ""})
// set a template of the user's login credential information: loginusername and loginpassword
const handleSubmit = async (e) => {
e.preventDefault();
// getting data from the backend login.php
const response = await fetch('http://...com:8000/Login.php', {
// recall: app.use('/api/auth', require('./routes/auth'))
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
mode: 'cors',
body: JSON.stringify({loginusername: credentials.loginusername, loginpassword: credentials.loginpassword})
// setting up values obtained from the user's form
});
const json = await response.json()
// const response = ...
// const json = ...
console.log(json);
if (json.success){ // if the user is successfully logged in
localStorage.setItem('token', json.authtoken);
setCredentials({ loginusername: "", loginpassword: "" });
alert("Successfully Logged in!");
}
else{
alert("Invalid credentials");
}
}
Код: Выделить всё
//Login.php
Код: Выделить всё
//AddEvent.js
const handleSubmit = async (e) => {
e.preventDefault();
// getting data from the backend AddEvent.php
const response = await fetch("http://...:8000/AddEvent.php", {
// recall: app.use('/api/auth', require('./routes/auth'))
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({content: event.content, title: event.title, type: event.type})
// setting up values obtained from the user's form
});
const json = await response.json()
// const response = ...
// const json = ...
console.log(json);
if (json.success){ // if the user is successfully logged in
localStorage.setItem('token', json.authtoken);
setevent({ content: "", title: "", type: ""});
alert("Successfully Added Event!");
}
else{
alert("Invalid event content");
}
}
Код: Выделить всё
//AddEvent.php
Один конкретный пример:
Когда пользователь входит в систему :
{успех: true, сообщение: «Вход успешен», имя пользователя: «bobby», идентификатор: 5, phpSessionId: «s1dfp2197id9n6n0d0pce5v5a0»
Когда пользователь добавляет событие :
{успех: false, сообщение: «Несанкционированный доступ», имя пользователя: null, id: null, phpSessionId: 'dtitrs2dm9mq9d0gk7ca5ot1dp'
Нет перезагрузки страницы, и пользователь не заходит на разные веб-сайты.
Подробнее здесь: https://stackoverflow.com/questions/781 ... age-reload