auth + groups invite

This commit is contained in:
Виталий Лавшонок
2025-11-15 17:37:47 +03:00
parent ded41ba7f0
commit dfc2985209
16 changed files with 673 additions and 225 deletions

View File

@@ -121,11 +121,26 @@ export const refreshToken = createAsyncThunk(
export const fetchWhoAmI = createAsyncThunk(
'auth/whoami',
async (_, { rejectWithValue }) => {
async (_, { dispatch, getState, rejectWithValue }) => {
try {
const response = await axios.get('/authentication/whoami');
return response.data;
} catch (err: any) {
const state: any = getState();
const refresh = state.auth.refreshToken;
if (refresh) {
// пробуем refresh
const result = await dispatch(
refreshToken({ refreshToken: refresh }),
);
// если успешный, повторить whoami
if (refreshToken.fulfilled.match(result)) {
const retry = await axios.get('/authentication/whoami');
return retry.data;
}
}
return rejectWithValue(
err.response?.data?.message || 'Failed to fetch user info',
);
@@ -269,6 +284,23 @@ const authSlice = createSlice({
builder.addCase(fetchWhoAmI.rejected, (state, action) => {
state.status = 'failed';
state.error = action.payload as string;
// Если пользователь не авторизован (401), делаем logout и пытаемся refresh
console.log(action);
if (
action.payload === 'Unauthorized' ||
action.payload === 'Failed to fetch user info'
) {
// Вызов logout
state.jwt = null;
state.refreshToken = null;
state.username = null;
state.email = null;
state.id = null;
localStorage.removeItem('jwt');
localStorage.removeItem('refreshToken');
delete axios.defaults.headers.common['Authorization'];
}
});
},
});