VOOZH about

URL: https://www.geeksforgeeks.org/reactjs/how-to-manage-user-sessions-and-tokens-in-redux-applications/

⇱ How to Manage User Sessions and Tokens in Redux Applications? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Manage User Sessions and Tokens in Redux Applications?

Last Updated : 3 Jun, 2024

This project is designed to provide a basic user authentication system entirely on the client side using React and Redux, allowing users to login, register, and logout. Authentication actions and state management are managed locally, with user data stored in the browser's local storage.

Approach

  • Project Setup : Created a React application is using create-react-app and Install Redux and React-Redux to manage the application's state
  • State Management: Initial state includes token and user data retrieved from local storage.
  • Reducer: Make LOGIN_SUCCESS to validates credentials and sets token. REGISTER_SUCCESS adds new user and LOGOUT clears token from local storage.
  • Components: Make Login, Register, and Logout components and handle user authentication and dispatch actions.
  • Conditional Rendering : Authenticated users see the Home component and Logout button. Unauthenticated users see the Login and Register components.

Step to Create Application

Step 1: Create a React Application named 'auth-app' and navigate to it using this command.

npx create-react-app auth-app
cd auth-app

Step 2: Install required packages and dependencies.

npm install react-redux redux 

Project Structure:
👁 project-structure
Project Structure

Updated dependencies in package.json file:

"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-redux": "^9.1.2",
"redux": "^5.0.1"
},

Example: This example shows the implementation of the above-explained approach.

Output:

Comment
Article Tags: