Login Alerts
Off by default since v2.3.370. Turn it on with account_emails=AccountEmailsConfig(new_sign_in_email=True). A user who has just signed in does not need a letter about it; enable it for projects where a second-device warning is worth the email.
from django_cfg import AccountEmailsConfig, DjangoConfig
class MyConfig(DjangoConfig):
account_emails = AccountEmailsConfig(new_sign_in_email=True) # default FalseApple-style login notification emails sent when users sign in from a new device or a new country. Detects potentially unauthorized access while suppressing false alarms with intelligent deduplication.
Features
- Device detection — browser, OS, and device type parsed from User-Agent
- Location — country (city only at
visitor_geo.level="city") fromrequest.geo - Real client IP — resolved by the edge trust rules, not taken from
X-Forwarded-For - Non-blocking — all work deferred to background thread
- Smart suppression — avoids alert fatigue with dedup rules
- Telegram integration — optional admin notifications for staff logins
Usage
Call send_login_alert after issuing JWT tokens in your login view:
from django_cfg.apps.system.accounts.services.login_alert_service import send_login_alert
@action(detail=False, methods=['post'])
def login(self, request):
user = authenticate(...)
tokens = issue_tokens(user)
# Trigger login alert (deferred, non-blocking)
send_login_alert(user, request)
return Response({"access": tokens.access, ...})Suppression Rules
Alerts are suppressed in these cases (applied in order):
| Rule | Description |
|---|---|
| Switch off | account_emails.new_sign_in_email is False (the default); the sign-in is still logged |
| Test accounts | user.is_test_account=True — never alerted |
| Registration grace | First 24h after signup (OTP or OAuth) — a brand-new account never gets one |
| Recent same-device | Same country + browser + OS seen in the past 24h — no repeat. When the country is unknown, the IP stands in for it |
Configuration
| Setting | Default | Description |
|---|---|---|
| Registration grace period | 24 hours | Suppress alerts for new users |
| Dedup window | 24 hours | Same country + browser + OS won’t trigger a repeat alert |
A phone changes IP all day; a new country is what a stolen session looks like. So an IP change inside one country does not alert again within 24 hours, and a sign-in from a new country always does. Behind Cloudflare the country comes from the edge; elsewhere it is empty unless you set up the geoip2 fallback, and dedup falls back to the IP.
Email Template
Template: emails/login_alert_email
Context variables:
| Variable | Description |
|---|---|
user | CustomUser instance |
device | Friendly label (e.g., “Chrome on Windows (mobile)“) |
location | "Tbilisi, Georgia", "Georgia" or ""; the Location row is shown only when it is set |
ip_address | Client IP address ("Unknown" when none was resolved) |
login_time | Formatted timestamp (“April 03, 2026 at 14:32 UTC”) |
project_name | DjangoConfig.project_name |
site_name, site_url, support_email, logo_url | From the user’s site; templates read site_name |
button_url | The site’s site_url, for the “review activity” button |
The subject is the gettext string "New sign-in to your %(project_name)s account", unless a stored EmailContent subject replaces it. The recipient’s language is user.language, then the site’s locale. The site key is captured from the request on the request thread, so the background job speaks for the site the sign-in happened on.
Profile change email
The sibling letter “Security Alert: Profile Updated” is also opt-in: AccountEmailsConfig(profile_change_email=True). When on, it is sent to the previous address only when the account’s email address changes. A name or username change, a first fill of an empty email, and anything written during sign-in or registration never send it. Telegram and admin notifications about profile changes are unchanged.
How It Works
send_login_alert(user, request)builds aLoginContextfrom the request (IP, location, User-Agent, fingerprint)- Suppression rules are evaluated in order
- If not suppressed, an email is sent via
DjangoEmailService - Login activity is recorded with the fingerprint and the location columns, for future dedup
- All steps run in a background thread — auth response is never delayed
Sign-in history
Every sign-in is a UserActivity row (activity_type="login"), written whether or not an email is sent. Besides ip_address and user_agent it has location columns, filled from request.geo at the time:
| Column | Holds |
|---|---|
country | ISO 3166-1 alpha-2, indexed; "" when unknown |
region_code | Region code, at visitor_geo.level="region" or above |
city | City, only at visitor_geo.level="city" |
timezone | IANA time zone, at region or above |
edge_trusted | The request came through the trusted edge, so the IP and country are real |
They are columns, so “sign-ins from GE” is a plain filter: UserActivity.objects.filter(country="GE"). The admin’s activity list has a Location column and filters for country and edge_trusted. The columns come with migration django_cfg_accounts 0029_user_activity_geo; run migrate.
Retention. A daily RQ job, cleanup_user_activity, deletes activity rows (and the IPs on them) older than 90 days. It is part of the RQ auto-cleanup schedules (DjangoRQConfig.enable_auto_cleanup, on by default), so it runs where rqcron runs.
Rows written before this release hold whatever X-Forwarded-For[0] was. Behind Traefik and Cloudflare that was a Cloudflare edge address, not the user’s; they age out with the retention.