diff --git a/app/auth/controller.tsx b/app/auth/controller.tsx index 6c316e9..e2a29f5 100644 --- a/app/auth/controller.tsx +++ b/app/auth/controller.tsx @@ -1,46 +1,43 @@ +import { completeAuth, verifyCredentials } from "remix/auth" import { parse } from "remix/data-schema" import type { Controller } from "remix/fetch-router" -import { createRedirectResponse as redirect } from "remix/response/redirect" +import { redirect } from "remix/response/redirect" import { Session } from "remix/session" import { Document } from "../components/document.tsx" -import { loadAuth } from "../middleware/auth.ts" +import { getPostAuthRedirect, getReturnToQuery, passwordProvider } from "../middleware/auth.ts" import { - authenticateUser, createPasswordResetToken, createUser, getUserByEmail, joinSchema, - loginSchema, resetPassword, } from "../models/user.ts" import { routes } from "../routes.ts" import { render } from "../utils/render.ts" export const auth = { - middleware: [loadAuth()], + middleware: [], actions: { login: { actions: { index({ get, url }) { let session = get(Session) let error = session.get("error") - let formAction = routes.auth.login.action.href(undefined, { - returnTo: url.searchParams.get("returnTo"), - }) + let formAction = routes.auth.login.action.href(undefined, getReturnToQuery(url)) return render( Login - Remix Wordle}>
- {error && typeof error === "string" ? ( -
{error}
- ) : null}
+ >
+ {error && typeof error === "string" ? ( +
{error}
+ ) : null} @@ -90,28 +87,36 @@ export const auth = { ) }, - async action({ get, url }) { - let session = get(Session) - let formData = get(FormData) - let result = parse(loginSchema, formData) - let returnTo = url.searchParams.get("returnTo") - - let user = await authenticateUser(result.email, result.password) - if (!user) { - session.flash("error", "Invalid email or password. Please try again.") - return redirect(routes.auth.login.index.href(undefined, { returnTo })) + async action(context) { + try { + let user = await verifyCredentials(passwordProvider, context) + + if (user == null) { + let session = context.get(Session) + session.flash("error", "Invalid email or password. Please try again.") + return redirect( + routes.auth.login.index.href(undefined, getReturnToQuery(context.url)), + ) + } + + let session = completeAuth(context) + session.set("auth", { userId: user.id }) + return redirect(getPostAuthRedirect(context.url)) + } catch (error) { + let session = context.get(Session) + session.flash("error", "We could not complete that sign-in request.") + return redirect(routes.auth.login.index.href(undefined, getReturnToQuery(context.url))) } - - session.set("auth", {userId: user.id}) - - return redirect(returnTo ?? routes.home.index.href()) }, }, }, register: { actions: { - index({ url }) { + index({ get, url }) { + let session = get(Session) + let error = session.get("error") + return render( Login - Remix Wordle}>
@@ -119,7 +124,10 @@ export const auth = { method="POST" action={routes.auth.register.action.href()} class="mx-auto flex h-full w-full max-w-md flex-col items-center justify-center space-y-6 px-8" - > + > + {error && typeof error === "string" ? ( +
{error}
+ ) : null}