Skip to content

Commit 4021f30

Browse files
committed
feat: add 401 error handler
1 parent f2043c3 commit 4021f30

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

hwproj.front/src/App.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ import Register from "./components/Auth/Register";
1616
import ExpertsNotebook from "./components/Experts/Notebook";
1717
import StudentSolutionsPage from "./components/Solutions/StudentSolutionsPage";
1818
import EditProfile from "./components/EditProfile";
19-
import ApiSingleton, {setAunauthorizedHandler} from "./api/ApiSingleton";
19+
import ApiSingleton from "./api/ApiSingleton";
2020
import SystemInfoComponent from "./components/System/SystemInfoComponent";
2121
import WrongPath from "./components/WrongPath";
2222
import ResetPassword from "components/Auth/ResetPassword";
2323
import PasswordRecovery from "components/Auth/PasswordRecovery";
2424
import AuthLayout from "./AuthLayout";
2525
import ExpertAuthLayout from "./components/Experts/AuthLayout";
2626
import TrackPageChanges from "TrackPageChanges";
27+
import {Alert, Button} from "@mui/material";
28+
import {Snackbar} from "@material-ui/core";
2729

2830
// TODO: add flux
2931

@@ -34,7 +36,6 @@ interface AppState {
3436
newNotificationsCount: number;
3537
appBarContextAction: AppBarContextAction;
3638
authReady: boolean;
37-
needAuth: boolean;
3839
}
3940

4041
const withRouter = (Component: any) => {
@@ -59,15 +60,12 @@ class App extends Component<{ navigate: any }, AppState> {
5960
isExpert: ApiSingleton.authService.isExpert(),
6061
newNotificationsCount: 0,
6162
appBarContextAction: "Default",
62-
authReady: false,
63-
needAuth: false,
63+
authReady: false
6464
};
6565
appBarStateManager.setOnContextActionChange(appBarState => this.setState({appBarContextAction: appBarState}))
6666
}
6767

6868
componentDidMount = async () => {
69-
setUnauthorizedHandler(() => this.setState({needAuth: true}));
70-
7169
const user = await ApiSingleton.authService.getProfile();
7270
this.setState(
7371
{
@@ -94,8 +92,7 @@ class App extends Component<{ navigate: any }, AppState> {
9492
this.setState({
9593
loggedIn: true,
9694
isLecturer: isLecturer,
97-
isExpert: isExpert,
98-
needAuth: false,
95+
isExpert: isExpert
9996
})
10097
if (!isExpert) {
10198
await this.updatedNewNotificationsCount()
@@ -105,7 +102,7 @@ class App extends Component<{ navigate: any }, AppState> {
105102

106103
logout = async () => {
107104
await ApiSingleton.authService.logout();
108-
this.setState({loggedIn: false, isLecturer: false, isExpert: false, newNotificationsCount: 0, authReady: true, needAuth: false});
105+
this.setState({loggedIn: false, isLecturer: false, isExpert: false, newNotificationsCount: 0, authReady: true});
109106
this.props.navigate("/login");
110107
}
111108

@@ -120,7 +117,6 @@ class App extends Component<{ navigate: any }, AppState> {
120117
onLogout={this.logout}
121118
contextAction={this.state.appBarContextAction}/>
122119
<TrackPageChanges/>
123-
124120
<Routes>
125121
<Route element={<AuthLayout/>}>
126122
<Route path="user/edit" element={<EditProfile isExpert={this.state.isExpert}/>}/>

hwproj.front/src/api/ApiSingleton.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ function getApiBase(): string {
6969
return `${protocol}//${hostname}${effectivePort ? `:${effectivePort}` : ""}`
7070
}
7171

72-
let unauthorizedHandler: (() => void) | null = null
72+
let skipRedirect = false;
7373

74-
export const setUnauthorizedHandler = (handler: (() => void) | null) => {
75-
unauthorizedHandler = handler;
74+
export async function runWithoutAuthRedirect(functionToRun: () => Promise<any>) {
75+
skipRedirect = true;
76+
try {
77+
return await functionToRun();
78+
} finally {
79+
skipRedirect = false;
80+
}
7681
}
7782

7883
const cookieFetch = async (url: string, init: any) => {
@@ -85,10 +90,9 @@ const cookieFetch = async (url: string, init: any) => {
8590

8691
if (response.status === 401 &&
8792
!path.includes("login") &&
88-
!path.includes("register")){
89-
if (unauthorizedHandler) {
90-
unauthorizedHandler()
91-
}
93+
!path.includes("register") &&
94+
!skipRedirect){
95+
window.location.href = `/login?returnUrl=${window.location.pathname}`;
9296
}
9397

9498
return response;

hwproj.front/src/services/AuthService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {LoginViewModel, RegisterViewModel} from '../api';
2-
import ApiSingleton from "../api/ApiSingleton";
2+
import ApiSingleton, {runWithoutAuthRedirect} from "../api/ApiSingleton";
33
import decode from "jwt-decode";
44

55

@@ -29,7 +29,8 @@ export default class AuthService {
2929

3030
public async getUser() {
3131
try {
32-
const accountData = await ApiSingleton.accountApi.accountGetUserSummary();
32+
const accountData = await runWithoutAuthRedirect(() =>
33+
ApiSingleton.accountApi.accountGetUserSummary());
3334
if (accountData) {
3435
this._user = {
3536
id: accountData.userId!,

0 commit comments

Comments
 (0)