feat: change the password in settings
Accounts that only sign in with SSO have no password and do not see it.
This commit is contained in:
@@ -3,7 +3,14 @@ import { getRequestHeaders } from "@tanstack/react-start/server"
|
||||
import { and, asc, eq, inArray, sql } from "drizzle-orm"
|
||||
import { z } from "zod"
|
||||
import { db } from "@/db"
|
||||
import { card, dashboard, link, settings, wallpaper } from "@/db/schema"
|
||||
import {
|
||||
account,
|
||||
card,
|
||||
dashboard,
|
||||
link,
|
||||
settings,
|
||||
wallpaper,
|
||||
} from "@/db/schema"
|
||||
import { auth, requireUser, signUpEnabled, ssoName } from "@/lib/auth"
|
||||
import { CLOCK_FONTS, CLOCK_STYLES } from "@/lib/clock"
|
||||
import { ENGINES, parseSuggestions } from "@/lib/search"
|
||||
@@ -57,13 +64,18 @@ export const getDashboard = createServerFn({ method: "GET" })
|
||||
const session = await auth.api.getSession({ headers: getRequestHeaders() })
|
||||
if (!session) return null
|
||||
const userId = session.user.id
|
||||
const [dashboards, rows] = await Promise.all([
|
||||
const [dashboards, rows, passwords] = await Promise.all([
|
||||
db
|
||||
.select(shown)
|
||||
.from(dashboard)
|
||||
.where(eq(dashboard.userId, userId))
|
||||
.orderBy(asc(dashboard.createdAt)),
|
||||
db.select().from(settings).where(eq(settings.userId, userId)),
|
||||
// someone who only ever signed in with SSO has no password to change
|
||||
db.$count(
|
||||
account,
|
||||
and(eq(account.userId, userId), eq(account.providerId, "credential"))
|
||||
),
|
||||
])
|
||||
// Cards need a dashboard and links need a card, so nobody is left without one.
|
||||
// shortcut: two first loads at the same moment make two of each, add a unique
|
||||
@@ -108,7 +120,11 @@ export const getDashboard = createServerFn({ method: "GET" })
|
||||
.returning({ id: card.id, name: card.name }))
|
||||
)
|
||||
return {
|
||||
user: { name: session.user.name, email: session.user.email },
|
||||
user: {
|
||||
name: session.user.name,
|
||||
email: session.user.email,
|
||||
hasPassword: passwords > 0,
|
||||
},
|
||||
dashboards,
|
||||
dashboardId,
|
||||
defaultDashboardId: byDefault.id,
|
||||
|
||||
@@ -640,6 +640,65 @@ function Settings() {
|
||||
</Button>
|
||||
</SettingsRowControl>
|
||||
</SettingsRow>
|
||||
{data.user.hasPassword && (
|
||||
<SettingsRow stacked>
|
||||
<SettingsRowText>
|
||||
<div className="leading-tight font-bold">Password</div>
|
||||
<SettingsRowDescription>
|
||||
A new password signs you out on your other devices.
|
||||
</SettingsRowDescription>
|
||||
</SettingsRowText>
|
||||
<SettingsRowControl>
|
||||
<form
|
||||
className="flex flex-wrap items-end gap-3"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault()
|
||||
const form = e.currentTarget
|
||||
const fields = new FormData(form)
|
||||
void run(async () => {
|
||||
// Better Auth returns { error } instead of throwing
|
||||
const { error: failure } =
|
||||
await authClient.changePassword({
|
||||
currentPassword: String(fields.get("current")),
|
||||
newPassword: String(fields.get("new")),
|
||||
revokeOtherSessions: true,
|
||||
})
|
||||
if (failure)
|
||||
return (
|
||||
failure.message ||
|
||||
"Could not change the password."
|
||||
)
|
||||
form.reset()
|
||||
})
|
||||
}}
|
||||
>
|
||||
<Field className="w-56">
|
||||
<FieldLabel>Current password</FieldLabel>
|
||||
<Input
|
||||
name="current"
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
required
|
||||
/>
|
||||
</Field>
|
||||
<Field className="w-56">
|
||||
<FieldLabel>New password</FieldLabel>
|
||||
<Input
|
||||
name="new"
|
||||
type="password"
|
||||
autoComplete="new-password"
|
||||
minLength={8}
|
||||
maxLength={128}
|
||||
required
|
||||
/>
|
||||
</Field>
|
||||
<Button type="submit" disabled={status === "saving"}>
|
||||
Change password
|
||||
</Button>
|
||||
</form>
|
||||
</SettingsRowControl>
|
||||
</SettingsRow>
|
||||
)}
|
||||
</SettingsSection>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user