local login now functional
This commit is contained in:
parent
3b62d87d31
commit
ae9abaf9fd
9 changed files with 174 additions and 72 deletions
|
|
@ -2,7 +2,7 @@ import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
|
|||
import moment from "moment";
|
||||
import { createContext, PropsWithChildren, useContext } from "react";
|
||||
import PondProvider from "./pond/pond";
|
||||
import { useAuth0 } from "@auth0/auth0-react";
|
||||
import { useAuthContext } from "./authContext";
|
||||
import SnackbarProvider from "./Snackbar";
|
||||
|
||||
interface IHTTPContext {
|
||||
|
|
@ -30,7 +30,7 @@ export const HTTPContext = createContext<IHTTPContext>({} as IHTTPContext);
|
|||
|
||||
export default function HTTPProvider(props: Props) {
|
||||
const { children, token } = props;
|
||||
const { isAuthenticated, loginWithRedirect } = useAuth0();
|
||||
const { isAuthenticated, loginWithRedirect } = useAuthContext();
|
||||
|
||||
const defaultOptions = (demo: boolean = false) => {
|
||||
if (demo || !isAuthenticated || !token) {
|
||||
|
|
@ -42,7 +42,7 @@ export default function HTTPProvider(props: Props) {
|
|||
}
|
||||
|
||||
const config = {
|
||||
headers: {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
|
|
@ -50,31 +50,24 @@ export default function HTTPProvider(props: Props) {
|
|||
return config;
|
||||
};
|
||||
|
||||
function isTokenExpired(token: string): boolean {
|
||||
function isTokenExpired(token: string | undefined): boolean {
|
||||
if (!token) return true;
|
||||
try {
|
||||
// Split the token and decode the payload (second part)
|
||||
const payloadBase64 = token.split('.')[1];
|
||||
const decodedPayload = atob(payloadBase64); // Decode base64
|
||||
const payload = JSON.parse(decodedPayload);
|
||||
|
||||
// Get expiration time (in seconds)
|
||||
const payload = JSON.parse(atob(payloadBase64));
|
||||
const exp = payload.exp;
|
||||
|
||||
if (!exp) return true; // No exp field? Treat as expired
|
||||
|
||||
// Current time in seconds
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
// Compare
|
||||
return now >= exp;
|
||||
} catch (error) {
|
||||
console.error("Invalid token format:", error);
|
||||
return true; // Err on the side of caution if decoding fails
|
||||
if (!exp) return true;
|
||||
return Math.floor(Date.now() / 1000) >= exp;
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function get<T>(url: string, spreadOptions?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
if (isTokenExpired(token)) loginWithRedirect()
|
||||
if (isTokenExpired(token)) {
|
||||
loginWithRedirect();
|
||||
return Promise.reject(new Error("token expired"));
|
||||
}
|
||||
return axios.get(url, {...defaultOptions(), ...spreadOptions});
|
||||
}
|
||||
|
||||
|
|
@ -83,6 +76,10 @@ export default function HTTPProvider(props: Props) {
|
|||
data?: any,
|
||||
spreadOptions?: AxiosRequestConfig
|
||||
): Promise<AxiosResponse<T>> {
|
||||
if (isTokenExpired(token)) {
|
||||
loginWithRedirect();
|
||||
return Promise.reject(new Error("token expired"));
|
||||
}
|
||||
return axios.put(url, data, { ...defaultOptions(), ...spreadOptions });
|
||||
}
|
||||
|
||||
|
|
@ -91,10 +88,18 @@ export default function HTTPProvider(props: Props) {
|
|||
data?: any,
|
||||
spreadOptions?: AxiosRequestConfig
|
||||
): Promise<AxiosResponse<T>> {
|
||||
if (isTokenExpired(token)) {
|
||||
loginWithRedirect();
|
||||
return Promise.reject(new Error("token expired"));
|
||||
}
|
||||
return axios.post(url, data, { ...defaultOptions(), ...spreadOptions });
|
||||
}
|
||||
|
||||
function del<T>(url: string, spreadOptions?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
if (isTokenExpired(token)) {
|
||||
loginWithRedirect();
|
||||
return Promise.reject(new Error("token expired"));
|
||||
}
|
||||
return axios.delete(url, { ...defaultOptions(), ...spreadOptions });
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue