fix(it-hilfe): land anonymous claimers on their request after password set

Adjacent-broken fix on the just-shipped anonymous-post flow. The claim
email pointed users at /auth/reset-password?token=...; after they set
a password they were redirected to /auth/login?reset=success and then,
on successful sign-in, the default post-login destination — not the
request they just submitted. That meant the user had to manually
navigate to /it-hilfe/my (or remember the request title and search)
to find their own work. Real UX seam in a brand-new flow.

Fix:

1. /auth/reset-password page reads ?callbackUrl= from searchParams and
   propagates it into the post-success login redirect:
     /auth/login?reset=success&callbackUrl=<original>
   Both the auto-redirect after 3s AND the manual "Login now" button
   honour it. When callbackUrl is absent, behavior unchanged (existing
   forgot-password users see no diff).

2. POST /api/it-hilfe/requests now builds the claim URL with
   &callbackUrl=/it-hilfe/<requestId> appended, so NextAuth picks up
   the callback after sign-in and lands the user directly on their
   request.

End-to-end the anonymous flow is now:
  submit → email → click → set password → log in → see your request.
No detour through default-page-then-figure-out-where-to-go.

Tests: extended the existing claim-email test in the requests route
suite to assert the URL contains both ?token= AND
&callbackUrl=<encoded /it-hilfe/req-new>. Catches the regression
where the route accidentally drops the callbackUrl wiring. Existing
reset-password tests still pass since the new behavior is purely
additive (callbackUrl is read-only on the page). 468 IT-Hilfe +
reset-password tests across 34 suites all pass; tsc clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-25 18:05:06 +02:00
1 parent 236eaf4b45
commit aedf208b5a
3 files changed
+39 -5

No files matched your search

@@ -361,6 +361,17 @@ describe('POST /api/it-hilfe/requests — anonymous submissions', () => {
'newuser@example.com',
expect.any(Object)
)
// The claim URL must include callbackUrl pointing to the just-created
// request, so the user lands on it after setting a password + signing in.
// (Without this, the post-claim UX dumps users on the default page and
// they have to navigate to /it-hilfe/my themselves.)
const templates = require('@/lib/email/templates/it-hilfe')
const claimArgs = templates.itHilfeAnonymousRequestClaim.mock.calls[0]
const claimUrl = claimArgs[1] as string
expect(claimUrl).toContain('callbackUrl=')
expect(claimUrl).toContain(encodeURIComponent('/it-hilfe/req-new'))
expect(claimUrl).toContain('token=')
})
it('does NOT send the claim email when wasCreated is false (existing account)', async () => {
+10 -2
View File
@@ -280,11 +280,19 @@ export async function POST(request: NextRequest) {
// For newly-provisioned anonymous accounts: send a claim email with a
// password-reset token so the user can set a password and access their
// request. Fire-and-forget; failure shouldn't fail the request creation.
// request. callbackUrl propagates through reset-password → login so
// the user lands directly on their just-submitted request after the
// sign-in completes — closing the seam between "set password" and
// "see what I posted." Fire-and-forget; failure shouldn't fail the
// request creation.
if (isNewAnonymousUser) {
createPasswordResetToken(requesterEmail)
.then(token => {
const claimUrl = `${APP_URL}/auth/reset-password?token=${encodeURIComponent(token)}`
const callbackUrl = `/it-hilfe/${requestId}`
const claimUrl =
`${APP_URL}/auth/reset-password` +
`?token=${encodeURIComponent(token)}` +
`&callbackUrl=${encodeURIComponent(callbackUrl)}`
return sendCustomEmail(
requesterEmail,
itHilfeAnonymousRequestClaim(sanitizedTitle, claimUrl)
+18 -3
View File
@@ -15,6 +15,10 @@ function ResetPasswordContent() {
const router = useRouter()
const searchParams = useSearchParams()
const token = searchParams.get('token')
// Propagated through from the email link so that callers (e.g. the
// IT-Hilfe anonymous-post claim flow) can land users on a specific
// page after they sign in — not just the default post-login destination.
const callbackUrl = searchParams.get('callbackUrl')
const [password, setPassword] = useState('')
const [confirmPassword, setConfirmPassword] = useState('')
@@ -67,9 +71,12 @@ function ResetPasswordContent() {
setSuccess(true)
// Redirect to login after 3 seconds
// Redirect to login after 3 seconds, preserving callbackUrl so the
// user lands on their original destination after signing in.
setTimeout(() => {
router.push('/auth/login?reset=success')
const params = new URLSearchParams({ reset: 'success' })
if (callbackUrl) params.set('callbackUrl', callbackUrl)
router.push(`/auth/login?${params.toString()}`)
}, 3000)
} catch (error) {
setError(error instanceof Error ? error.message : t('genericError'))
@@ -135,7 +142,15 @@ function ResetPasswordContent() {
<p className="text-sm text-neutral-600 mb-6">
{t('successRedirect')}
</p>
<Button as={Link} href={ROUTES.public.login} variant="primary">
<Button
as={Link}
href={
callbackUrl
? `${ROUTES.public.login}?callbackUrl=${encodeURIComponent(callbackUrl)}`
: ROUTES.public.login
}
variant="primary"
>
{t('loginNow')}
</Button>
</div>