Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key

### Set to 1 to enable verbose logging
# NEXT_PUBLIC_VERBOSE=0

### Set to 1 to pause sign-ups and sign-ins during auth migration.
# NEXT_PUBLIC_AUTH_MIGRATION_IN_PROGRESS=0
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spec/openapi.dashboard-api.yaml linguist-generated=true
src/core/shared/contracts/dashboard-api.types.ts linguist-generated=true
113 changes: 113 additions & 0 deletions src/app/(auth)/forgot-password/forgot-password-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use client'

import { zodResolver } from '@hookform/resolvers/zod'
import { useHookFormAction } from '@next-safe-action/adapter-react-hook-form/hooks'
import { useSearchParams } from 'next/navigation'
import { useEffect, useState } from 'react'
import { AUTH_URLS } from '@/configs/urls'
import {
getTimeoutMsFromUserMessage,
USER_MESSAGES,
} from '@/configs/user-messages'
import { forgotPasswordAction } from '@/core/server/actions/auth-actions'
import { forgotPasswordSchema } from '@/core/server/functions/auth/auth.types'
import { AuthFormMessage, type AuthMessage } from '@/features/auth/form-message'
import { Button } from '@/ui/primitives/button'
import { Input } from '@/ui/primitives/input'
import { Label } from '@/ui/primitives/label'

export default function ForgotPassword() {
const searchParams = useSearchParams()
const [message, setMessage] = useState<AuthMessage | undefined>()

const {
form,
handleSubmitWithAction,
action: { isExecuting },
} = useHookFormAction(
forgotPasswordAction,
zodResolver(forgotPasswordSchema),
{
actionProps: {
onSuccess: () => {
form.reset()
setMessage({ success: USER_MESSAGES.passwordReset.message })
},
onError: ({ error }) => {
if (error.serverError) {
setMessage({ error: error.serverError })
}
},
},
}
)

useEffect(() => {
const email = searchParams.get('email')
if (email) {
form.setValue('email', email)
}
}, [searchParams, form])

useEffect(() => {
if (!message) return

const messageText =
'success' in message
? message.success
: 'error' in message
? message.error
: undefined

if (!messageText) return

const timer = setTimeout(
() => setMessage(undefined),
getTimeoutMsFromUserMessage(messageText) || 5000
)
return () => clearTimeout(timer)
}, [message])

const handleBackToSignIn = () => {
const email = form.getValues('email')
const searchParams = email ? `?email=${encodeURIComponent(email)}` : ''
window.location.href = `${AUTH_URLS.SIGN_IN}${searchParams}`
}

return (
<div className="flex w-full flex-col">
<h1>Reset Password</h1>
<p className="text-fg-secondary leading-6">
Remember your password?{' '}
<button
type="button"
onClick={handleBackToSignIn}
className="text-fg underline"
>
Sign in
</button>
.
</p>

<form
onSubmit={handleSubmitWithAction}
className="mt-5 flex flex-col gap-2"
>
<Label htmlFor="email">E-Mail</Label>
<Input
{...form.register('email')}
id="email"
name="email"
type="email"
placeholder="you@example.com"
required
/>
<Button type="submit" loading={isExecuting ? 'Sending...' : undefined}>
Reset Password
</Button>
</form>

{message && <AuthFormMessage className="mt-4" message={message} />}
</div>
)
}
115 changes: 3 additions & 112 deletions src/app/(auth)/forgot-password/page.tsx
Original file line number Diff line number Diff line change
@@ -1,114 +1,5 @@
'use client'
import ForgotPassword from './forgot-password-form'

import { zodResolver } from '@hookform/resolvers/zod'
import { useHookFormAction } from '@next-safe-action/adapter-react-hook-form/hooks'
import { useSearchParams } from 'next/navigation'
import { useEffect, useState } from 'react'
import { AUTH_URLS } from '@/configs/urls'
import {
getTimeoutMsFromUserMessage,
USER_MESSAGES,
} from '@/configs/user-messages'
import { forgotPasswordAction } from '@/core/server/actions/auth-actions'
import { forgotPasswordSchema } from '@/core/server/functions/auth/auth.types'
import { AuthFormMessage, type AuthMessage } from '@/features/auth/form-message'
import { Button } from '@/ui/primitives/button'
import { Input } from '@/ui/primitives/input'
import { Label } from '@/ui/primitives/label'

export default function ForgotPassword() {
const searchParams = useSearchParams()
const [message, setMessage] = useState<AuthMessage | undefined>()

const {
form,
handleSubmitWithAction,
action: { isExecuting },
} = useHookFormAction(
forgotPasswordAction,
zodResolver(forgotPasswordSchema),
{
actionProps: {
onSuccess: () => {
form.reset()
setMessage({ success: USER_MESSAGES.passwordReset.message })
},
onError: ({ error }) => {
if (error.serverError) {
setMessage({ error: error.serverError })
}
},
},
}
)

useEffect(() => {
const email = searchParams.get('email')
if (email) {
form.setValue('email', email)
}
}, [searchParams, form])

useEffect(() => {
if (
message &&
(('success' in message && message.success) ||
('error' in message && message.error))
) {
const timer = setTimeout(
() => setMessage(undefined),
getTimeoutMsFromUserMessage(
'success' in message
? message.success!
: 'error' in message
? message.error!
: ''
) || 5000
)
return () => clearTimeout(timer)
}
}, [message])

const handleBackToSignIn = () => {
const email = form.getValues('email')
const searchParams = email ? `?email=${encodeURIComponent(email)}` : ''
window.location.href = `${AUTH_URLS.SIGN_IN}${searchParams}`
}

return (
<div className="flex w-full flex-col">
<h1>Reset Password</h1>
<p className="text-fg-secondary leading-6">
Remember your password?{' '}
<button
type="button"
onClick={handleBackToSignIn}
className="text-fg underline"
>
Sign in
</button>
.
</p>

<form
onSubmit={handleSubmitWithAction}
className="mt-5 flex flex-col gap-2"
>
<Label htmlFor="email">E-Mail</Label>
<Input
{...form.register('email')}
id="email"
name="email"
type="email"
placeholder="you@example.com"
required
/>
<Button type="submit" loading={isExecuting ? 'Sending...' : undefined}>
Reset Password
</Button>
</form>

{message && <AuthFormMessage className="mt-4" message={message} />}
</div>
)
export default function Page() {
return <ForgotPassword />
}
Loading
Loading