From d0655697db4a7bf6f8cf9bf2c4f0d9e507e7785f Mon Sep 17 00:00:00 2001 From: yykai Date: Sun, 12 Jan 2025 08:28:32 +0800 Subject: [PATCH 1/2] New feature: login with guest. Signed-off-by: yykai --- src/pages/Login/index.tsx | 59 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/pages/Login/index.tsx b/src/pages/Login/index.tsx index cfa690c..a638896 100644 --- a/src/pages/Login/index.tsx +++ b/src/pages/Login/index.tsx @@ -38,6 +38,63 @@ const Login: React.FC = () => { } }; + function simpleHash(input: string) { + let hash = 0; + if (input.length === 0) return hash; + + for (let i = 0; i < input.length; i++) { + let char = input.charCodeAt(i); + hash = ((hash << 5) - hash) + char; + hash |= 0; + } + + return hash.toString(); + } + + function generateGuestName() { + const timestamp = Date.now(); + const randomNum = Math.floor(Math.random() * 10000); + return "Guest-" + timestamp + randomNum; + } + + function generateGuestPassword(name: string) { + return simpleHash(name).toString(); + } + + + function generateGuestEmail() { + return "Guest@placeholder.com"; + } + + const handleGuestAuth = async () => { + const userId = localStorage.getItem('userId'); + const userProfile = localStorage.getItem('userProfile'); + const twitterProfile = localStorage.getItem('twitterProfile'); + console.log("Guest info: " + userId + " " + userProfile?.toString().length + " " + twitterProfile?.toString().length); + + if (userId && userProfile && twitterProfile) { + navigate('/plugin/chat'); // already login + return; + } + setLoading(true); + try { + // Guest + const username = generateGuestName(); + const password = generateGuestPassword(username); + const email = generateGuestEmail(); + const credentials = { username, password, email }; + const response = await authService.login(credentials); + console.log("guest auth, res: " + response); + // Navigate to next page + navigate('/egg-select'); + } catch (err) { + console.error('Guest auth error:', err); + setError(err instanceof Error ? err.message : 'Guest authentication failed'); + } finally { + setLoading(false); + } + }; + return (
@@ -94,7 +151,7 @@ const Login: React.FC = () => {