adjusting retry on interceptor
This commit is contained in:
parent
f030234438
commit
ed7723a8e4
4 changed files with 19 additions and 12 deletions
|
|
@ -47,7 +47,6 @@ export function AuthProvider({ children }): React.ReactElement {
|
|||
}
|
||||
|
||||
function login(newAccessToken: string, refreshToken: string) {
|
||||
|
||||
//if we want to use cookie
|
||||
cookies.set('access_token', newAccessToken, { path: '/' });
|
||||
cookies.set('refresh_token', refreshToken, { path: '/' });
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import axios, { AxiosError, AxiosInstance } from "axios";
|
||||
import { useContext, useEffect, useRef } from "react";
|
||||
import { useContext, useEffect, useRef, useState } from "react";
|
||||
import { alertContext } from "../../contexts/alertContext";
|
||||
import { AuthContext } from "../../contexts/authContext";
|
||||
import { URL_EXCLUDED_FROM_ERROR_RETRIES } from "../../constants/constants";
|
||||
|
|
@ -29,11 +29,15 @@ function ApiInterceptor() {
|
|||
const refreshToken = localStorage.getItem("refresh_token");
|
||||
if (refreshToken) {
|
||||
const res = await renewAccessToken(refreshToken);
|
||||
login(res.access_token, res.refresh_token)
|
||||
login(res.data.access_token, res.data.refresh_token);
|
||||
try {
|
||||
const response = await axios.request(error.config);
|
||||
return response;
|
||||
} catch (error) {
|
||||
const accessToken = localStorage.getItem("access_token");
|
||||
delete error.config.headers["Authorization"];
|
||||
error.config.headers["Authorization"] = `Bearer ${accessToken}`;
|
||||
const response = await axios.request(error.config);
|
||||
return response;
|
||||
}
|
||||
catch (error) {
|
||||
if(error.response?.status === 401){
|
||||
logout();
|
||||
navigate("/login");
|
||||
|
|
|
|||
|
|
@ -369,14 +369,13 @@ export async function onLogin(
|
|||
return data;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Error:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function renewAccessToken(
|
||||
token: string
|
||||
): Promise<LoginAuthType> {
|
||||
){
|
||||
try {
|
||||
return await api.post(`http://localhost:7860/refresh?token=${token}`);
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ import {
|
|||
import { onLogin } from "../../controllers/API";
|
||||
import { LoginType } from "../../types/api";
|
||||
import { AuthContext } from "../../contexts/authContext";
|
||||
import { alertContext } from "../../contexts/alertContext";
|
||||
import { error } from "console";
|
||||
|
||||
export default function LoginPage(): JSX.Element {
|
||||
const [inputState, setInputState] =
|
||||
|
|
@ -21,6 +23,7 @@ export default function LoginPage(): JSX.Element {
|
|||
const { password, username } = inputState;
|
||||
const { login } = useContext(AuthContext);
|
||||
const navigate = useNavigate();
|
||||
const { setErrorData } = useContext(alertContext);
|
||||
|
||||
function handleInput({
|
||||
target: { name, value },
|
||||
|
|
@ -35,16 +38,18 @@ export default function LoginPage(): JSX.Element {
|
|||
password: password
|
||||
};
|
||||
|
||||
try{
|
||||
onLogin(
|
||||
user
|
||||
).then((user) => {
|
||||
login(user.access_token, user.refresh_token);
|
||||
navigate("/");
|
||||
}).catch(error => {
|
||||
setErrorData({
|
||||
title: "Error signing in",
|
||||
list: [error['response']['data']['detail']],
|
||||
})
|
||||
});
|
||||
}
|
||||
catch(error){
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue