Skip to content

farhad0085/dj-user-login-history

Repository files navigation

dj-user-login-history

A Django app which keep track of user login history.

How to install

pip install dj_user_login_history

Usage

  1. Add login_history app to INSTALLED_APPS
INSTALLED_APPS = [
    ...
    'login_history',
    ...
]
  1. Migrate database:
python manage.py migrate
  1. Now all your user's login history will be tracked to a model called LoginHistory
  2. You can check a user's active logins:
user.active_logins
  1. You can add login_history.urls to your project url to see an example page explaining the user login histores.
  2. For better understanding, read following code:
def login_histories(request):

    if not request.user.is_authenticated:
        return HttpResponse("<h1>Please login to see your login histories</h1>")\
        
    active_logins = request.user.active_logins

    active_logins_html = ""
    for login in active_logins:
        active_logins_html += f'<li>{login.ip} - {login.date_time} - {login.user_agent}</li>'

    return HttpResponse(
    f"""
        <h1>Active Logins</h1>
        <ul>
            {active_logins_html}
        </ul>
    """
    )
  1. Use the ip to grab more information directly from the Frontend instead of backend, it'll optimize performance. There're huge JS library which do the work, use any one of them.

NB: When you write your own login view, make sure you log the user in using django login

from django.contrib.auth import login as django_login

django_login(request, user)

About

A Django app which keep track of user login history.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages