Django REST Framework asosida yaratilgan User Management mikroservisi
User Microservice - bu mikroservis arxitekturasi uchun mo'ljallangan mustaqil foydalanuvchi boshqaruv tizimi. JWT autentifikatsiya, user profile management va RESTful API bilan to'liq jihozlangan.
- β JWT Authentication - Secure token-based auth
- β Custom Login - Email-based login
- β Token Refresh - Automatic token renewal
- β Password Security - Hashed passwords
- β Custom User Model - Email as primary identifier
- β User Registration - Ro'yxatdan o'tish API
- β User Profile - Batafsil profil ma'lumotlari
- β Profile Update - Profil yangilash
- β Microservice Design - Mustaqil servis
- β RESTful API - To'liq REST API
- β CORS Enabled - Frontend integratsiya
- β Modular Structure - Apps asosida tuzilgan
- β Scalable - Kengaytirilishi oson
- β User Profile Model - Telefon, manzil, tug'ilgan kun
- β Admin Integration - Django admin panel
- β Logging System - Request logging
- β Timezone Support - Toshkent vaqt zonasi
Backend:
- Python 3.10+
- Django 5.2.5
- Django REST Framework 3.14
- Simple JWT
- django-cors-headers
- SQLite3 (development)
Python 3.10+
pip
virtualenv
- Clone qiling:
git clone https://github.com/psix-coder/user-microservice.git
cd user-microservice
- Virtual environment:
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/Mac
source venv/bin/activate
- Dependencies o'rnating:
pip install django
pip install djangorestframework
pip install djangorestframework-simplejwt
pip install django-cors-headers
Yoki:
pip install -r requirements.txt
- Database yarating:
# databases papkasini yarating
mkdir -p databases
# Migrations
python manage.py migrate
- Superuser:
python manage.py createsuperuser
- Serverni ishga tushiring:
python manage.py runserver
API: http://127.0.0.1:8000/
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
- 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)
- id (AutoField)
- user (OneToOneField β User)
- phone (CharField, max_length=12)
- address (TextField)
- date_of_birth (DateField)
- created_at (DateTimeField)
- updated_at (DateTimeField)
Method | Endpoint | Tavsif | Auth |
---|---|---|---|
POST | /api/auth/login/ |
Tizimga kirish | β |
POST | /api/auth/refresh/ |
Token yangilash | β |
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 | β |
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"
}
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"
}
}
curl -X GET http://127.0.0.1:8000/api/users/profile/ \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
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"
}'
curl -X POST http://127.0.0.1:8000/api/auth/refresh/ \
-H "Content-Type: application/json" \
-d '{
"refresh": "YOUR_REFRESH_TOKEN"
}'
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60), # 1 soat
'REFRESH_TOKEN_LIFETIME': timedelta(days=7), # 7 kun
'ROTATE_REFRESH_TOKENS': True,
}
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000", # Frontend dev
"http://127.0.0.1:3000",
"http://localhost:8000",
"http://127.0.0.1:8000",
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR.parent.parent / 'databases' / 'user.db',
}
}
Bu servis mikroservis arxitekturasida ishlatilishi uchun yaratilgan:
βββββββββββββββββββ
β API Gateway β
ββββββββββ¬βββββββββ
β
ββββββ΄βββββββββββββββββ¬ββββββββββββββ
β β β
βββββΌβββββββ ββββββββΌβββββ ββββββΌβββββββ
β User β β Product β β Order β
β Service β β Service β β Service β
ββββββββββββ βββββββββββββ βββββββββββββ
- Mustaqil deploy
- Alohida database
- RESTful API communication
- JWT token sharing
- CORS enabled
- β JWT token authentication
- β Password hashing (PBKDF2)
- β CORS protection
- β CSRF protection
- β SQL injection protection
- β XSS protection
- 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
python manage.py test
Django==5.2.5
djangorestframework==3.14.0
djangorestframework-simplejwt==5.3.0
django-cors-headers==4.3.0
- Fork qiling
- Branch yarating (
git checkout -b feature/Feature
) - Commit qiling (
git commit -m 'Add Feature'
) - Push qiling (
git push origin feature/Feature
) - Pull Request oching
MIT License
Psix Coder
- GitHub: @psix-coder
β Star qo'yishni unutmang! β
Made with β€οΈ and Django by Psix Coder
π₯ User Microservice - Scalable Authentication Solution