Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Redirect the login if not login #251

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions web/src/pages/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ export default function Login({ localServer }: { localServer: string }) {
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
}
}).catch(error => {
const errorcode = error.response.data as IHTTPError;
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
if (error.response != undefined) {
const errorcode = error.response.data as IHTTPError;
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
}
});
}, [])

Expand Down Expand Up @@ -206,8 +208,10 @@ function GitHubButton({ localServer, endpoint }: { localServer: string, endpoint
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
}
}).catch(error => {
const errorcode = error.response.data as IHTTPError;
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
if (error.response != undefined) {
const errorcode = error.response.data as IHTTPError;
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
}
});
}, []);

Expand Down Expand Up @@ -244,8 +248,10 @@ function GitLabButton({ localServer, endpoint }: { localServer: string, endpoint
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
}
}).catch(error => {
const errorcode = error.response.data as IHTTPError;
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
if (error.response != undefined) {
const errorcode = error.response.data as IHTTPError;
Notification({ level: "warning", title: errorcode.title, message: errorcode.description });
}
});
}, []);

Expand Down
25 changes: 15 additions & 10 deletions web/src/utils/request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@

import axios from 'axios';

axios.interceptors.request.use((config: any) => {
const token = localStorage.getItem('token');
if ((config.headers.Authorization === undefined || config.headers.Authorization === null) &&
token !== undefined && token !== null) {
config.headers.Authorization = "Bearer " + token
}
return config;
});

export const setupResponseInterceptor = (navigate: any) => {
axios.interceptors.response.use(response => {
return response;
Expand All @@ -36,5 +27,19 @@ export const setupResponseInterceptor = (navigate: any) => {
} else {
return Promise.resolve(error?.response);
}
})
});
axios.interceptors.request.use((config: any) => {
const token = localStorage.getItem('token');
if (config.headers.Authorization === undefined || config.headers.Authorization === null) {
if (token === null) {
if (!config.url.endsWith("/api/v1/users/login")) {
navigate('/login');
return Promise.reject(new Error('request has been banned by axios interceptor'));
}
} else {
config.headers.Authorization = "Bearer " + token;
}
}
return config;
});
}