xxxxxxxxxx
import { useAuth } from "@/context/AuthContext";
import axios, { AxiosError, AxiosRequestConfig } from "axios";
import { useCallback, useEffect, useRef, useState } from "react";
interface FetchState<T> {
data: T | null;
error: string | null;
isLoading: boolean;
}
interface UseFetchOptions extends AxiosRequestConfig {
immediate?: boolean;
onSuccess?: (data: any) => void;
onError?: (error: AxiosError | Error) => void;
cacheKey?: string;
cacheTime?: number;
}
interface Cache {
[key: string]: {
data: any;
timestamp: number;
};
}
const cache: Cache = {};
export function useFetch<T = any>(
url: string,
options: UseFetchOptions = {}
) {
const {
immediate = true,
onSuccess,
onError,
cacheKey,
cacheTime = 5 * 60 * 1000, // 5 minutes default cache time
axiosOptions
} = options;
const { authState } = useAuth();
const [state, setState] = useState<FetchState<T>>({
data: null,
error: null,
isLoading: immediate,
});
const abortControllerRef = useRef<AbortController | null>(null);
const execute = useCallback(
async (overrideOptions: AxiosRequestConfig = {}) => {
// Cancel any ongoing requests
abortControllerRef.current?.abort();
abortControllerRef.current = new AbortController();
// Check cache
if (cacheKey && cache[cacheKey]) {
const { data, timestamp } = cache[cacheKey];
if (Date.now() - timestamp < cacheTime) {
setState({ data, error: null, isLoading: false });
onSuccess?.(data);
return { data, error: null };
}
delete cache[cacheKey];
}
setState(prev => ({ prev, isLoading: true, error: null }));
try {
const config: AxiosRequestConfig = {
axiosOptions,
overrideOptions,
signal: abortControllerRef.current.signal,
headers: {
axiosOptions.headers,
overrideOptions.headers,
authState?.token && { (
Authorization: `Bearer ${authState.token}`,
}),
},
};
const response = await axios(url, config);
const data = response.data;
// Cache the response if cacheKey is provided
if (cacheKey) {
cache[cacheKey] = {
data,
timestamp: Date.now(),
};
}
setState({ data, error: null, isLoading: false });
onSuccess?.(data);
return { data, error: null };
} catch (error) {
const errorMessage =
error instanceof AxiosError
? error.response?.data?.message || error.message
: "An unexpected error occurred";
setState({
data: null,
error: errorMessage,
isLoading: false,
});
onError?.(error as AxiosError | Error);
return { data: null, error: errorMessage };
}
},
[url, authState, cacheKey, cacheTime, onSuccess, onError, axiosOptions]
);
useEffect(() => {
if (immediate) {
execute();
}
return () => {
abortControllerRef.current?.abort();
};
}, [execute, immediate]);
const clearCache = useCallback(() => {
if (cacheKey && cache[cacheKey]) {
delete cache[cacheKey];
}
}, [cacheKey]);
return {
state,
execute,
clearCache,
};
}
xxxxxxxxxx
import React, { Component } from 'react';
import axios from 'axios';
class Login extends Component {
constructor() {
super();
this.state = {
username: '',
password: '',
loggedIn: false
};
}
handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
// Replace 'YOUR_API_ENDPOINT' with the actual endpoint for user authentication.
const apiEndpoint = 'YOUR_API_ENDPOINT';
// Prepare the data to send to the API
const data = {
username: this.state.username,
password: this.state.password
};
// Make an Axios POST request to authenticate the user
axios.post(apiEndpoint, data)
.then((response) => {
// Check if the authentication was successful (you should customize this based on your API response)
if (response.data && response.data.success) {
this.setState({
loggedIn: true
});
}
})
.catch((error) => {
console.error("Authentication failed", error);
// Handle authentication failure, e.g., show an error message to the user
});
}
render() {
if (this.state.loggedIn) {
return (
<div>
<h2>Welcome, {this.state.username}!</h2>
<button onClick={() => this.setState({ loggedIn: false })}>Logout</button>
</div>
);
}
return (
<form onSubmit={this.handleSubmit}>
<label>
Username:
<input
type="text"
name="username"
value={this.state.username}
onChange={this.handleChange}
/>
</label>
<br />
<label>
Password:
<input
type="password"
name="password"
value={this.state.password}
onChange={this.handleChange}
/>
</label>
<br />
<button type="submit">Login</button>
</form>
);
}
}
export default Login;
xxxxxxxxxx
# Temporarily set SELinux to permissive mode
setenforce 0
# Permanently set SELinux to permissive mode
sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
# Verify changes (temporary)
getenforce
# Reboot to apply permanent changes
reboot
xxxxxxxxxx
# Temporarily set SELinux to permissive mode
setenforce 0
# Permanently set SELinux to permissive mode
sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
# Verify changes (temporary)
getenforce
# Reboot to apply permanent changes
reboot
xxxxxxxxxx
sudo setenforce 0
sudo sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config