Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit ef2cf15

Browse files
committed
fix: dynamically inject fetchwrapper to only initialise after resolving
This fixes a bug causing Timetabl not to work until after refreshing the first time you log in
1 parent 7820a3f commit ef2cf15

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

apps/client/src/main.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,35 @@ const oauthClient = new OAuth2Client({
8484
authorizationEndpoint: config.authorization_endpoint,
8585
});
8686

87-
const fetchWrapper = new OAuth2Fetch({
88-
client: oauthClient,
87+
const fetchWrapperInjector = () =>
88+
new OAuth2Fetch({
89+
client: oauthClient,
8990

90-
getNewToken: () => {
91-
log("getNewToken invoked");
91+
getNewToken: () => {
92+
log("getNewToken invoked");
9293

93-
// Set the status to expired
94-
useAuthStore.setState({ status: AuthStatus.EXPIRED });
94+
// Set the status to expired
95+
useAuthStore.setState({ status: AuthStatus.EXPIRED });
9596

96-
return null; // Fail this step, we don't want to log out until the user does so explicitly
97-
},
97+
return null; // Fail this step, we don't want to log out until the user does so explicitly
98+
},
9899

99-
storeToken: (token) => {
100-
useAuthStore.setState({
101-
token,
102-
});
103-
},
100+
storeToken: (token) => {
101+
useAuthStore.setState({
102+
token,
103+
});
104+
},
104105

105-
getStoredToken: () => {
106-
return useAuthStore.getState().token;
107-
},
108-
});
106+
getStoredToken: () => {
107+
return useAuthStore.getState().token;
108+
},
109+
});
109110

110111
const authActions = new OAuth2Actions(
111112
useAuthStore,
112113
queryClient,
113114
oauthClient,
114-
fetchWrapper,
115+
fetchWrapperInjector,
115116
toast
116117
);
117118

apps/client/src/services/OAuth2Actions/OAuth2Actions.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ interface AuthStore {
2727
}
2828

2929
class OAuth2Actions implements AuthActions {
30+
private fetchWrapper?: FetchWrapper;
31+
3032
constructor(
3133
private authStore: AuthStore,
3234
private queryClient: QueryClient,
3335
private oauthClient: OAuth2Client,
34-
private fetchWrapper: FetchWrapper,
36+
private fetchWrapperInject: () => FetchWrapper,
3537
private toast: Notifier
3638
) {}
3739

@@ -135,7 +137,15 @@ class OAuth2Actions implements AuthActions {
135137
pkceState: "",
136138
codeVerifier: "",
137139
});
140+
return;
141+
}
142+
143+
if (!this.authStore.getState().token) {
144+
return;
138145
}
146+
147+
// Inject fetch wrapper
148+
this.fetchWrapper = this.fetchWrapperInject();
139149
};
140150

141151
public logout = async () => {
@@ -160,6 +170,10 @@ class OAuth2Actions implements AuthActions {
160170
endpoint: SbhsApiEndpoint,
161171
options?: Record<string, string>
162172
) => {
173+
if (!this.fetchWrapper) {
174+
throw new Error("Fetch wrapper not set");
175+
}
176+
163177
let res: Response;
164178
try {
165179
res = await this.fetchWrapper.fetch(

0 commit comments

Comments
 (0)