fix: change login page logic
Signed-off-by: Raul Kele <raulkeleblk@gmail.com>
This commit is contained in:
parent
70a870a616
commit
769ffdc60d
@ -24,7 +24,7 @@ describe('Signin component automatic navigation', () => {
|
|||||||
|
|
||||||
it('navigates to homepage when auth is disabled', async () => {
|
it('navigates to homepage when auth is disabled', async () => {
|
||||||
// mock request to check auth
|
// mock request to check auth
|
||||||
jest.spyOn(api, 'get').mockResolvedValue({ status: 200, data: {} });
|
jest.spyOn(api, 'get').mockResolvedValue({ status: 200, data: { http: {} } });
|
||||||
render(<SignIn isAuthEnabled={true} setIsAuthEnabled={() => {}} isLoggedIn={false} setIsLoggedIn={() => {}} />);
|
render(<SignIn isAuthEnabled={true} setIsAuthEnabled={() => {}} isLoggedIn={false} setIsLoggedIn={() => {}} />);
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(mockedUsedNavigate).toHaveBeenCalledWith('/home');
|
expect(mockedUsedNavigate).toHaveBeenCalledWith('/home');
|
||||||
|
19
src/__tests__/api.test.js
Normal file
19
src/__tests__/api.test.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { api } from '../api';
|
||||||
|
|
||||||
|
describe('api module', () => {
|
||||||
|
it('should redirect to login if a 401 error is received', () => {
|
||||||
|
const location = new URL('https://www.test.com');
|
||||||
|
location.replace = jest.fn();
|
||||||
|
delete window.location;
|
||||||
|
window.location = location;
|
||||||
|
const axiosInstance = api.getAxiosInstance();
|
||||||
|
expect(
|
||||||
|
axiosInstance.interceptors.response.handlers[0].rejected({
|
||||||
|
response: { statusText: 'Unauthorized', status: 401 }
|
||||||
|
})
|
||||||
|
).rejects.toMatchObject({
|
||||||
|
response: { statusText: 'Unauthorized', status: 401 }
|
||||||
|
});
|
||||||
|
expect(location.replace).toHaveBeenCalledWith('/login');
|
||||||
|
});
|
||||||
|
});
|
@ -9,12 +9,15 @@ axios.interceptors.response.use(
|
|||||||
(error) => {
|
(error) => {
|
||||||
if (error.response.status === 401) {
|
if (error.response.status === 401) {
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
|
window.location.replace('/login');
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const api = {
|
const api = {
|
||||||
|
getAxiosInstance: () => axios,
|
||||||
|
|
||||||
getRequestCfg: () => {
|
getRequestCfg: () => {
|
||||||
const genericHeaders = {
|
const genericHeaders = {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
@ -71,6 +74,7 @@ const api = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const endpoints = {
|
const endpoints = {
|
||||||
|
authConfig: `/v2/_zot/ext/mgmt`,
|
||||||
repoList: ({ pageNumber = 1, pageSize = 15 } = {}) =>
|
repoList: ({ pageNumber = 1, pageSize = 15 } = {}) =>
|
||||||
`/v2/_zot/ext/search?query={RepoListWithNewestImage(requestedPage: {limit:${pageSize} offset:${
|
`/v2/_zot/ext/search?query={RepoListWithNewestImage(requestedPage: {limit:${pageSize} offset:${
|
||||||
(pageNumber - 1) * pageSize
|
(pageNumber - 1) * pageSize
|
||||||
|
@ -3,7 +3,8 @@ import React, { useEffect, useMemo, useState } from 'react';
|
|||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { host } from '../../host';
|
import { host } from '../../host';
|
||||||
// utility
|
// utility
|
||||||
import { api } from '../../api';
|
import { api, endpoints } from '../../api';
|
||||||
|
import { isEmpty } from 'lodash';
|
||||||
|
|
||||||
// components
|
// components
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
@ -117,15 +118,15 @@ export default function SignIn({ isLoggedIn, setIsLoggedIn, wrapperSetLoading =
|
|||||||
navigate('/home');
|
navigate('/home');
|
||||||
} else {
|
} else {
|
||||||
api
|
api
|
||||||
.get(`${host()}/v2/`, abortController.signal)
|
.get(`${host()}${endpoints.authConfig}`, abortController.signal)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response.status === 200) {
|
if (response.data?.http && isEmpty(response.data?.http?.auth)) {
|
||||||
localStorage.setItem('token', '-');
|
localStorage.setItem('token', '-');
|
||||||
setIsLoggedIn(true);
|
setIsLoggedIn(true);
|
||||||
setIsLoading(false);
|
|
||||||
wrapperSetLoading(false);
|
|
||||||
navigate('/home');
|
navigate('/home');
|
||||||
}
|
}
|
||||||
|
setIsLoading(false);
|
||||||
|
wrapperSetLoading(false);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
Loading…
Reference in New Issue
Block a user