Skip to content

Commit 53f998e

Browse files
committed
Fix: Add logout navigation redirect to prevent 501 error
- Redirect users to home page after logout instead of staying on profile page - Resolves 501 error when logging in from profile page after logout - Fixes #6
1 parent 921f365 commit 53f998e

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

front-end/lib/api.mjs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {state} from "../index.mjs";
2-
import {handleErrorDialog} from "../components/error.mjs";
1+
import { state } from "../index.mjs";
2+
import { handleErrorDialog } from "../components/error.mjs";
33

44
// === ABOUT THE STATE
55
// state gives you these two functions only
@@ -20,13 +20,13 @@ async function _apiRequest(endpoint, options = {}) {
2020
const defaultOptions = {
2121
headers: {
2222
"Content-Type": "application/json",
23-
...(token ? {Authorization: `Bearer ${token}`} : {}),
23+
...(token ? { Authorization: `Bearer ${token}` } : {}),
2424
},
2525
mode: "cors",
2626
credentials: "include",
2727
};
2828

29-
const fetchOptions = {...defaultOptions, ...options};
29+
const fetchOptions = { ...defaultOptions, ...options };
3030
const url = endpoint.startsWith("http") ? endpoint : `${baseUrl}${endpoint}`;
3131

3232
try {
@@ -54,7 +54,7 @@ async function _apiRequest(endpoint, options = {}) {
5454
const contentType = response.headers.get("content-type");
5555
return contentType?.includes("application/json")
5656
? await response.json()
57-
: {success: true};
57+
: { success: true };
5858
} catch (error) {
5959
if (!error.status) {
6060
// Only handle network errors here, response errors are handled above
@@ -70,19 +70,19 @@ function _updateProfile(username, profileData) {
7070
const index = profiles.findIndex((p) => p.username === username);
7171

7272
if (index !== -1) {
73-
profiles[index] = {...profiles[index], ...profileData};
73+
profiles[index] = { ...profiles[index], ...profileData };
7474
} else {
75-
profiles.push({username, ...profileData});
75+
profiles.push({ username, ...profileData });
7676
}
77-
state.updateState({profiles});
77+
state.updateState({ profiles });
7878
}
7979

8080
// ====== AUTH methods
8181
async function login(username, password) {
8282
try {
8383
const data = await _apiRequest("/login", {
8484
method: "POST",
85-
body: JSON.stringify({username, password}),
85+
body: JSON.stringify({ username, password }),
8686
});
8787

8888
if (data.success && data.token) {
@@ -96,20 +96,20 @@ async function login(username, password) {
9696

9797
return data;
9898
} catch (error) {
99-
return {success: false};
99+
return { success: false };
100100
}
101101
}
102102

103103
async function getWhoToFollow() {
104104
try {
105105
const usernamesToFollow = await _apiRequest("/suggested-follows/3");
106106

107-
state.updateState({whoToFollow: usernamesToFollow});
107+
state.updateState({ whoToFollow: usernamesToFollow });
108108

109109
return usernamesToFollow;
110110
} catch (error) {
111111
// Error already handled by _apiRequest
112-
state.updateState({usernamesToFollow: []});
112+
state.updateState({ usernamesToFollow: [] });
113113
return [];
114114
}
115115
}
@@ -118,7 +118,7 @@ async function signup(username, password) {
118118
try {
119119
const data = await _apiRequest("/register", {
120120
method: "POST",
121-
body: JSON.stringify({username, password}),
121+
body: JSON.stringify({ username, password }),
122122
});
123123

124124
if (data.success && data.token) {
@@ -132,20 +132,21 @@ async function signup(username, password) {
132132

133133
return data;
134134
} catch (error) {
135-
return {success: false};
135+
return { success: false };
136136
}
137137
}
138138

139139
function logout() {
140140
state.destroyState();
141-
return {success: true};
141+
window.location.hash = "/";
142+
return { success: true };
142143
}
143144

144145
// ===== BLOOM methods
145146
async function getBloom(bloomId) {
146147
const endpoint = `/bloom/${bloomId}`;
147148
const bloom = await _apiRequest(endpoint);
148-
state.updateState({singleBloomToShow: bloom});
149+
state.updateState({ singleBloomToShow: bloom });
149150
return bloom;
150151
}
151152

@@ -156,18 +157,18 @@ async function getBlooms(username) {
156157
const blooms = await _apiRequest(endpoint);
157158

158159
if (username) {
159-
_updateProfile(username, {blooms});
160+
_updateProfile(username, { blooms });
160161
} else {
161-
state.updateState({timelineBlooms: blooms});
162+
state.updateState({ timelineBlooms: blooms });
162163
}
163164

164165
return blooms;
165166
} catch (error) {
166167
// Error already handled by _apiRequest
167168
if (username) {
168-
_updateProfile(username, {blooms: []});
169+
_updateProfile(username, { blooms: [] });
169170
} else {
170-
state.updateState({timelineBlooms: []});
171+
state.updateState({ timelineBlooms: [] });
171172
}
172173
return [];
173174
}
@@ -189,15 +190,15 @@ async function getBloomsByHashtag(hashtag) {
189190
return blooms;
190191
} catch (error) {
191192
// Error already handled by _apiRequest
192-
return {success: false};
193+
return { success: false };
193194
}
194195
}
195196

196197
async function postBloom(content) {
197198
try {
198199
const data = await _apiRequest("/bloom", {
199200
method: "POST",
200-
body: JSON.stringify({content}),
201+
body: JSON.stringify({ content }),
201202
});
202203

203204
if (data.success) {
@@ -208,7 +209,7 @@ async function postBloom(content) {
208209
return data;
209210
} catch (error) {
210211
// Error already handled by _apiRequest
211-
return {success: false};
212+
return { success: false };
212213
}
213214
}
214215

@@ -225,24 +226,24 @@ async function getProfile(username) {
225226
const currentUsername = profileData.username;
226227
const fullProfileData = await _apiRequest(`/profile/${currentUsername}`);
227228
_updateProfile(currentUsername, fullProfileData);
228-
state.updateState({currentUser: currentUsername, isLoggedIn: true});
229+
state.updateState({ currentUser: currentUsername, isLoggedIn: true });
229230
}
230231

231232
return profileData;
232233
} catch (error) {
233234
// Error already handled by _apiRequest
234235
if (!username) {
235-
state.updateState({isLoggedIn: false, currentUser: null});
236+
state.updateState({ isLoggedIn: false, currentUser: null });
236237
}
237-
return {success: false};
238+
return { success: false };
238239
}
239240
}
240241

241242
async function followUser(username) {
242243
try {
243244
const data = await _apiRequest("/follow", {
244245
method: "POST",
245-
body: JSON.stringify({follow_username: username}),
246+
body: JSON.stringify({ follow_username: username }),
246247
});
247248

248249
if (data.success) {
@@ -255,7 +256,7 @@ async function followUser(username) {
255256

256257
return data;
257258
} catch (error) {
258-
return {success: false};
259+
return { success: false };
259260
}
260261
}
261262

@@ -277,7 +278,7 @@ async function unfollowUser(username) {
277278
return data;
278279
} catch (error) {
279280
// Error already handled by _apiRequest
280-
return {success: false};
281+
return { success: false };
281282
}
282283
}
283284

@@ -300,4 +301,4 @@ const apiService = {
300301
getWhoToFollow,
301302
};
302303

303-
export {apiService};
304+
export { apiService };

0 commit comments

Comments
 (0)