mirror of
https://github.com/usatiuk/y.git
synced 2025-10-29 02:37:49 +01:00
little login fixum
This commit is contained in:
@@ -13,6 +13,7 @@ import { Home } from "./Home";
|
|||||||
import { loginAction, signupAction } from "./actions";
|
import { loginAction, signupAction } from "./actions";
|
||||||
import { homeLoader } from "./loaders";
|
import { homeLoader } from "./loaders";
|
||||||
import { isError } from "./api/dto";
|
import { isError } from "./api/dto";
|
||||||
|
import { Feed } from "./Feed";
|
||||||
|
|
||||||
const router = createBrowserRouter([
|
const router = createBrowserRouter([
|
||||||
{
|
{
|
||||||
@@ -39,6 +40,7 @@ const router = createBrowserRouter([
|
|||||||
return ret;
|
return ret;
|
||||||
},
|
},
|
||||||
element: <Home />,
|
element: <Home />,
|
||||||
|
children: [{ path: "feed", element: <Feed /> }],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/login",
|
path: "/login",
|
||||||
|
|||||||
3
client/src/Feed.tsx
Normal file
3
client/src/Feed.tsx
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export function Feed() {
|
||||||
|
return <a>feed</a>;
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ export function Login() {
|
|||||||
| undefined;
|
| undefined;
|
||||||
|
|
||||||
if (data && !isError(data)) {
|
if (data && !isError(data)) {
|
||||||
return <div className="authForm">Success, now log in!;</div>;
|
return <div className="authForm">Login success</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
let errors: JSX.Element[] = [];
|
let errors: JSX.Element[] = [];
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export function Signup() {
|
|||||||
| undefined;
|
| undefined;
|
||||||
|
|
||||||
if (data && !isError(data)) {
|
if (data && !isError(data)) {
|
||||||
return <div className="authForm">Success, now log in!;</div>;
|
return <div className="authForm">Signup success</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
let errors: JSX.Element[] = [];
|
let errors: JSX.Element[] = [];
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export async function loginAction({ request }: ActionFunctionArgs) {
|
|||||||
formData.get("password")!.toString(),
|
formData.get("password")!.toString(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!isError(ret)) {
|
if (ret && !isError(ret)) {
|
||||||
setToken(ret.token);
|
setToken(ret.token);
|
||||||
return redirect("/home");
|
return redirect("/home");
|
||||||
}
|
}
|
||||||
@@ -21,9 +21,26 @@ export async function loginAction({ request }: ActionFunctionArgs) {
|
|||||||
|
|
||||||
export async function signupAction({ request }: ActionFunctionArgs) {
|
export async function signupAction({ request }: ActionFunctionArgs) {
|
||||||
const formData = await request.formData();
|
const formData = await request.formData();
|
||||||
return await signup(
|
const s = await signup(
|
||||||
formData.get("username")!.toString(),
|
formData.get("username")!.toString(),
|
||||||
formData.get("fullName")!.toString(),
|
formData.get("fullName")!.toString(),
|
||||||
formData.get("password")!.toString(),
|
formData.get("password")!.toString(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!s || isError(s)) {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Login if everything's OK
|
||||||
|
const ret = await login(
|
||||||
|
formData.get("username")!.toString(),
|
||||||
|
formData.get("password")!.toString(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (ret && !isError(ret)) {
|
||||||
|
setToken(ret.token);
|
||||||
|
return redirect("/home");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,16 @@ import jakarta.validation.ConstraintViolationException;
|
|||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.context.request.WebRequest;
|
import org.springframework.web.context.request.WebRequest;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
|
public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
|
||||||
@ExceptionHandler(value = {ConstraintViolationException.class})
|
@ExceptionHandler(value = {ConstraintViolationException.class})
|
||||||
@@ -19,4 +24,25 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
|
|||||||
new ErrorTo(ex.getConstraintViolations().stream().map(ConstraintViolation::getMessage), HttpStatus.BAD_REQUEST.value()),
|
new ErrorTo(ex.getConstraintViolations().stream().map(ConstraintViolation::getMessage), HttpStatus.BAD_REQUEST.value()),
|
||||||
new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(value = {AuthenticationException.class})
|
||||||
|
protected ResponseEntity<Object> handleAuthenticationException(AuthenticationException ex, WebRequest request) {
|
||||||
|
return handleExceptionInternal(ex,
|
||||||
|
new ErrorTo(List.of(ex.getMessage()), HttpStatus.UNAUTHORIZED.value()),
|
||||||
|
new HttpHeaders(), HttpStatus.UNAUTHORIZED, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(value = {ResponseStatusException.class})
|
||||||
|
protected ResponseEntity<Object> handleResponseStatusException(ResponseStatusException ex, WebRequest request) {
|
||||||
|
return handleExceptionInternal(ex,
|
||||||
|
new ErrorTo(List.of(Objects.requireNonNullElse(ex.getReason(), ex.getStatusCode().toString())), ex.getStatusCode().value()),
|
||||||
|
new HttpHeaders(), ex.getStatusCode(), request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(value = {Exception.class})
|
||||||
|
protected ResponseEntity<Object> handleGenericException(Exception ex, WebRequest request) {
|
||||||
|
return handleExceptionInternal(ex,
|
||||||
|
new ErrorTo(List.of("Error"), HttpStatus.INTERNAL_SERVER_ERROR.value()),
|
||||||
|
new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class TokenController {
|
|||||||
public TokenResponseTo request(@RequestBody TokenRequestTo tokenRequestTo) throws UserNotFoundException {
|
public TokenResponseTo request(@RequestBody TokenRequestTo tokenRequestTo) throws UserNotFoundException {
|
||||||
Optional<Person> found = personService.login(tokenRequestTo.username(), tokenRequestTo.password());
|
Optional<Person> found = personService.login(tokenRequestTo.username(), tokenRequestTo.password());
|
||||||
|
|
||||||
if (found.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
if (found.isEmpty()) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Couldn't find user");
|
||||||
|
|
||||||
return new TokenResponseTo(tokenService.generateToken(found.get().getId()));
|
return new TokenResponseTo(tokenService.generateToken(found.get().getId()));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user