Skip to content

Commit ae704bd

Browse files
committed
feat: Update login workflow
1 parent 6865a48 commit ae704bd

File tree

4 files changed

+88
-30
lines changed

4 files changed

+88
-30
lines changed

src/api/chrisapiclient.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,58 @@ class ChrisAPIClient {
1919
static getClient(): Client {
2020
const cookie = new Cookies();
2121
const user = cookie.get("username");
22-
const token: string = cookie.get(`${user}_token`);
2322

24-
// If client exists but token has changed, just update the auth token
25-
if (ChrisAPIClient.client && token !== ChrisAPIClient.lastCreatedWith) {
26-
// Update the token on the existing client
27-
ChrisAPIClient.client.auth = token;
28-
// Update the stored token
29-
ChrisAPIClient.lastCreatedWith = token;
30-
return ChrisAPIClient.client;
31-
}
23+
// Get token if username is available
24+
const token = user ? cookie.get(`${user}_token`) : null;
3225

33-
// Create new client if it doesn't exist yet
26+
// Case 1: No client exists yet, create a new one
3427
if (!ChrisAPIClient.client) {
3528
ChrisAPIClient.client = new Client(import.meta.env.VITE_CHRIS_UI_URL, {
36-
token,
29+
token: token || undefined,
3730
});
3831
ChrisAPIClient.lastCreatedWith = token;
32+
return ChrisAPIClient.client;
3933
}
4034

35+
// Case 2: Client exists but no token is available (user logged out)
36+
if (!token) {
37+
// Reset client auth if it was previously authenticated
38+
if (ChrisAPIClient.lastCreatedWith) {
39+
ChrisAPIClient.client.auth = "";
40+
ChrisAPIClient.lastCreatedWith = null;
41+
}
42+
return ChrisAPIClient.client;
43+
}
44+
45+
// Case 3: Client exists and token is different from last time
46+
if (token !== ChrisAPIClient.lastCreatedWith) {
47+
ChrisAPIClient.client.auth = token;
48+
ChrisAPIClient.lastCreatedWith = token;
49+
}
50+
51+
return ChrisAPIClient.client;
52+
}
53+
54+
/**
55+
* Explicitly set the client with a new token
56+
* Use this during login to ensure the client is properly configured
57+
* @param token The authentication token
58+
*/
59+
static setClientWithToken(token: string): Client {
60+
// Always create a fresh client during explicit login
61+
ChrisAPIClient.client = new Client(import.meta.env.VITE_CHRIS_UI_URL, {
62+
token,
63+
});
64+
ChrisAPIClient.lastCreatedWith = token;
4165
return ChrisAPIClient.client;
4266
}
4367

4468
/**
4569
* Reset the client instance (useful for testing or logout)
4670
*/
4771
static resetClient(): void {
48-
ChrisAPIClient.client = null;
72+
// Instead of setting to null, create a new unauthenticated client
73+
ChrisAPIClient.client = new Client(import.meta.env.VITE_CHRIS_UI_URL);
4974
ChrisAPIClient.lastCreatedWith = null;
5075
}
5176
}

src/components/Login/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ export const SimpleLoginPage: React.FunctionComponent = () => {
6565
path: "/",
6666
maxAge: oneDayToSeconds,
6767
});
68-
const client = ChrisAPIClient.getClient();
68+
69+
// Use the new method to ensure a fresh client is created with the correct token
70+
const client = ChrisAPIClient.setClientWithToken(token);
71+
6972
const user = await client.getUser();
7073
setCookie("isStaff", user.data.is_staff, {
7174
path: "/",
7275
maxAge: oneDayToSeconds,
7376
});
77+
78+
// Update Redux store after all cookies and client are set
7479
dispatch(
7580
setAuthTokenSuccess({
7681
token,

src/components/Wrapper/Toolbar.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,28 @@ const ToolbarComponent: React.FC<ToolbarComponentProps> = (
5555
};
5656

5757
const onLogout = () => {
58+
// Store the current URL for redirect after login
59+
const currentPath = location.pathname + location.search;
60+
61+
// Clear query cache first
5862
queryClient.clear();
63+
64+
// Remove all auth-related cookies with proper path
65+
if (username) {
66+
removeCookie(`${username}_token`, { path: "/" });
67+
}
68+
removeCookie("username", { path: "/" });
69+
removeCookie("isStaff", { path: "/" });
70+
71+
// Reset API client after cookies are removed
5972
ChrisAPIClient.resetClient();
60-
removeCookie("username", {
61-
path: "/",
62-
});
63-
removeCookie(`${username}_token`, {
64-
path: "/",
65-
});
66-
removeCookie("isStaff", {
67-
path: "/",
68-
});
73+
74+
// Update Redux state
6975
dispatch(clearCartOnLogout());
7076
dispatch(setLogoutSuccess());
77+
78+
// Redirect to login page with the return URL as a parameter
79+
navigate(`/login?redirectTo=${encodeURIComponent(currentPath)}`);
7180
};
7281

7382
const onDropdownToggle = () => {

src/store/user/userSlice.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,36 @@ export interface IUserState {
1111
isStaff?: boolean;
1212
}
1313

14-
const cookie = new Cookies();
15-
const user = cookie.get("username");
16-
const token = cookie.get(`${user}_token`);
17-
const isStaff = cookie.get("isStaff");
14+
// Improved cookie retrieval function that handles potential inconsistencies
15+
const getAuthStateFromCookies = () => {
16+
const cookie = new Cookies();
17+
const username = cookie.get("username");
18+
19+
// Only attempt to get token if username exists
20+
let token = null;
21+
if (username) {
22+
token = cookie.get(`${username}_token`);
23+
}
24+
25+
const isStaff = cookie.get("isStaff");
26+
const isLoggedIn = !!(username && token);
27+
28+
return {
29+
username,
30+
token,
31+
isStaff: !!isStaff,
32+
isLoggedIn,
33+
};
34+
};
35+
36+
const cookieState = getAuthStateFromCookies();
1837

1938
const initialState: IUserState = {
20-
username: user,
21-
token: token,
39+
username: cookieState.username,
40+
token: cookieState.token,
2241
isRememberMe: false,
23-
isLoggedIn: !!token,
24-
isStaff: !!isStaff,
42+
isLoggedIn: cookieState.isLoggedIn,
43+
isStaff: cookieState.isStaff,
2544
};
2645

2746
const userSlice = createSlice({

0 commit comments

Comments
 (0)