randusertry commited on
Commit
7b08cfa
·
verified ·
1 Parent(s): f5b1e50

Upload 19 files

Browse files
web/__pycache__/views.cpython-310.pyc CHANGED
Binary files a/web/__pycache__/views.cpython-310.pyc and b/web/__pycache__/views.cpython-310.pyc differ
 
web/views.py CHANGED
@@ -62,7 +62,7 @@ def ensure_user_session(request):
62
  if request.user.is_authenticated:
63
  return request.user
64
 
65
- # Check URL parameters first, then headers
66
  init_data = request.GET.get('initData') or request.headers.get('X-Telegram-Init-Data')
67
 
68
  if init_data:
@@ -71,6 +71,10 @@ def ensure_user_session(request):
71
  user = get_or_create_tg_user(user_data)
72
  login(request, user)
73
  return user
 
 
 
 
74
  return None
75
 
76
  @csrf_exempt
@@ -92,14 +96,21 @@ def login_page(request):
92
  return render(request, 'web/login.html')
93
 
94
  def index(request):
95
- # If already logged in, show the chats
96
- if request.user.is_authenticated:
97
- chats = request.user.chats.all().order_by('-created_at')
98
- return render(request, 'web/index.html', {'chats': chats, 'user_name': request.user.first_name})
99
 
100
- # If NOT logged in, we still render index.html (which will run the JS login)
101
- return render(request, 'web/index.html', {'chats': [], 'user_name': 'Guest'})
102
-
 
 
 
 
 
 
 
 
 
 
103
  def chat_detail(request, chat_id):
104
  user = ensure_user_session(request)
105
  if not user:
 
62
  if request.user.is_authenticated:
63
  return request.user
64
 
65
+ # Try to get data from URL or Headers
66
  init_data = request.GET.get('initData') or request.headers.get('X-Telegram-Init-Data')
67
 
68
  if init_data:
 
71
  user = get_or_create_tg_user(user_data)
72
  login(request, user)
73
  return user
74
+ else:
75
+ # DEBUG: If validation fails, it's usually a wrong TELEGRAM_TOKEN
76
+ print("TELEGRAM VALIDATION FAILED")
77
+
78
  return None
79
 
80
  @csrf_exempt
 
96
  return render(request, 'web/login.html')
97
 
98
  def index(request):
99
+ user = ensure_user_session(request)
 
 
 
100
 
101
+ if not user:
102
+ # If we are NOT logged in, we still show the index page,
103
+ # but with an empty chat list. The JS will then try to log in.
104
+ return render(request, 'web/index.html', {
105
+ 'chats': [],
106
+ 'user_name': 'Guest'
107
+ })
108
+
109
+ chats = user.chats.all().order_by('-created_at')
110
+ return render(request, 'web/index.html', {
111
+ 'chats': chats,
112
+ 'user_name': user.first_name
113
+ })
114
  def chat_detail(request, chat_id):
115
  user = ensure_user_session(request)
116
  if not user: