Skip to content

πŸ‘₯ User Microservice - JWT authentication, user profiles, va Django REST Framework asosida yaratilgan mikroservis. Custom User Model, CORS, Registration/Login API endpoints.

Notifications You must be signed in to change notification settings

psix-coder/Microservice-shop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ‘₯ User Microservice

Django DRF JWT Python Microservice

Django REST Framework asosida yaratilgan User Management mikroservisi

πŸ“– Loyiha haqida

User Microservice - bu mikroservis arxitekturasi uchun mo'ljallangan mustaqil foydalanuvchi boshqaruv tizimi. JWT autentifikatsiya, user profile management va RESTful API bilan to'liq jihozlangan.

✨ Asosiy xususiyatlar

πŸ” Authentication

  • βœ… JWT Authentication - Secure token-based auth
  • βœ… Custom Login - Email-based login
  • βœ… Token Refresh - Automatic token renewal
  • βœ… Password Security - Hashed passwords

πŸ‘€ User Management

  • βœ… Custom User Model - Email as primary identifier
  • βœ… User Registration - Ro'yxatdan o'tish API
  • βœ… User Profile - Batafsil profil ma'lumotlari
  • βœ… Profile Update - Profil yangilash

πŸ—οΈ Architecture

  • βœ… Microservice Design - Mustaqil servis
  • βœ… RESTful API - To'liq REST API
  • βœ… CORS Enabled - Frontend integratsiya
  • βœ… Modular Structure - Apps asosida tuzilgan
  • βœ… Scalable - Kengaytirilishi oson

πŸ“Š Features

  • βœ… User Profile Model - Telefon, manzil, tug'ilgan kun
  • βœ… Admin Integration - Django admin panel
  • βœ… Logging System - Request logging
  • βœ… Timezone Support - Toshkent vaqt zonasi

πŸ› οΈ Texnologiyalar

Python Django DRF JWT SQLite

Backend:

  • Python 3.10+
  • Django 5.2.5
  • Django REST Framework 3.14
  • Simple JWT
  • django-cors-headers
  • SQLite3 (development)

πŸš€ O'rnatish

Talablar

Python 3.10+
pip
virtualenv

Qadamlar

  1. Clone qiling:
git clone https://github.com/psix-coder/user-microservice.git
cd user-microservice
  1. Virtual environment:
python -m venv venv

# Windows
venv\Scripts\activate

# Linux/Mac
source venv/bin/activate
  1. Dependencies o'rnating:
pip install django
pip install djangorestframework
pip install djangorestframework-simplejwt
pip install django-cors-headers

Yoki:

pip install -r requirements.txt
  1. Database yarating:
# databases papkasini yarating
mkdir -p databases

# Migrations
python manage.py migrate
  1. Superuser:
python manage.py createsuperuser
  1. Serverni ishga tushiring:
python manage.py runserver

API: http://127.0.0.1:8000/

πŸ“ Loyiha strukturasi

user-microservice/
β”œβ”€β”€ apps/
β”‚   β”œβ”€β”€ authentication/        # Auth endpoints
β”‚   β”‚   β”œβ”€β”€ views.py          # Login, refresh views
β”‚   β”‚   └── urls.py           # Auth URLs
β”‚   └── users/                # User management
β”‚       β”œβ”€β”€ models.py         # User & Profile models
β”‚       β”œβ”€β”€ serializers.py    # DRF serializers
β”‚       β”œβ”€β”€ views.py          # User views
β”‚       β”œβ”€β”€ urls.py           # User URLs
β”‚       └── admin.py          # Admin config
β”œβ”€β”€ config/                   # Project settings
β”‚   β”œβ”€β”€ settings.py          # Django settings
β”‚   β”œβ”€β”€ urls.py              # Root URLs
β”‚   └── wsgi.py              # WSGI config
β”œβ”€β”€ databases/               # Database files
β”‚   └── user.db             # SQLite database
β”œβ”€β”€ manage.py
└── README.md

πŸ—„οΈ Ma'lumotlar bazasi

User Model

- id (AutoField)
- email (EmailField, unique) # USERNAME_FIELD
- username (CharField, unique)
- first_name (CharField)
- last_name (CharField)
- password (CharField, hashed)
- is_active (BooleanField)
- is_staff (BooleanField)
- is_superuser (BooleanField)
- date_joined (DateTimeField)
- last_login (DateTimeField)

UserProfile Model

- id (AutoField)
- user (OneToOneField β†’ User)
- phone (CharField, max_length=12)
- address (TextField)
- date_of_birth (DateField)
- created_at (DateTimeField)
- updated_at (DateTimeField)

πŸ”Œ API Endpoints

Authentication

Method Endpoint Tavsif Auth
POST /api/auth/login/ Tizimga kirish ❌
POST /api/auth/refresh/ Token yangilash ❌

User Management

Method Endpoint Tavsif Auth
POST /api/users/register/ Ro'yxatdan o'tish ❌
GET /api/users/profile/ Profil ko'rish βœ…
PUT/PATCH /api/users/profile/update/ Profil yangilash βœ…

πŸ’» API Foydalanish misollari

1. Ro'yxatdan o'tish (Register)

curl -X POST http://127.0.0.1:8000/api/users/register/ \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "username": "johndoe",
    "first_name": "John",
    "last_name": "Doe",
    "password": "securepass123",
    "password_confirm": "securepass123"
  }'

Response:

{
  "id": 1,
  "email": "user@example.com",
  "username": "johndoe",
  "first_name": "John",
  "last_name": "Doe"
}

2. Login (Kirish)

curl -X POST http://127.0.0.1:8000/api/auth/login/ \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "securepass123"
  }'

Response:

{
  "access": "eyJ0eXAiOiJKV1QiLCJhbGc...",
  "refresh": "eyJ0eXAiOiJKV1QiLCJhbGc...",
  "user": {
    "id": 1,
    "email": "user@example.com",
    "username": "johndoe",
    "first_name": "John",
    "last_name": "Doe"
  }
}

3. Profile olish

curl -X GET http://127.0.0.1:8000/api/users/profile/ \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

4. Profile yangilash

curl -X PATCH http://127.0.0.1:8000/api/users/profile/update/ \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "phone": "+998901234567",
    "address": "Toshkent, O'zbekiston",
    "date_of_birth": "1990-01-01"
  }'

5. Token yangilash

curl -X POST http://127.0.0.1:8000/api/auth/refresh/ \
  -H "Content-Type: application/json" \
  -d '{
    "refresh": "YOUR_REFRESH_TOKEN"
  }'

βš™οΈ Sozlamalar

JWT Configuration

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),  # 1 soat
    'REFRESH_TOKEN_LIFETIME': timedelta(days=7),     # 7 kun
    'ROTATE_REFRESH_TOKENS': True,
}

CORS Settings

CORS_ALLOWED_ORIGINS = [
    "http://localhost:3000",    # Frontend dev
    "http://127.0.0.1:3000",
    "http://localhost:8000",
    "http://127.0.0.1:8000",
]

Database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR.parent.parent / 'databases' / 'user.db',
    }
}

πŸ—οΈ Mikroservis Arxitekturasi

Bu servis mikroservis arxitekturasida ishlatilishi uchun yaratilgan:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   API Gateway   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚                     β”‚             β”‚
β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
β”‚   User   β”‚      β”‚  Product  β”‚  β”‚  Order    β”‚
β”‚ Service  β”‚      β”‚  Service  β”‚  β”‚  Service  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Xususiyatlar:

  • Mustaqil deploy
  • Alohida database
  • RESTful API communication
  • JWT token sharing
  • CORS enabled

πŸ”’ Xavfsizlik

  • βœ… JWT token authentication
  • βœ… Password hashing (PBKDF2)
  • βœ… CORS protection
  • βœ… CSRF protection
  • βœ… SQL injection protection
  • βœ… XSS protection

πŸ“ˆ Kelajakdagi rejalar

  • OAuth2 integration (Google, Facebook)
  • Email verification
  • Password reset
  • Two-factor authentication (2FA)
  • Role-based access control (RBAC)
  • PostgreSQL migration
  • Redis caching
  • Celery async tasks
  • Docker containerization
  • Kubernetes deployment
  • API rate limiting
  • Swagger documentation
  • Unit tests
  • CI/CD pipeline

πŸ§ͺ Testing

python manage.py test

πŸ“ requirements.txt

Django==5.2.5
djangorestframework==3.14.0
djangorestframework-simplejwt==5.3.0
django-cors-headers==4.3.0

🀝 Hissa qo'shish

  1. Fork qiling
  2. Branch yarating (git checkout -b feature/Feature)
  3. Commit qiling (git commit -m 'Add Feature')
  4. Push qiling (git push origin feature/Feature)
  5. Pull Request oching

πŸ› Issues

GitHub Issues

πŸ“„ License

MIT License

πŸ‘€ Muallif

Psix Coder


⭐ Star qo'yishni unutmang! ⭐

Made with ❀️ and Django by Psix Coder

πŸ‘₯ User Microservice - Scalable Authentication Solution

About

πŸ‘₯ User Microservice - JWT authentication, user profiles, va Django REST Framework asosida yaratilgan mikroservis. Custom User Model, CORS, Registration/Login API endpoints.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages