auth + groups invite
This commit is contained in:
@@ -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'];
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user