From 01f43ffdc5d9d82270415a7f533bdbe6f45dda6d Mon Sep 17 00:00:00 2001 From: "Khoa.vo" Date: Fri, 19 Dec 2025 15:27:18 +0700 Subject: [PATCH] Add Login with TikTok button using Playwright browser_login --- frontend/src/pages/Login.tsx | 182 ++++++++++++++++++++--------------- 1 file changed, 105 insertions(+), 77 deletions(-) diff --git a/frontend/src/pages/Login.tsx b/frontend/src/pages/Login.tsx index caa3417..b27aa93 100644 --- a/frontend/src/pages/Login.tsx +++ b/frontend/src/pages/Login.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import axios from 'axios'; import { API_BASE_URL } from '../config'; @@ -7,9 +7,53 @@ export const Login: React.FC = () => { const [sessionId, setSessionId] = useState(''); const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); + const [loginStatus, setLoginStatus] = useState(''); const navigate = useNavigate(); - const handleConnect = async () => { + // Check if already authenticated + useEffect(() => { + checkAuth(); + }, []); + + const checkAuth = async () => { + try { + const res = await axios.get(`${API_BASE_URL}/auth/status`); + if (res.data.authenticated) { + navigate('/'); + } + } catch (err) { + // Not authenticated, stay on login + } + }; + + // Try browser login (opens TikTok in server's Playwright browser) + const handleBrowserLogin = async () => { + setError(''); + setIsLoading(true); + setLoginStatus('Opening TikTok login... Please wait'); + + try { + const res = await axios.post(`${API_BASE_URL}/auth/browser-login`, {}, { + timeout: 200000 // 3+ minutes timeout + }); + + if (res.data.status === 'success') { + setLoginStatus('Success! Redirecting...'); + setTimeout(() => navigate('/'), 1000); + } else { + setError(res.data.message || 'Login timed out. Please try the manual method.'); + setIsLoading(false); + setLoginStatus(''); + } + } catch (err: any) { + setError('Connection failed. Please try the manual method below.'); + setIsLoading(false); + setLoginStatus(''); + } + }; + + // Manual sessionid login + const handleManualLogin = async () => { if (!sessionId.trim()) return; setError(''); @@ -27,19 +71,15 @@ export const Login: React.FC = () => { if (res.data.status === 'success') { navigate('/'); } else { - setError('Connection failed. Please check your session ID.'); + setError('Invalid session ID. Please try again.'); } } catch (err: any) { - setError('Invalid session ID. Please try again.'); + setError('Connection failed. Please check your session ID.'); } finally { setIsLoading(false); } }; - const openTikTokLogin = () => { - window.open('https://www.tiktok.com/login', '_blank'); - }; - return (
{/* Header */} @@ -65,88 +105,76 @@ export const Login: React.FC = () => {
)} - {/* Important Note */} -
-

- ⚠️ This requires a desktop/laptop computer with Chrome or Firefox + {/* Primary: Browser Login Button */} +

+ +

+ Opens TikTok login - complete the login and wait

- {/* Steps */} -
-
-
1
-
-

Login to TikTok on desktop

- -
-
- -
-
2
-
-

Open DevTools & copy sessionid

-
-

• Press F12 or Ctrl+Shift+I

-

• Click Application tab

-

• Click Cookiestiktok.com

-

• Find sessionid and copy its value

-
-
-
- -
-
3
-
-

Paste here and connect

- setSessionId(e.target.value)} - placeholder="Paste sessionid value..." - className="w-full bg-black border-2 border-white/10 rounded-lg p-3 text-white text-sm font-mono focus:outline-none focus:border-pink-500/50 placeholder:text-gray-600" - disabled={isLoading} - /> -
-
+ {/* Divider */} +
+
+ or paste manually +
- {/* Connect Button */} - + {/* Manual Method */} +
+

+ If the button above doesn't work, paste your TikTok sessionid cookie: +

+ setSessionId(e.target.value)} + placeholder="Paste sessionid here..." + className="w-full bg-black border border-white/10 rounded-lg p-3 text-white text-sm font-mono focus:outline-none focus:border-pink-500/50 placeholder:text-gray-600 mb-3" + disabled={isLoading} + /> + +
{/* Help */}