Skip to content

Commit

Permalink
chore: user can send dto object with login endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ramesz6 committed Jul 15, 2024
1 parent 31e13a5 commit 987817d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand Down Expand Up @@ -75,7 +76,9 @@ public Map<String, String> handleValidationExceptions(MethodArgumentNotValidExce
@CrossOrigin (origins = "http://localhost:5173")
@PostMapping ("/login")
public ResponseEntity<?> loginUser(@RequestBody LoginUserDto loginUserDto) {
return ResponseEntity.ok().body(loginUserDto);
Map<String,String> response = new HashMap<>();

Check warning on line 79 in backend/src/main/java/com/greenfoxacademy/backend/controller/UserController.java

View workflow job for this annotation

GitHub Actions / build

[testtool] reported by reviewdog 🐶 ',' is not followed by whitespace. Raw Output: /github/workspace/./backend/src/main/java/com/greenfoxacademy/backend/controller/UserController.java:79:15: warning: ',' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck)
response.put("Response","Login OK");

Check warning on line 80 in backend/src/main/java/com/greenfoxacademy/backend/controller/UserController.java

View workflow job for this annotation

GitHub Actions / build

[testtool] reported by reviewdog 🐶 ',' is not followed by whitespace. Raw Output: /github/workspace/./backend/src/main/java/com/greenfoxacademy/backend/controller/UserController.java:80:28: warning: ',' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck)
return ResponseEntity.ok().body(response);
}

}
Expand Down
77 changes: 49 additions & 28 deletions frontend/src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,70 @@ import { Link } from "react-router-dom";
export function Login() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
console.log(email)
console.log(password)

const handleLogin = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const handleLogin = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

fetch('http://localhost:8080/login', {
mode: 'cors',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: email,
password: password,
})
body: JSON.stringify({ email, password })
})
}
.then(async res => {
// Ellenőrizd a válasz státuszát
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}

// Próbáld meg parse-olni a választ
const data = await res.json().catch(() => {
throw new Error('Invalid JSON response');
});

// Ha minden rendben, logold ki a választ
console.log(data);
})
.catch(error => console.error('Error:', error));
};

const saveFormData = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
switch (name) {
case 'email':
setEmail(value);
break;
case 'password':
setPassword(value);
break;
default:
break;
if (name === 'email') {
setEmail(value);
} else if (name === 'password') {
setPassword(value);
}
};

return (
<>
<h1>Login</h1>
<form onSubmit={handleLogin}>
<input type='email' name={"email"} value={email} onChange={saveFormData}></input>
<label>Password:</label>
<input type='password' aria-label={"pass"} name={"password"} value={password} onChange={saveFormData}></input>
<button type='submit'>Login</button>
<label htmlFor="email">Email:</label>
<input
type="email"
name="email"
id="email"
value={email}
onChange={saveFormData}
required
/>
<label htmlFor="password">Password:</label>
<input
type="password"
name="password"
id="password"
value={password}
onChange={saveFormData}
required
/>
<button type="submit">Login</button>
</form>
<Link className={"links"} to='/'>Main</Link>
<Link className={"links"} to='/register'>Register</Link>
<Link className="links" to="/">Main</Link>
<Link className="links" to="/register">Register</Link>
</>
)
}
);
}

0 comments on commit 987817d

Please sign in to comment.