From cd5ad2e1e4021a9285ea77352cbbbba1ebe894a4 Mon Sep 17 00:00:00 2001 From: Avnyr Date: Fri, 16 May 2025 14:43:56 +0200 Subject: [PATCH] feat: implement API service, middleware, and authentication context - Added `lib/api.ts` to centralize API communication for authentication, projects, persons, tags, and groups. - Introduced `middleware.ts` to handle route protection based on authentication and roles. - Created `auth-context.tsx` to manage authentication state with `AuthProvider` and `useAuth` hook. - Updated `package.json` to include `swr` for data fetching. - Enhanced project documentation (`RESPONSIVE_DESIGN.md` and `README.md`) with responsive design and architecture details. --- frontend/README.md | 105 +++++++--- frontend/docs/RESPONSIVE_DESIGN.md | 91 +++++++++ frontend/lib/api.ts | 301 +++++++++++++++++++++++++++++ frontend/lib/auth-context.tsx | 145 ++++++++++++++ frontend/middleware.ts | 60 ++++++ frontend/package.json | 1 + 6 files changed, 680 insertions(+), 23 deletions(-) create mode 100644 frontend/docs/RESPONSIVE_DESIGN.md create mode 100644 frontend/lib/api.ts create mode 100644 frontend/lib/auth-context.tsx create mode 100644 frontend/middleware.ts diff --git a/frontend/README.md b/frontend/README.md index e215bc4..c3a98de 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -1,36 +1,95 @@ -This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). +# Frontend Implementation -## Getting Started +This document provides an overview of the frontend implementation for the "Application de Création de Groupes" project. -First, run the development server: +## Architecture -```bash -npm run dev -# or -yarn dev -# or -pnpm dev -# or -bun dev -``` +The frontend is built with Next.js 15 using the App Router architecture. It follows a component-based approach with a clear separation of concerns: -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. +- **app/**: Contains all the pages and layouts organized by route +- **components/**: Reusable UI components +- **lib/**: Utility functions, hooks, and services -You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. +## Authentication Flow -This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel. +The application uses GitHub OAuth for authentication: -## Learn More +1. User clicks "Login with GitHub" on the login page +2. User is redirected to GitHub for authorization +3. GitHub redirects back to our callback page with an authorization code +4. The callback page exchanges the code for an access token +5. User information is stored in the AuthContext and localStorage +6. User is redirected to the dashboard or the original page they were trying to access -To learn more about Next.js, take a look at the following resources: +### Authentication Components -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. +- **AuthProvider**: Context provider that manages authentication state +- **AuthLoading**: Component that displays a loading screen during authentication checks +- **useAuth**: Hook to access authentication state and methods -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome! +## API Communication -## Deploy on Vercel +All API communication is centralized in the `lib/api.ts` file, which provides: -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. +- A base `fetchAPI` function with error handling and authentication +- Specific API modules for different resources (auth, projects, persons, tags, groups) +- Type-safe methods for all API operations -Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details. +## Protected Routes + +All authenticated routes are protected by: + +1. **Middleware**: Redirects unauthenticated users to the login page +2. **AuthLoading**: Shows a loading screen during authentication checks +3. **AuthContext**: Provides user information and authentication methods + +## Layout Structure + +The application uses a nested layout structure: + +- **RootLayout**: Provides global styles and the AuthProvider +- **DashboardLayout**: Provides the sidebar navigation and user interface for authenticated pages +- **AdminLayout**: Provides the admin interface for admin-only pages + +## Components + +### UI Components + +The application uses ShadcnUI for UI components, which provides: + +- A consistent design system +- Accessible components +- Dark mode support + +### Custom Components + +- **dashboard-layout.tsx**: Main layout for authenticated pages +- **auth-loading.tsx**: Loading component for authentication checks +- **admin-layout.tsx**: Layout for admin pages + +## Future Development + +### Adding New Pages + +1. Create a new directory in the `app/` folder +2. Create a `page.tsx` file with your page content +3. Create a `layout.tsx` file that uses the appropriate layout and AuthLoading component + +### Adding New API Endpoints + +1. Add new methods to the appropriate API module in `lib/api.ts` +2. Use the methods in your components with the `useEffect` hook or event handlers + +### Adding New Features + +1. Create new components in the `components/` folder +2. Use the components in your pages +3. Add new API methods if needed + +## Best Practices + +- Use the AuthContext for authentication-related operations +- Use the API service for all API communication +- Wrap authenticated pages with the AuthLoading component +- Use TypeScript for type safety +- Follow the component-based architecture \ No newline at end of file diff --git a/frontend/docs/RESPONSIVE_DESIGN.md b/frontend/docs/RESPONSIVE_DESIGN.md new file mode 100644 index 0000000..6cea565 --- /dev/null +++ b/frontend/docs/RESPONSIVE_DESIGN.md @@ -0,0 +1,91 @@ +# Responsive Design Patterns + +This document outlines the responsive design patterns used in the application to ensure a consistent user experience across different devices and screen sizes. + +## Breakpoints + +The application uses the following breakpoints, based on Tailwind CSS defaults: + +- **sm**: 640px and up (small devices like large phones and small tablets) +- **md**: 768px and up (medium devices like tablets) +- **lg**: 1024px and up (large devices like desktops) +- **xl**: 1280px and up (extra large devices) +- **2xl**: 1536px and up (very large screens) + +## Viewport Configuration + +The application uses the following viewport configuration in the root layout: + +```tsx +export const viewport: Viewport = { + width: "device-width", + initialScale: 1, + maximumScale: 1, + userScalable: true, +}; +``` + +This ensures proper scaling on mobile devices while allowing users to zoom if needed. + +## Layout Patterns + +### Responsive Container + +- Use `container` class with responsive padding: `px-4 md:px-6` +- Example: `
` + +### Responsive Flexbox + +- Stack elements vertically on small screens, horizontally on larger screens +- Example: `
` + +### Responsive Grid + +- Single column on small screens, multiple columns on larger screens +- Example: `
` + +### Responsive Spacing + +- Less padding on small screens, more on larger screens +- Example: `
` + +## Component Patterns + +### Responsive Typography + +- Smaller font sizes on mobile, larger on desktop +- Example: `

` + +### Responsive Buttons + +- Full-width on small screens, auto-width on larger screens +- Example: `