Skip to content

Commit d9cc910

Browse files
committed
Consolidate destructive buttons in <Button> and tidy auth/settings
- AuthController.reset_password no longer manually deletes reset tokens; Accounts.reset_user_password/2 already wipes them. Drops the now-unused Repo and UserToken aliases too. - <Button> grows danger and dangerOutline variants, and Settings.tsx replaces its two hand-rolled red buttons with them. - SettingsController.update_password fallback now attaches the "current and new password are required" error to both fields so it surfaces next to the right input(s).
1 parent 2c949e1 commit d9cc910

4 files changed

Lines changed: 11 additions & 20 deletions

File tree

assets/js/components/Button.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ButtonHTMLAttributes } from 'react';
22

33
interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
4-
variant?: 'primary' | 'secondary';
4+
variant?: 'primary' | 'secondary' | 'danger' | 'dangerOutline';
55
}
66

77
const base =
@@ -11,6 +11,9 @@ const variants: Record<NonNullable<ButtonProps['variant']>, string> = {
1111
primary: 'bg-primary text-white hover:opacity-90 focus-visible:outline-primary',
1212
secondary:
1313
'border border-gray-300 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-800 focus-visible:outline-gray-500',
14+
danger: 'bg-red-600 text-white hover:bg-red-700 focus-visible:outline-red-500',
15+
dangerOutline:
16+
'border border-red-300 text-red-700 dark:border-red-700 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950/40 focus-visible:outline-red-500',
1417
};
1518

1619
export default function Button({ variant = 'primary', className = '', type = 'button', ...rest }: ButtonProps) {

assets/js/pages/Settings.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,9 @@ function DeleteAccountSection() {
119119
}}
120120
>
121121
<AlertDialogTrigger asChild>
122-
<button
123-
type="button"
124-
className="inline-flex items-center justify-center rounded px-4 py-2 text-sm font-medium border border-red-300 text-red-700 dark:border-red-700 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950/40 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-red-500"
125-
>
122+
<Button type="button" variant="dangerOutline">
126123
{t('settings.deleteAccount.button')}
127-
</button>
124+
</Button>
128125
</AlertDialogTrigger>
129126

130127
<AlertDialogContent>
@@ -157,13 +154,9 @@ function DeleteAccountSection() {
157154
</Button>
158155
</AlertDialogCancel>
159156
<AlertDialogAction asChild>
160-
<button
161-
type="submit"
162-
disabled={processing}
163-
className="inline-flex items-center justify-center rounded px-4 py-2 text-sm font-medium bg-red-600 text-white hover:bg-red-700 disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-red-500"
164-
>
157+
<Button type="submit" variant="danger" disabled={processing}>
165158
{t('settings.deleteAccount.confirm')}
166-
</button>
159+
</Button>
167160
</AlertDialogAction>
168161
</AlertDialogFooter>
169162
</form>

lib/elixir_react_starter_web/controllers/auth_controller.ex

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ defmodule ElixirReactStarterWeb.AuthController do
2323

2424
alias ElixirReactStarter.Accounts
2525
alias ElixirReactStarter.Accounts.User
26-
alias ElixirReactStarter.Accounts.UserToken
2726
alias ElixirReactStarter.Log
28-
alias ElixirReactStarter.Repo
2927
alias ElixirReactStarterWeb.Email
3028
alias ElixirReactStarterWeb.UserAuth
3129

@@ -236,11 +234,6 @@ defmodule ElixirReactStarterWeb.AuthController do
236234
user ->
237235
case Accounts.reset_user_password(user, %{password: password}) do
238236
{:ok, _user} ->
239-
# Consume the reset token so the same link can't be reused.
240-
Repo.delete_all(
241-
UserToken.delete_user_tokens_by_context_query(user.id, "password_reset")
242-
)
243-
244237
conn
245238
|> put_flash(:info, dgettext("app", "Password reset successfully. Please log in."))
246239
|> redirect(to: ~p"/login")

lib/elixir_react_starter_web/controllers/settings_controller.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ defmodule ElixirReactStarterWeb.SettingsController do
4747
end
4848

4949
def update_password(conn, _params) do
50+
message = dgettext("app", "Current and new password are required")
51+
5052
conn
51-
|> assign_errors(%{password: dgettext("app", "Current and new password are required")})
53+
|> assign_errors(%{current_password: message, password: message})
5254
|> redirect(to: ~p"/settings")
5355
end
5456

0 commit comments

Comments
 (0)