Skip to content
Snippets Groups Projects
Commit b4c37340 authored by lcross2002's avatar lcross2002
Browse files

fix login infinite loop bug

parent 6da3dcad
No related branches found
No related tags found
No related merge requests found
......@@ -47,7 +47,7 @@ namespace UserMicroservice.Controllers
{
string? refreshToken = Request.Cookies["RefreshToken"];
if (string.IsNullOrEmpty(refreshToken))
return BadRequest("Refresh token is missing.");
return Unauthorized("Refresh token is missing.");
if (!_authService.ValidateRefreshToken(refreshToken))
return Unauthorized("Invalid or expired refresh token.");
......
......@@ -23,16 +23,20 @@ export function Login() {
try {
const result = await loginUser(formValue);
giveAuth();
updateUser(result.data);
navigate(`/${userToDashboard(result.data)}`);
if (result) {
giveAuth();
updateUser(result.data);
navigate(`/${userToDashboard(result.data)}`);
} else {
throw new Error();
}
} catch (error) {
const errorMessage = (error as AxiosError).response?.data;
if (typeof errorMessage == 'string') {
setError(errorMessage);
} else {
setError('An unexpected error has occurred');
setError('An unexpected error has occurred, most likely incorrect credentials');
}
}
};
......
......@@ -6,11 +6,19 @@ const Api = axios.create({
baseURL: 'http://localhost:5267/api/'
});
const isAlreadyAuthUrl = (error: AxiosError) => {
const full = error.request.responseURL as string;
return full.endsWith('User/authorize');
};
Api.interceptors.response.use(
(response) => response, // Normal success logic
(error: AxiosError) => {
if (error.request.status === 401) { // If 401 (Unauthorized)
AuthoriseUser().then(() => { // Re-auth
if (error.request.status === 401 && !isAlreadyAuthUrl(error)) { // If 401 (Unauthorized)
AuthoriseUser().then((response: any) => { // Re-auth
if (!response) // If no response then just return
return;
return Api.request(error.config as AxiosRequestConfig); // Redo the request
});
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment