Skip to content

Commit

Permalink
update register & login
Browse files Browse the repository at this point in the history
  • Loading branch information
Hhhhhhhharu committed Jun 13, 2024
1 parent de139a9 commit 0fdf441
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 19 deletions.
107 changes: 102 additions & 5 deletions frontend/wallet/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frontend/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"antd": "^5.18.0",
"ethers": "^6.12.1",
"axios": "^1.7.2",
"ethers": "^6.13.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.23.1",
Expand Down Expand Up @@ -45,6 +46,6 @@
]
},
"devDependencies": {
"typescript": "^5.4.5"
"typescript": "^4.9.5"
}
}
24 changes: 18 additions & 6 deletions frontend/wallet/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@ import '../css/Login.css'
function Login() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [message, setMessage] = useState('');
const navigate = useNavigate();

const handleSubmit = (e) => {
const handleSubmit = async (e) => {
e.preventDefault();
// 处理登录逻辑,例如发送请求到服务器进行身份验证
console.log('Logging in with:', email, password);
// 登录成功后,可以跳转到其他页面,例如首页
if(email === '111' && password === '111'){
navigate('/home');
}else{
alert('Invalid username or password')
try{
const response = await axios.post('/user/sign-in', { email, password });

// 登录成功后,可以跳转到其他页面,例如首页
if(response.data.success){
setMessage('登录成功')
localStorage.setItem('token', response.data.token);
navigate('/home');
}else{
setMessage('登录失败:'+response.data.message)
}
}catch(error){
console.error(error);
setMessage('登录失败: 服务器错误');
}


};

return (
Expand Down
16 changes: 15 additions & 1 deletion frontend/wallet/src/components/Register.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import Button from '../components/Button';
import { createAccount, createWallet } from '../util/wallet.js';
import axios from 'axios';

function Register() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [message, setMessage] = useState('');

const handleSubmit = (e) => {
const handleSubmit = async (e) => {
e.preventDefault();
// 处理注册逻辑,例如发送请求到服务器进行用户创建
console.log('Registering with:', email, password);
try{
const response = await axios.post('/user/sign-up', { email, password });
setMessage(response.data.message);
}catch(error){
console.error(error);
setMessage('Registration failed');
}
// 调用钱包注册方法
// const result = createWallet;
// console.log(result);
// 重定向到登录页面
window.location.href = '/login';
};
Expand All @@ -31,6 +44,7 @@ function Register() {
Confirm
</Button>
</form>
{message && <p>{message}</p>}
<p>Already have an account? <Link to="/login">Login</Link></p>
</div>
</div>
Expand Down
Loading

0 comments on commit 0fdf441

Please sign in to comment.