-
Notifications
You must be signed in to change notification settings - Fork 0
feat: migrate to remix-run/auth-middleware #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
80d7b0f
9db0e8f
97af936
d351b0e
22a9abc
f06e5b3
9aaa7b4
fcc78b7
1d0ccb0
dbe5879
5c9bcde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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( | ||||||
| <Document url={url} head={<title>Login - Remix Wordle</title>}> | ||||||
| <main class="h-dvh"> | ||||||
| {error && typeof error === "string" ? ( | ||||||
| <div class="text-red-500">{error}</div> | ||||||
| ) : null} | ||||||
| <form | ||||||
| method="post" | ||||||
| class="mx-auto flex h-full w-full max-w-md flex-col items-center justify-center space-y-6 px-8" | ||||||
| action={formAction} | ||||||
| > | ||||||
| > | ||||||
| <div class="w-full"> | ||||||
| {error && typeof error === "string" ? ( | ||||||
| <div class="text-red-500 mb-1">{error}</div> | ||||||
| ) : null} | ||||||
| <label htmlFor="email" class="block text-sm font-medium text-gray-700"> | ||||||
| Email address | ||||||
| </label> | ||||||
|
|
@@ -90,36 +87,47 @@ 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( | ||||||
| <Document url={url} head={<title>Login - Remix Wordle</title>}> | ||||||
| <main class="h-dvh"> | ||||||
| <form | ||||||
| method="POST" | ||||||
| action={routes.auth.register.action.href()} | ||||||
|
||||||
| action={routes.auth.register.action.href()} | |
| action={routes.auth.register.action.href(undefined, getReturnToQuery(url))} |
Copilot
AI
Apr 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After successful registration, the code sets session.set("auth", ...) but does not call completeAuth(context) (used in the login flow) to finalize auth/session state (e.g., session id regeneration to prevent session fixation). Consider using the same completeAuth + getPostAuthRedirect pattern here for consistency and security.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The register page title is still set to "Login - Remix Wordle". Since this hunk was updated, it’s a good opportunity to correct the title to match the register page to avoid confusing users and improve accessibility (page title is announced by screen readers).