anotherath commited on
Commit
cfc278b
·
1 Parent(s): 4bae792

fix logout

Browse files
Files changed (2) hide show
  1. src/App.jsx +2 -2
  2. src/store/slices/authSlice.js +16 -7
src/App.jsx CHANGED
@@ -13,7 +13,7 @@ function App() {
13
  const dispatch = useDispatch();
14
  const { activeView, activeSpace, activeRoom, searchQuery, isSettings } =
15
  useSelector((state) => state.app);
16
- const { isAuthenticated, initialized, loading } = useSelector((state) => state.auth);
17
 
18
  useEffect(() => {
19
  if (window.location.pathname !== "/") {
@@ -26,7 +26,7 @@ function App() {
26
 
27
  const currentView = isSettings ? "settings" : activeView;
28
 
29
- if (!initialized) {
30
  return (
31
  <div className="w-screen h-screen flex items-center justify-center bg-linear-to-br from-white via-indigo-100 to-blue-200">
32
  <div className="w-10 h-10 border-4 border-indigo-200 border-t-indigo-500 rounded-full animate-spin" />
 
13
  const dispatch = useDispatch();
14
  const { activeView, activeSpace, activeRoom, searchQuery, isSettings } =
15
  useSelector((state) => state.app);
16
+ const { isAuthenticated, initialized, loading, isLoggingOut } = useSelector((state) => state.auth);
17
 
18
  useEffect(() => {
19
  if (window.location.pathname !== "/") {
 
26
 
27
  const currentView = isSettings ? "settings" : activeView;
28
 
29
+ if (!initialized || isLoggingOut) {
30
  return (
31
  <div className="w-screen h-screen flex items-center justify-center bg-linear-to-br from-white via-indigo-100 to-blue-200">
32
  <div className="w-10 h-10 border-4 border-indigo-200 border-t-indigo-500 rounded-full animate-spin" />
src/store/slices/authSlice.js CHANGED
@@ -112,7 +112,13 @@ export const register = createAsyncThunk(
112
  );
113
 
114
  export const logout = createAsyncThunk("auth/logout", async () => {
115
- // Fire-and-forget: clear client state immediately, let API run in background
 
 
 
 
 
 
116
  clearAccessToken();
117
  localStorage.removeItem("refreshToken");
118
  localStorage.removeItem("auth_user");
@@ -120,12 +126,6 @@ export const logout = createAsyncThunk("auth/logout", async () => {
120
  localStorage.removeItem("userBio");
121
  localStorage.removeItem("userAvatar");
122
  localStorage.removeItem("usernameColor");
123
-
124
- try {
125
- authService.logout();
126
- } catch {
127
- // Ignore API errors
128
- }
129
  });
130
 
131
  export const fetchProfile = createAsyncThunk(
@@ -206,7 +206,16 @@ const authSlice = createSlice({
206
  state.loading = false;
207
  state.error = action.payload;
208
  })
 
 
 
209
  .addCase(logout.fulfilled, (state) => {
 
 
 
 
 
 
210
  state.user = null;
211
  state.isAuthenticated = false;
212
  })
 
112
  );
113
 
114
  export const logout = createAsyncThunk("auth/logout", async () => {
115
+ // Gọi API logout TRƯỚC khi xóa token
116
+ try {
117
+ await authService.logout();
118
+ } catch {
119
+ // Ignore API errors — vẫn tiếp tục xóa local state
120
+ }
121
+
122
  clearAccessToken();
123
  localStorage.removeItem("refreshToken");
124
  localStorage.removeItem("auth_user");
 
126
  localStorage.removeItem("userBio");
127
  localStorage.removeItem("userAvatar");
128
  localStorage.removeItem("usernameColor");
 
 
 
 
 
 
129
  });
130
 
131
  export const fetchProfile = createAsyncThunk(
 
206
  state.loading = false;
207
  state.error = action.payload;
208
  })
209
+ .addCase(logout.pending, (state) => {
210
+ state.isLoggingOut = true;
211
+ })
212
  .addCase(logout.fulfilled, (state) => {
213
+ state.isLoggingOut = false;
214
+ state.user = null;
215
+ state.isAuthenticated = false;
216
+ })
217
+ .addCase(logout.rejected, (state) => {
218
+ state.isLoggingOut = false;
219
  state.user = null;
220
  state.isAuthenticated = false;
221
  })