Skip to content

Commit f2043c3

Browse files
committed
fix: fix cors and add Unauthorized error handler
1 parent 4630ff7 commit f2043c3

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/AccountController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public async Task<IActionResult> RefreshToken()
155155
}
156156

157157
[HttpPost("logout")]
158+
[ProducesResponseType((int)HttpStatusCode.OK)]
158159
public IActionResult Logout()
159160
{
160161
ClearTokenCookie();

HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void Configure(IApplicationBuilder app, IHostEnvironment env)
126126

127127
app.UseRouting();
128128
app.UseCors(x => x
129-
.AllowAnyMethod()
129+
.WithMethods("GET")
130130
.AllowAnyHeader()
131131
.SetIsOriginAllowed(_ => true)
132132
.AllowCredentials());

hwproj.front/src/App.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ 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 from "./api/ApiSingleton";
19+
import ApiSingleton, {setAunauthorizedHandler} from "./api/ApiSingleton";
2020
import SystemInfoComponent from "./components/System/SystemInfoComponent";
2121
import WrongPath from "./components/WrongPath";
2222
import ResetPassword from "components/Auth/ResetPassword";
@@ -34,6 +34,7 @@ interface AppState {
3434
newNotificationsCount: number;
3535
appBarContextAction: AppBarContextAction;
3636
authReady: boolean;
37+
needAuth: boolean;
3738
}
3839

3940
const withRouter = (Component: any) => {
@@ -58,12 +59,15 @@ class App extends Component<{ navigate: any }, AppState> {
5859
isExpert: ApiSingleton.authService.isExpert(),
5960
newNotificationsCount: 0,
6061
appBarContextAction: "Default",
61-
authReady: false
62+
authReady: false,
63+
needAuth: false,
6264
};
6365
appBarStateManager.setOnContextActionChange(appBarState => this.setState({appBarContextAction: appBarState}))
6466
}
6567

6668
componentDidMount = async () => {
69+
setUnauthorizedHandler(() => this.setState({needAuth: true}));
70+
6771
const user = await ApiSingleton.authService.getProfile();
6872
this.setState(
6973
{
@@ -90,7 +94,8 @@ class App extends Component<{ navigate: any }, AppState> {
9094
this.setState({
9195
loggedIn: true,
9296
isLecturer: isLecturer,
93-
isExpert: isExpert
97+
isExpert: isExpert,
98+
needAuth: false,
9499
})
95100
if (!isExpert) {
96101
await this.updatedNewNotificationsCount()
@@ -100,7 +105,7 @@ class App extends Component<{ navigate: any }, AppState> {
100105

101106
logout = async () => {
102107
await ApiSingleton.authService.logout();
103-
this.setState({loggedIn: false, isLecturer: false, isExpert: false, newNotificationsCount: 0, authReady: true});
108+
this.setState({loggedIn: false, isLecturer: false, isExpert: false, newNotificationsCount: 0, authReady: true, needAuth: false});
104109
this.props.navigate("/login");
105110
}
106111

@@ -115,6 +120,7 @@ class App extends Component<{ navigate: any }, AppState> {
115120
onLogout={this.logout}
116121
contextAction={this.state.appBarContextAction}/>
117122
<TrackPageChanges/>
123+
118124
<Routes>
119125
<Route element={<AuthLayout/>}>
120126
<Route path="user/edit" element={<EditProfile isExpert={this.state.isExpert}/>}/>

hwproj.front/src/api/ApiSingleton.ts

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

72+
let unauthorizedHandler: (() => void) | null = null
73+
74+
export const setUnauthorizedHandler = (handler: (() => void) | null) => {
75+
unauthorizedHandler = handler;
76+
}
77+
7278
const cookieFetch = async (url: string, init: any) => {
7379
const response = await fetch(url, {
7480
...init,
7581
credentials: "include"
7682
});
7783

84+
const path = window.location.pathname;
85+
86+
if (response.status === 401 &&
87+
!path.includes("login") &&
88+
!path.includes("register")){
89+
if (unauthorizedHandler) {
90+
unauthorizedHandler()
91+
}
92+
}
93+
7894
return response;
7995
}
8096

hwproj.front/src/services/AuthService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {LoginViewModel, AccountApi, RegisterViewModel} from '../api';
1+
import {LoginViewModel, RegisterViewModel} from '../api';
22
import ApiSingleton from "../api/ApiSingleton";
33
import decode from "jwt-decode";
44

0 commit comments

Comments
 (0)