Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

feat(settings): set or change the password from Settings

spacesdrivecb6b73607d0f
diff --git a/frontend/src/features/settings/PasswordSection.tsx b/frontend/src/features/settings/PasswordSection.tsx
new file mode 100644
index 0000000..296c29a
--- /dev/null
+++ b/frontend/src/features/settings/PasswordSection.tsx
@@ -0,0 +1,85 @@
+import React, { useState } from 'react';
+import { KeyRound } from 'lucide-react';
+import { Button } from '@/components/ui/button';
+import { Input } from '@/components/ui/input';
+import { Spinner } from '@/components/ui/spinner';
+import SurfaceBox from '@/components/SurfaceBox';
+import { FieldRow } from '@/components/DetailPanel';
+import { PASSWORD_MIN_LENGTH, newPasswordProblem } from '@/lib/passwords';
+import { passwordErrorMessage } from '@/lib/authErrors';
+import type { AuthErrorLike } from '@/lib/authErrors';
+import { useAuth } from '../../context/AuthContext';
+import { useAppContext } from '../../context/AppContext';
+import SettingsSection from './SettingsSection';
+
+export default function PasswordSection() {
+ const { user, updatePassword } = useAuth();
+ const { showToast } = useAppContext();
+ const [password, setPassword] = useState('');
+ const [confirmation, setConfirmation] = useState('');
+ const [saving, setSaving] = useState(false);
+
+ async function handleSave(e: React.FormEvent<HTMLFormElement>) {
+ e.preventDefault();
+ const problem = newPasswordProblem(password, confirmation);
+ if (problem) {
+ showToast(problem, 'error');
+ return;
+ }
+ setSaving(true);
+ try {
+ const { error } = await updatePassword(password);
+ if (error) throw error;
+ setPassword('');
+ setConfirmation('');
+ showToast('Password saved. You can now sign in with your email and this password.');
+ } catch (err) {
+ showToast(`Couldn't save the password. ${passwordErrorMessage(err as AuthErrorLike)}`, 'error');
+ } finally {
+ setSaving(false);
+ }
+ }
+
+ return (
+ <SettingsSection
+ id="password"
+ title="Password"
+ description="Sign in with your email and a password. If you joined with Google, this adds a password to the same account."
+ >
+ <SurfaceBox>
+ <form onSubmit={handleSave} className="flex flex-col gap-4">
+ {/* lets password managers save the new password against the right account */}
+ <input type="email" name="username" autoComplete="username" value={user?.email ?? ''} readOnly hidden />
+ <FieldRow label="New password" htmlFor="new-password" hint={`At least ${PASSWORD_MIN_LENGTH} characters.`}>
+ <Input
+ id="new-password"
+ type="password"
+ value={password}
+ onChange={e => setPassword(e.target.value)}
+ autoComplete="new-password"
+ minLength={PASSWORD_MIN_LENGTH}
+ required
+ className="max-w-sm"
+ />
+ </FieldRow>
+ <FieldRow label="Confirm the new password" htmlFor="confirm-password">
+ <Input
+ id="confirm-password"
+ type="password"
+ value={confirmation}
+ onChange={e => setConfirmation(e.target.value)}
+ autoComplete="new-password"
+ minLength={PASSWORD_MIN_LENGTH}
+ required
+ className="max-w-sm"
+ />
+ </FieldRow>
+ <Button type="submit" variant="outline" className="self-start" disabled={saving || !password || !confirmation}>
+ {saving ? <Spinner className="size-4" /> : <KeyRound data-icon="inline-start" />}
+ Save password
+ </Button>
+ </form>
+ </SurfaceBox>
+ </SettingsSection>
+ );
+}
diff --git a/frontend/src/features/settings/Settings.tsx b/frontend/src/features/settings/Settings.tsx
index f5ff619..4890386 100644
--- a/frontend/src/features/settings/Settings.tsx
+++ b/frontend/src/features/settings/Settings.tsx
@@ -6,6 +6,7 @@ import { useAppContext } from '../../context/AppContext';
import { useServerSettings } from './useServerSettings';
import SettingsSection from './SettingsSection';
import AppearanceSection from './AppearanceSection';
+import PasswordSection from './PasswordSection';
import ServerKeysSection from './ServerKeysSection';
import SchedulingSection from './SchedulingSection';
import PlatformRequirementsSection from './PlatformRequirementsSection';
@@ -26,7 +27,7 @@ export default function Settings() {
if (loading) {
return (
<div className="flex flex-col gap-8">
- <PageHeader title="Settings" description="Appearance, connections and setup guides" />
+ <PageHeader title="Settings" description="Appearance, password, connections and setup guides" />
{[0, 1, 2].map(i => <Skeleton key={i} className="h-40 rounded-card" />)}
</div>
);
@@ -34,10 +35,11 @@ export default function Settings() {
return (
<div className="flex flex-col gap-8">
- <PageHeader title="Settings" description="Appearance, connections and setup guides" />
+ <PageHeader title="Settings" description="Appearance, password, connections and setup guides" />
<div className="flex flex-col gap-8">
<AppearanceSection />
+ <PasswordSection />
<ServerKeysSection serverStatus={serverStatus} refreshAll={refreshAll} refreshingAll={refreshingAll} />
<SchedulingSection github={github} />
<PlatformRequirementsSection />
diff --git a/frontend/src/lib/passwords.ts b/frontend/src/lib/passwords.ts
new file mode 100644
index 0000000..639f3dd
--- /dev/null
+++ b/frontend/src/lib/passwords.ts
@@ -0,0 +1,9 @@
+// Supabase's default minimum; a project set higher still answers with its own message
+export const PASSWORD_MIN_LENGTH = 6;
+
+// what is wrong with a new password before it is sent, or null when it can be saved
+export function newPasswordProblem(password: string, confirmation: string): string | null {
+ if (password.length < PASSWORD_MIN_LENGTH) return `Use at least ${PASSWORD_MIN_LENGTH} characters.`;
+ if (password !== confirmation) return 'The two passwords do not match.';
+ return null;
+}
diff --git a/frontend/tests/lib/passwords.test.ts b/frontend/tests/lib/passwords.test.ts
new file mode 100644
index 0000000..3efdaa3
--- /dev/null
+++ b/frontend/tests/lib/passwords.test.ts
@@ -0,0 +1,14 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { PASSWORD_MIN_LENGTH, newPasswordProblem } from '../../src/lib/passwords.ts';
+
+test('a new password needs the minimum length and a matching confirmation', () => {
+ assert.equal(PASSWORD_MIN_LENGTH, 6);
+ assert.equal(newPasswordProblem('12345', '12345'), 'Use at least 6 characters.');
+ assert.equal(newPasswordProblem('123456', '123457'), 'The two passwords do not match.');
+ assert.equal(newPasswordProblem('123456', '123456'), null);
+});
+
+test('length is checked before the confirmation, so a short password says what to fix first', () => {
+ assert.equal(newPasswordProblem('abc', 'xyz'), 'Use at least 6 characters.');
+});