ansopedia / creator-studio

Ansopedia Creator Studio (ACS) is an open-source content management system (CMS) designed to power Ansopedia, a learning platform. It provides a user-friendly interface for creating, editing, and managing various content types like posts, comments, questions, quizzes, and assets (images, files).
MIT License
1 stars 2 forks source link

[Hacktoberfest] Feature Request: Implement Token Refreshing with `api/v1/auth/refresh-token` #9

Open sanjaysah101 opened 6 days ago

sanjaysah101 commented 6 days ago

Description:

Implement automatic token refreshing in the Next.js app using Redux for state management, Axios for API calls, and React Query for data management. This ensures that users remain authenticated seamlessly when their access token expires.

Tasks:

  1. Set Up Redux for Auth State Management:

    • Create authSlice using Redux Toolkit to store accessToken, refreshToken, user, and isAuthenticated.
    • Use redux-persist to persist auth state in localStorage or HttpOnly cookies.
  2. Create Token Refresh Logic with Axios Interceptors:

    • Set up a custom Axios instance with an interceptor to catch 401 Unauthorized responses.
    • In the interceptor, dispatch a refreshToken action from authSlice to call api/v1/auth/refresh-token.
    • Update the accessToken in the Redux store and retry the original request using the new token.
  3. Integrate React Query for API Calls:

    • Use React Query’s useQuery for fetching data that depends on authentication (e.g., user profile).
    • Set up automatic refetching of queries when the accessToken changes.

Example of Token Refresh Logic

Acceptance Criteria: