Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gvshemanth30034/edu-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

76 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Educational Resource Library - Frontend

A modern React frontend application for managing educational resources with JWT-based authentication and role-based access control.

πŸš€ Features

  • Modern React Stack: Built with React 18, TypeScript, and Vite
  • Fast Development: Vite provides lightning-fast HMR and builds
  • Routing: React Router v6 for navigation and protected routes
  • Authentication: JWT-based authentication with context API
  • Styling: Tailwind CSS for utility-first responsive design
  • API Integration: Axios with interceptors for API calls
  • Role-Based Access: Admin and User role-based routing
  • Type Safety: Full TypeScript support throughout

πŸ“ Project Structure

src/
β”œβ”€β”€ components/        # Reusable UI components
β”‚   └── ProtectedRoute.tsx
β”œβ”€β”€ pages/             # Page components
β”‚   β”œβ”€β”€ LoginPage.tsx
β”‚   β”œβ”€β”€ RegisterPage.tsx
β”‚   β”œβ”€β”€ HomePage.tsx
β”‚   β”œβ”€β”€ ResourcesPage.tsx
β”‚   β”œβ”€β”€ AdminDashboard.tsx
β”‚   └── UnauthorizedPage.tsx
β”œβ”€β”€ layouts/           # Layout components
β”‚   └── MainLayout.tsx
β”œβ”€β”€ services/          # API services
β”‚   β”œβ”€β”€ authService.ts
β”‚   └── resourceService.ts
β”œβ”€β”€ contexts/          # React contexts
β”‚   └── AuthContext.tsx
β”œβ”€β”€ hooks/             # Custom hooks
β”‚   └── useAuth.ts
β”œβ”€β”€ types/             # TypeScript type definitions
β”‚   └── index.ts
β”œβ”€β”€ utils/             # Utility functions
β”‚   β”œβ”€β”€ tokenManager.ts
β”‚   └── axiosInstance.ts
β”œβ”€β”€ index.css          # Global styles with Tailwind
β”œβ”€β”€ App.tsx            # Main app component
└── main.tsx           # Entry point

πŸ› οΈ Installation

  1. Clone the repository

    git clone <repository-url>
    cd edu-library
  2. Install dependencies

    npm install
  3. Environment Setup

    cp .env.example .env
  4. Start development server

    npm run dev

The application will be available at http://localhost:5173

πŸ“¦ Dependencies

Core

  • react - UI library
  • react-dom - React DOM renderer
  • react-router-dom - Routing library
  • typescript - Type system

Styling

  • tailwindcss - Utility-first CSS framework
  • postcss - CSS transformations
  • autoprefixer - CSS vendor prefixes

API

  • axios - HTTP client

Development

  • vite - Build tool
  • @vitejs/plugin-react - Vite React plugin
  • @types/react - React type definitions
  • @types/react-dom - React DOM type definitions

πŸ” Authentication

The application uses JWT-based authentication with the following features:

  • Login: Credentials-based authentication
  • Registration: New user registration
  • Token Management: Automatic token storage and retrieval
  • Protected Routes: Routes requiring authentication
  • Role-Based Access: Differentiated access for Admin and User roles
  • Auto-Logout: Automatic logout on token expiration (401 response)

Authentication Flow

  1. User logs in with email/password
  2. Server returns JWT token and user data
  3. Token stored in localStorage
  4. Token added to all API requests via axios interceptor
  5. Protected routes check authentication status
  6. Admin-only routes check user role

🎯 Pages Overview

Public Pages

  • /login - User login
  • /register - User registration

Protected Pages (Authenticated Users Only)

  • / - Home page
  • /resources - Browse educational resources

Admin Pages (Admin Role Only)

  • /admin - Admin dashboard with resource management

Special Pages

  • /unauthorized - Access denied page

πŸ”Œ API Integration

Axios Instance

The axiosInstance in src/utils/axiosInstance.ts automatically:

  • Adds authentication tokens to requests
  • Handles 401 responses with auto-logout
  • Uses the base URL from environment variables

Services

Auth Service (src/services/authService.ts)

  • login(email, password) - User login
  • register(email, password, name) - User registration
  • logout() - User logout
  • verifyToken() - Verify JWT token

Resource Service (src/services/resourceService.ts)

  • getAll() - Fetch all resources
  • getById(id) - Fetch single resource
  • create(resource) - Create new resource
  • update(id, resource) - Update resource
  • delete(id) - Delete resource

🎨 Styling

The project uses Tailwind CSS with custom theme colors:

  • Primary: #3B82F6 (Blue)
  • Secondary: #10B981 (Green)
  • Danger: #EF4444 (Red)
  • Warning: #F59E0B (Amber)
  • Dark: #1F2937 (Dark Gray)
  • Light: #F3F4F6 (Light Gray)

πŸ“ Environment Variables

Create a .env file based on .env.example:

VITE_API_URL=http://localhost:3000/api

πŸš€ Build

npm run build

Builds the application for production. The optimized build will be in the dist directory.

πŸ§ͺ Development

npm run dev

Starts the development server with hot module replacement (HMR).

✨ Key Features Implementation

Protected Routes

Routes are protected using the ProtectedRoute component which:

  • Checks authentication status
  • Redirects unauthenticated users to login
  • Enforces role-based access control
  • Shows loading state during auth verification

Token Management

Tokens are managed using:

  • localStorage for persistence
  • axios interceptors for automatic attachment
  • Custom tokenManager utility for clean API

Context API

Authentication state managed via:

  • AuthContext for global auth state
  • useAuth hook for easy access
  • AuthProvider wrapper component

πŸ”„ Component Architecture

Smart Components (Pages)

  • Handle routing and data fetching
  • Pass data to presentational components

Presentational Components

  • Receive data via props
  • Handle UI rendering and user interactions

Layout Components

  • Wrap page content
  • Provide consistent structure (header, footer, navigation)

πŸ“š Resource Structure

Resources follow this structure:

interface Resource {
  id: string;
  title: string;
  description: string;
  category: string;
  author: string;
  url: string;
  createdAt: string;
  updatedAt: string;
}

πŸ”§ Troubleshooting

CORS Issues

If you encounter CORS errors, ensure your backend is configured to accept requests from localhost:5173 in development.

Token Expiration

Implement token refresh logic in axiosInstance.ts to handle token expiration gracefully.

Styling Issues

Clear cache and restart dev server if Tailwind styles aren't applying:

npm run dev

πŸ“– Additional Resources

🀝 Contributing

Contributions are welcome! Please follow these guidelines:

  1. Create a new branch for your feature
  2. Make your changes with clear commit messages
  3. Ensure code follows the project style
  4. Test your changes thoroughly
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸš€ Next Steps

  1. Connect Backend API: Update API endpoints in services
  2. Implement Real Authentication: Connect to real authentication backend
  3. Add More Features: Implement resource creation, editing, and deletion
  4. Testing: Add unit and integration tests
  5. Error Handling: Implement comprehensive error handling
  6. Analytics: Add application analytics
  7. Performance: Optimize bundle size and performance

Happy coding! πŸŽ‰

export default defineConfig([
  globalIgnores(['dist']),
  {
    files: ['**/*.{ts,tsx}'],
    extends: [
      // Other configs...

      // Remove tseslint.configs.recommended and replace with this
      tseslint.configs.recommendedTypeChecked,
      // Alternatively, use this for stricter rules
      tseslint.configs.strictTypeChecked,
      // Optionally, add this for stylistic rules
      tseslint.configs.stylisticTypeChecked,

      // Other configs...
    ],
    languageOptions: {
      parserOptions: {
        project: ['./tsconfig.node.json', './tsconfig.app.json'],
        tsconfigRootDir: import.meta.dirname,
      },
      // other options...
    },
  },
])

You can also install eslint-plugin-react-x and eslint-plugin-react-dom for React-specific lint rules:

// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'

export default defineConfig([
  globalIgnores(['dist']),
  {
    files: ['**/*.{ts,tsx}'],
    extends: [
      // Other configs...
      // Enable lint rules for React
      reactX.configs['recommended-typescript'],
      // Enable lint rules for React DOM
      reactDom.configs.recommended,
    ],
    languageOptions: {
      parserOptions: {
        project: ['./tsconfig.node.json', './tsconfig.app.json'],
        tsconfigRootDir: import.meta.dirname,
      },
      // other options...
    },
  },
])

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages