feat(auth): 优化注册登录功能,调整头像组件

This commit is contained in:
zyh 2024-10-22 06:44:18 +00:00
parent 2cd653bbdc
commit 7dad0eddd1
3 changed files with 14 additions and 6 deletions

View File

@ -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<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const navigate = useNavigate();
const { login } = useAuth();
const handleAvatarChange = (e: React.ChangeEvent<HTMLInputElement>) => {

View File

@ -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);

View File

@ -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 });