Auth with redux toolkit and rtk query

Viewed 782

I dont understand the proccess between rtk query with redux toolkit slice.

i have a auth slice like this:

const Auth = createSlice({
  name: 'Auth',
  initialState,
  reducers: {
    setLoading: (state) => {
      state.load = true;
      state.error = { message: '' }
    },
    setAuth: (state, action: PayloadAction<User>) => {
      state.user = action.payload;
      state.auth = true;
    },
    setLogout: (state, action: PayloadAction<Error>) => {
      state.error = action.payload
    }
  }
});

export const { setLoading, setAuth, setLogout } = Auth.actions;

export default Auth.reducer;

And I want to login with rtk query. Should I do this in this way ?

const { isLoading, data, error } = useAllDataQuery(...);

useEffect(() => {
 isLoading && dispatch(setLoading());
 data && dispatch(setAuth());
 error && dispatch(setError(());
}, [isLoading, data, error]);

or is there a better way to handle the auth

0 Answers
Related