From 7089515e47fb6f7e9aa3db4bbbac2d3300ff0048 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 11 Aug 2024 15:32:47 +0900 Subject: feat(frontend): improve error handling of login form --- backend/api/handler.go | 14 ++- frontend/app/.server/auth.ts | 5 +- frontend/app/components/BorderedContainer.tsx | 13 +++ frontend/app/components/InputText.tsx | 12 +++ frontend/app/components/SubmitButton.tsx | 12 +++ frontend/app/routes/_index.tsx | 25 +++-- frontend/app/routes/login.tsx | 150 +++++++++++++++++--------- 7 files changed, 167 insertions(+), 64 deletions(-) create mode 100644 frontend/app/components/BorderedContainer.tsx create mode 100644 frontend/app/components/InputText.tsx create mode 100644 frontend/app/components/SubmitButton.tsx diff --git a/backend/api/handler.go b/backend/api/handler.go index 134a4f9..5b53791 100644 --- a/backend/api/handler.go +++ b/backend/api/handler.go @@ -31,9 +31,19 @@ func (h *Handler) PostLogin(ctx context.Context, request PostLoginRequestObject) userID, err := auth.Login(ctx, h.q, username, password, registrationToken) if err != nil { log.Printf("login failed: %v", err) + var msg string + if errors.Is(err, auth.ErrInvalidRegistrationToken) { + msg = "登録用 URL が無効です。イベントスタッフにお声がけください" + } else if errors.Is(err, auth.ErrNoRegistrationToken) { + msg = "登録用 URL からログインしてください。登録用 URL は Connpass のイベントページに記載しています" + } else if errors.Is(err, auth.ErrForteeLoginTimeout) { + msg = "ログインに失敗しました" + } else { + msg = "ユーザー名またはパスワードが誤っています" + } return PostLogin401JSONResponse{ UnauthorizedJSONResponse: UnauthorizedJSONResponse{ - Message: "Invalid username or password", + Message: msg, }, }, nil } @@ -42,7 +52,7 @@ func (h *Handler) PostLogin(ctx context.Context, request PostLoginRequestObject) if err != nil { return PostLogin401JSONResponse{ UnauthorizedJSONResponse: UnauthorizedJSONResponse{ - Message: "Invalid username or password", + Message: "ログインに失敗しました", }, }, nil } diff --git a/frontend/app/.server/auth.ts b/frontend/app/.server/auth.ts index 943f424..4df0924 100644 --- a/frontend/app/.server/auth.ts +++ b/frontend/app/.server/auth.ts @@ -32,9 +32,12 @@ export type User = components["schemas"]["User"]; // Remix's createCookie() returns "structured" cookies, which cannot be reused directly by non-Remix servers. const tokenCookie = createUnstructuredCookie("albatross_token", cookieOptions); +/** + * @throws Error on failure + */ export async function login(request: Request): Promise { const jwt = await authenticator.authenticate("default", request, { - failureRedirect: request.url, + throwOnError: true, }); const session = await sessionStorage.getSession( diff --git a/frontend/app/components/BorderedContainer.tsx b/frontend/app/components/BorderedContainer.tsx new file mode 100644 index 0000000..cbbfbde --- /dev/null +++ b/frontend/app/components/BorderedContainer.tsx @@ -0,0 +1,13 @@ +import React from "react"; + +type Props = { + children: React.ReactNode; +}; + +export default function BorderedContainer({ children }: Props) { + return ( +
+ {children} +
+ ); +} diff --git a/frontend/app/components/InputText.tsx b/frontend/app/components/InputText.tsx new file mode 100644 index 0000000..3f2c526 --- /dev/null +++ b/frontend/app/components/InputText.tsx @@ -0,0 +1,12 @@ +import React from "react"; + +type InputProps = React.InputHTMLAttributes; + +export default function InputText(props: InputProps) { + return ( + + ); +} diff --git a/frontend/app/components/SubmitButton.tsx b/frontend/app/components/SubmitButton.tsx new file mode 100644 index 0000000..1400a7b --- /dev/null +++ b/frontend/app/components/SubmitButton.tsx @@ -0,0 +1,12 @@ +import React from "react"; + +type ButtonProps = React.ButtonHTMLAttributes; + +export default function SubmitButton(props: ButtonProps) { + return ( + - +
+ +
+

+ fortee アカウントでログイン +

+

+ fortee + のアカウントをお持ちでない場合は、イベントスタッフにお声がけください。 +

+ {loginErrors?.message && ( +

{loginErrors.message}

+ )} +
+ + + {loginErrors?.errors?.username && ( +

+ {loginErrors.errors.username} +

+ )} +
+
+ + + {loginErrors?.errors?.password && ( +

+ {loginErrors.errors.password} +

+ )} +
+ +
+ ログイン +
+
+
+
); } -- cgit v1.2.3-70-g09d2