Skip to content

t1m4/social-network-with-jwt-authentication

Repository files navigation

Overview

It's simple REST API for everyday social network.

Created using Django-Rest-Framework using JWT authentication

Basic Features:

  • User signup
  • User login
  • Post creation
  • Like post
  • Unlike post

Installation

1. Install all requirements.

pip install requirements.txt

  1. Run tests...

python manage.py test

  1. Add .env file to main and automated_bot/ directories.

  2. And if everything all right start server.

python manage.py runserver

Basic API Features

Post creation using POST request.

1. Sign up example.

{
  "username": "test",
  "email": "[email protected]",
  "password": "password",
  "double_password": "password"
}

2. Login example.

{
  "username": "test",
  "password": "password"
}

3. Post creation example.

{
  "title": "test",
  "description": "I love testing!"
}

4. Post like/unlike example.

{
  "post_id": "test"
}

5. Analytics point example.

GET /facebook/api/analitics/?date_from=2020-02-02&date_to=2020-02-15

6. Activity point example.

GET /facebook/api/activity/?username=test
{
  "last_login": "2021-07-19 11:31:55",
  "last_request": "2021-07-19 11:48:37"
}

Authentication Using JWT

1. Override default User model

2. Override default UserManager model

3. Add rest_framework_simplejwt library

INSTALLED_APPS += [
    'rest_framework_simplejwt'
]

Automated bot

1. Start bot from automated_bot/

python async_bot.py

2.The bot use data from automated_bot/.env</ file

number_of_users=5
max_posts_per_user=7
max_likes_per_user=8
  1. Sign Up number_of_users users

  2. Each user creates random number of posts, but maximum max_posts_per_user

  3. Each user randomly like max_likes_per_user posts

Security Tips

1. Сheck password strength

2. Add lifetime for tokens

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=30),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
}

3. Add throttling to your views. Configure it for yourself.

# settings.py
REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.AnonRateThrottle',
        'rest_framework.throttling.UserRateThrottle'
    ],
    'DEFAULT_THROTTLE_RATES': {
        'anon': '50/day',
        'user': '1000/day'
    }
# views.py
class RegisterAPIView(APIView):
    throttle_classes = [AnonRateThrottle]

Releases

No releases published

Packages

No packages published

Languages