我無法驗(yàn)證我的項(xiàng)目的登錄頁面
我的注冊(cè)頁面將獲取數(shù)據(jù)并發(fā)布到后端,所以我嘗試驗(yàn)證登錄。然而,它每次都失敗
LoginController.java
package com.example.medicalwebsite.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.medicalwebsite.entity.LoginEntity;
import com.example.medicalwebsite.service.LoginService;
@RestController
public class LoginController {
@Autowired
private LoginService loginService;
@PostMapping("/login")
public String login(@RequestBody LoginEntity loginEntity) {
boolean isAuthenticated = loginService.authenticate(loginEntity.getEmail(), loginEntity.getPassword());
if (isAuthenticated) {
return "Login successful";
} else {
return "Invalid email or password";
}
}
}
LoginService.java
package com.example.medicalwebsite.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.medicalwebsite.entity.SignupEntity;
import com.example.medicalwebsite.repo.SignupRepo;
@Service
public class LoginService {
@Autowired
private SignupRepo signupRepository;
public boolean authenticate(String email, String password) {
SignupEntity signupEntity = signupRepository.findByEmail(email);
if (signupEntity != null && signupEntity.getPassword().equals(password)) {
return true;
}
return false;
}
}
LoginRepo.java
package com.example.medicalwebsite.service;
public interface LoginServiceInt {
public boolean authenticate(String email, String password);
}
前端(loginpage.js)
import React, { useState } from "react";
import axios from "axios";
import './loginpage.css';
import { Link, useNavigate } from "react-router-dom";
const LoginPage = () => {
const navigate = useNavigate();
const [emailValue, setEmailValue] = useState("");
const [passwordValue, setPasswordValue] = useState("");
const handleEmailChange = (event) => {
setEmailValue(event.target.value);
};
const handlePasswordChange = (event) => {
setPasswordValue(event.target.value);
};
const handleFormSubmit = async (event) => {
event.preventDefault();
if (emailValue === "") {
alert("Please enter your email address.");
} else if (passwordValue === "") {
alert("Please enter your password.");
} else {
try {
// Send a request to the backend API to validate the login credentials
const response = await axios.post("http://localhost:8080/login", {
email: emailValue,
password: passwordValue,
});
if (response.data.valid) {
// Login successful, redirect to the home page
navigate("/");
} else {
// Login failed, display appropriate error message
console.log("Login failed");
// Display an error message to the user
}
} catch (error) {
console.log("Error:", error);
}
}
};
return (
<div className="login">
<div className="left-container">
<h1 className="logintext1">Login to Your Account</h1>
<form onSubmit={handleFormSubmit}>
<div className="email">
<input
type="email"
id="email"
name="email"
placeholder="Email"
value={emailValue}
onChange={handleEmailChange}
className="login-input"
/>
</div>
<div className="password">
<input
type="password"
id="password"
name="password"
placeholder="Password"
value={passwordValue}
onChange={handlePasswordChange}
className="login-input"
/>
<h6 className="forgot">Forgot Password?</h6>
<span className="loginlink" onClick={handleFormSubmit}>Login</span>
</div>
</form>
</div>
<div className="right-container">
<h1 className="newhere">Discover what awaits you - Sign up today.</h1>
<p className="newheretext">
Join now to unlock exclusive access to top-tier medications and book hassle-free appointments with your preferred doctors! Don't wait - take control of your health today!{" "}
</p>
<div className="signup">
<Link to="/signup">Sign Up</Link>
</div>
</div>
</div>
);
};
導(dǎo)出默認(rèn)登錄頁面;
為了我的注冊(cè),
SignupController.java
package com.example.medicalwebsite.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.medicalwebsite.entity.SignupEntity;
import com.example.medicalwebsite.service.SignupService;
@RestController
public class SignupController {
@Autowired
private SignupService ss;
@GetMapping("/signup")
public List<SignupEntity> signup() {
return ss.getSignup();
}
@PostMapping("/post")
public String post(@RequestBody SignupEntity se) {
ss.save(se);
return "Data has been added";
}
@DeleteMapping("/delete")
public String delete(@RequestParam int id) {
ss.delete(id);
return "id " + id + " is deleted";
}
@GetMapping("/login")
public String login(@RequestParam String email, @RequestParam String password) {
SignupEntity signupEntity = ss.findByEmail(email);
if (signupEntity != null && signupEntity.getPassword().equals(password)) {
return "Login successful";
} else {
return "Invalid email or password";
}
}
}
SignupService.java
package com.example.medicalwebsite.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.medicalwebsite.entity.SignupEntity;
import com.example.medicalwebsite.repo.SignupRepo;
@Service
public class SignupService {
@Autowired
private SignupRepo signupRepository;
public List<SignupEntity> getSignup() {
return signupRepository.findAll();
}
public SignupEntity save(SignupEntity se) {
return signupRepository.save(se);
}
public void delete(int id) {
signupRepository.deleteById(id);
}
public SignupEntity findByEmail(String email) {
return signupRepository.findByEmail(email);
}
}