Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/agent/AgentHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import './index.css';
import { useAgentInfo } from '@/hooks/useAgentInfo';

const AgentHeader = () => {
const { level, experience, nextLevelExp } = useAgentInfo();
const { level, experience, nextLevelExp, agentname } = useAgentInfo();

return (
<div className="w-[calc(100%-40px)] mx-[20px] mt-[20px] flex items-center justify-between">
Expand All @@ -15,7 +15,7 @@ const AgentHeader = () => {
</div>
<div className="ml-[16px] flex flex-col items-start justify-center gap-1">
<span className="flex items-center gap-2">
<span className="text-[12px]">Blommy</span>
<span className="text-[12px]">{agentname}</span>
<span className="text-[12px] text-[#39CE78]">Level {level}</span>
</span>
<span className="flex items-center gap-2">
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/useAgentInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ export function useAgentInfo() {
return userProfile?.points || 0;
}, [userProfile]);

const agentname = useMemo(() => {
if(userProfile?.agentname) {
return userProfile?.agentname;
}
return "Blommy";
}, [userProfile]);

return {
level,
experience,
nextLevelExp,
agentname,
points,
refetch,
};
Expand Down
6 changes: 4 additions & 2 deletions src/pages/AgentCustomized/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ const AgentCustomized: React.FC = () => {
throw new Error('User not logged in');
}

var agentname = name;
// 构建profile更新对象
const profileUpdate = {
name,
userId,
agentname,
gender,
bio: [
`I'm ${name}, a ${gender.toLowerCase()} agent with ${agentStyle.toLowerCase()} style`,
`I'm ${agentname}, a ${gender.toLowerCase()} agent with ${agentStyle.toLowerCase()} style`,
`Specializing in ${agentStyle.toLowerCase()} interactions and responses`,
`Ready to engage with unique ${agentStyle.toLowerCase()} perspective`,
],
Expand Down
75 changes: 60 additions & 15 deletions src/pages/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,77 @@ const Login: React.FC = () => {
}
};

const handleGoogleAuth = async () => {
setLoading(true);
try {
// Navigate to next page
navigate('/egg-select');
} catch (err) {
console.error('Google auth error:', err);
setError(err instanceof Error ? err.message : 'Google auth failed');
} finally {
setLoading(false);
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;
}
};

const handleGuest = async () => {
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) {
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.guestLogin(credentials);
console.log("guest auth, res: " + response);
// Navigate to next page
navigate('/egg-select');
} catch (err) {
console.error('Guest error:', err);
setError(err instanceof Error ? err.message : 'Guest login failed');
console.error('Guest auth error:', err);
setError(err instanceof Error ? err.message : 'Guest authentication failed');
} finally {
setLoading(false);
}
};

const handleGoogleAuth = async () => {
setLoading(true);
try {
// Navigate to next page
navigate('/egg-select');
} catch (err) {
console.error('Google auth error:', err);
setError(err instanceof Error ? err.message : 'Google auth failed');
} finally {
setLoading(false);
}
};


return (
<div className="page press-start-2p">
<div className="absolute top-0 left-0 z-[-1] w-full h-full bg-white">
Expand Down Expand Up @@ -120,7 +165,7 @@ const Login: React.FC = () => {
<Button
color={BTNCOLOR.PURPLE}
className="w-auto min-w-[346px] px-[28px] h-[48px] mt-[42px] text-white frc-center gap-[10px]"
onClick={handleGuest}
onClick={handleGuestAuth}
disabled={loading}
>
<img src={guestIcon} alt="guest" className="w-[24px] h-[24px]" />
Expand Down
21 changes: 21 additions & 0 deletions src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ import { useUserStore } from '@/stores/useUserStore';
import api from '@/services/axios';

export const authService = {
/** Guest login */
async guestLogin(credentials: LoginForm): Promise<ApiResponse<LoginResponse['data']>> {
console.log("guestLogin");
try {
const response = await api.post<LoginResponse>('/guest_login', credentials);
if (!response?.data.success) {
throw new Error(response.data.message || 'Login Failed');
}

if (response.data.data) {
useUserStore.getState().login(response.data.data.profile, response.data.data.twitterProfile);
}
return response.data;
} catch (err) {
throw err instanceof Error ? err : new Error('Login Failed');
}
},

/**
* 用户登录
* @param credentials 登录凭证(用户名、密码、邮箱)
Expand Down Expand Up @@ -48,6 +66,9 @@ export const authService = {
username: userId,
profile,
});
if (response.data) {
useUserStore.getState().updateProfile(response.data.profile);
}
return response.data;
} catch (error) {
console.error('Profile update error:', error);
Expand Down
4 changes: 2 additions & 2 deletions src/stores/useUserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const useUserStore = create<UserState>((set, get) => ({
// 持久化到本地存储
localStorage.setItem('userProfile', JSON.stringify(userProfile));
localStorage.setItem('twitterProfile', JSON.stringify(twitterProfile));
localStorage.setItem('userId', userProfile.username);
localStorage.setItem('userId', userProfile?.userId);
},

logout: () => {
Expand All @@ -61,5 +61,5 @@ export const useUserStore = create<UserState>((set, get) => ({
},

// 工具方法
getUserId: () => get().userProfile?.username || null,
getUserId: () => get().userProfile?.userId || null,
}));
2 changes: 2 additions & 0 deletions src/types/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface TwitterProfile {

export interface UserProfile {
username?: string;
userId?: string;
agentname?: string;
email?: string;
avatar?: string;
bio?: string | string[];
Expand Down