From 7dad0eddd1d11af6a495235a5bd9fdadc9fedf9b Mon Sep 17 00:00:00 2001 From: zyh Date: Tue, 22 Oct 2024 06:44:18 +0000 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E4=BC=98=E5=8C=96=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD=EF=BC=8C=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E5=A4=B4=E5=83=8F=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/components/auth/Register.tsx | 2 -- app/components/ui/Avatar.tsx | 2 +- app/routes/api.auth.login.ts | 16 +++++++++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/components/auth/Register.tsx b/app/components/auth/Register.tsx index 406c246..142b95a 100644 --- a/app/components/auth/Register.tsx +++ b/app/components/auth/Register.tsx @@ -1,5 +1,4 @@ import React, { useState, useRef } from 'react'; -import { useNavigate } from '@remix-run/react'; import { useAuth } from '~/hooks/useAuth'; import type { RegisterResponse } from '~/routes/api.auth.register'; import { uploadToOSS } from '~/utils/uploadToOSS'; @@ -14,7 +13,6 @@ export function Register() { const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const fileInputRef = useRef(null); - const navigate = useNavigate(); const { login } = useAuth(); const handleAvatarChange = (e: React.ChangeEvent) => { diff --git a/app/components/ui/Avatar.tsx b/app/components/ui/Avatar.tsx index d37dc84..6e47dbf 100644 --- a/app/components/ui/Avatar.tsx +++ b/app/components/ui/Avatar.tsx @@ -7,7 +7,7 @@ interface AvatarProps { className?: string; } -export function Avatar({ src, alt, className = '' }: AvatarProps) { +export function Avatar({ src = '', alt, className = '' }: AvatarProps) { const [imgSrc, setImgSrc] = useState(src.startsWith('http') ? src : `${env.OSS_HOST}${src}`); const [error, setError] = useState(false); diff --git a/app/routes/api.auth.login.ts b/app/routes/api.auth.login.ts index 501d04d..d944be4 100644 --- a/app/routes/api.auth.login.ts +++ b/app/routes/api.auth.login.ts @@ -16,7 +16,7 @@ export interface LoginResponse { } export const action: ActionFunction = async ({ request }) => { - const { phone, password } = await request.json() as { phone: string, password: string }; + const { phone, password } = (await request.json()) as { phone: string; password: string }; if (!validatePhoneNumber(phone)) { return json({ error: '无效的手机号码' }, { status: 400 }); @@ -27,9 +27,19 @@ export const action: ActionFunction = async ({ request }) => { if (!user) { return json({ error: '手机号或密码不正确' }, { status: 401 }); } - + const token = createToken(user._id.toString()); - return json({ token }); + const response: LoginResponse = { + success: true, + token, + user: { + id: user._id, + phone: user.phone, + nickname: user.nickname, + avatarUrl: user.avatar_url, + }, + }; + return json(response); } catch (error) { console.error('Login error:', error); return json({ error: '登录失败,请稍后再试' }, { status: 500 });