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
4 changes: 4 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ProtectedRoute } from './components/ProtectedRoute'
import { DeepLinkListener } from './components/DeepLinkListener'
import { AppLayout } from './components/layout/AppLayout'
import { LoginPage } from './pages/LoginPage'
import { ForgotPasswordPage } from './pages/ForgotPasswordPage'
import { ResetPasswordPage } from './pages/ResetPasswordPage'
import { SignupPage } from './pages/SignupPage'
import { ConfirmEmailPage } from './pages/ConfirmEmailPage'
import { AcceptInvitationPage } from './pages/AcceptInvitationPage'
Expand All @@ -30,6 +32,8 @@ function App() {
<DeepLinkListener />
<Routes>
<Route path="/login" element={<LoginPage />} />
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
<Route path="/reset-password/:token" element={<ResetPasswordPage />} />
<Route path="/signup" element={<SignupPage />} />
<Route path="/confirm-email/:token" element={<ConfirmEmailPage />} />
<Route path="/invitation/:token" element={<AcceptInvitationPage />} />
Expand Down
18 changes: 10 additions & 8 deletions src/components/DeepLinkListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { listen } from '@tauri-apps/api/event'
import { isTauriApp } from '@/lib/platform'

/**
* Bridges nodea:// deep links (opened from invitation/confirm-email
* emails) into the React Router. The Rust side (src-tauri/src/main.rs)
* registers the OS-level scheme handler and forwards incoming links here
* via the "scheme-request-received" event.
* Bridges nodea:// deep links (opened from emails) into the React Router.
* The Rust side (src-tauri/src/main.rs) registers the OS-level scheme
* handler and forwards incoming links here via the
* "scheme-request-received" event.
*
* FRONTEND_BASE_URL is "nodea://" in production, so a link built as
* `${FRONTEND_BASE_URL}/confirm-email/${token}` becomes
* "nodea:///confirm-email/<token>" - an empty host with the app route as
* the URL's pathname, which is what we navigate to below.
* The backend builds the password reset links this app asks for from
* DESKTOP_FRONTEND_BASE_URL, "nodea://" in production, so
* `${DESKTOP_FRONTEND_BASE_URL}/reset-password/${token}` becomes
* "nodea:///reset-password/<token>" - an empty host with the app route as
* the URL's pathname, which is what we navigate to below. Its other links
* (FRONTEND_BASE_URL) open the web console.
*/
export function DeepLinkListener() {
const navigate = useNavigate()
Expand Down
21 changes: 21 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,27 @@ class ApiClient {
body: JSON.stringify({ email }),
})
}

// Forgot password - public, the user cannot sign in. The emailed link is a
// nodea:// deep link back into this app (client: 'desktop').
async requestPasswordReset(email: string): Promise<{ message: string }> {
return this.fetch<{ message: string }>('/v1/password-reset', {
method: 'POST',
body: JSON.stringify({ email, client: 'desktop' }),
})
}

// Answers 410 for a link that is unknown, used, replaced or expired.
async checkPasswordReset(token: string): Promise<{ email: string }> {
return this.fetch<{ email: string }>(`/v1/password-reset/${encodeURIComponent(token)}`)
}

async resetPassword(token: string, password: string): Promise<{ message: string; email: string }> {
return this.fetch<{ message: string; email: string }>(`/v1/password-reset/${encodeURIComponent(token)}`, {
method: 'POST',
body: JSON.stringify({ password }),
})
}
}


Expand Down
18 changes: 18 additions & 0 deletions src/lib/mockApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,24 @@ export class MockApiClient {
return { message: 'Confirmation email resent' }
}

// Forgot password
async requestPasswordReset(_email: string): Promise<{ message: string }> {
await delay(300)
console.log('Mock: Password reset email sent')
return { message: 'If an account exists for this email, a reset link is on its way' }
}

async checkPasswordReset(_token: string): Promise<{ email: string }> {
await delay(200)
return { email: 'demo@getnodea.com' }
}

async resetPassword(_token: string, _password: string): Promise<{ message: string; email: string }> {
await delay(300)
console.log('Mock: Password reset')
return { message: 'Password updated', email: 'demo@getnodea.com' }
}

// Onboarding
async createOrganizationOnboarding(): Promise<{ organization: Organization }> {
await delay(500)
Expand Down
14 changes: 14 additions & 0 deletions src/lib/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Mirrors the backend's validation.validate_password, so the form can say what
// is wrong before submitting.
export function validatePassword(password: string): string | null {
if (password.length < 8) {
return 'Password must be at least 8 characters long'
}
if (!/[A-Za-z]/.test(password)) {
return 'Password must contain at least 1 letter'
}
if (!/\d/.test(password)) {
return 'Password must contain at least 1 digit'
}
return null
}
81 changes: 81 additions & 0 deletions src/pages/ForgotPasswordPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useState } from 'react'
import { Link, useLocation } from 'react-router-dom'
import { apiClient } from '@/lib/api'

/* Same layout as LoginPage, which links here with the email typed there. */
export function ForgotPasswordPage() {
const location = useLocation()
const [email, setEmail] = useState<string>((location.state as { email?: string } | null)?.email ?? '')
const [sending, setSending] = useState(false)
const [sent, setSent] = useState(false)
const [error, setError] = useState('')

const submit = async (e: React.FormEvent) => {
e.preventDefault()
setError('')
setSending(true)
try {
await apiClient.requestPasswordReset(email)
setSent(true)
} catch (err) {
setError(err instanceof Error ? err.message : 'Could not send a reset link')
} finally {
setSending(false)
}
}

return (
<div className="flex h-screen flex-col justify-center bg-[var(--n-bg)] px-[54px]">
<div className="n-drag absolute left-0 right-0 top-0 h-[46px]" data-tauri-drag-region />

<span className="flex h-[34px] w-[34px] items-center justify-center rounded-[10px] bg-[var(--n-accent)] text-[16px] font-semibold leading-none text-[var(--n-bg)]">
N
</span>

<h2 className="mb-1.5 mt-[22px] text-[25px] font-semibold leading-[1.25] tracking-[-0.02em] text-[var(--n-text)]">
Reset your password
</h2>

{sent ? (
<>
{/* The backend answers the same for any address, so neither can we. */}
<p role="status" className="mb-[22px] max-w-[360px] text-[14.5px] leading-[1.5] text-[var(--n-text-3)]">
If an account exists for <strong className="text-[var(--n-text-2)]">{email}</strong>, a link to
reset its password is on its way. Open it on this computer to come back to the app. It works for
one hour.
</p>
<Link to="/login" className="text-[13.5px] font-medium text-[var(--n-accent-400)] hover:text-[var(--n-accent-300)]">
Back to sign in
</Link>
</>
) : (
<>
<p className="mb-[22px] max-w-[360px] text-[14.5px] leading-[1.5] text-[var(--n-text-3)]">
Enter the email of your account and we'll send you a link to choose a new password.
</p>

<form onSubmit={submit} className="flex max-w-[360px] flex-col gap-2">
{error && <p role="alert" className="text-[13.5px] leading-[1.4] text-[var(--n-text-2)]">{error}</p>}

<input id="email" type="email" value={email}
onChange={(e) => setEmail(e.target.value)}
required autoFocus
placeholder="you@company.com"
className="rounded-[8px] border border-[var(--n-border)] bg-[var(--n-sidebar)] px-3 py-2.5 text-[15px] leading-[1.2] text-[var(--n-text)] outline-none placeholder:text-[var(--n-text-4)] focus:border-[var(--n-accent-800)]" />

<button type="submit" disabled={sending || !email}
className="mt-1.5 rounded-[8px] border border-[var(--n-accent)] bg-transparent px-3 py-2.5 text-[15px] font-semibold leading-[1.2] text-[var(--n-accent-300)] transition-colors hover:bg-[rgba(145,132,217,.14)] disabled:opacity-45">
{sending ? 'Sending…' : 'Send reset link'}
</button>

<p className="mt-1 text-[13.5px] leading-none text-[var(--n-text-3)]">
<Link to="/login" className="font-medium text-[var(--n-accent-400)] hover:text-[var(--n-accent-300)]">
Back to sign in
</Link>
</p>
</form>
</>
)}
</div>
)
}
17 changes: 15 additions & 2 deletions src/pages/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState, useEffect } from 'react'
import { Link, useNavigate, useSearchParams } from 'react-router-dom'
import { Link, useLocation, useNavigate, useSearchParams } from 'react-router-dom'
import { useAuth } from '@/contexts/AuthContext'
import { apiClient } from '@/lib/api'
import type { PasswordResetLoginState } from './ResetPasswordPage'

// Only accounts created before signups could log in unconfirmed still get
// this from Supabase; confirming (or resending) the link unblocks them.
Expand All @@ -12,7 +13,9 @@ function isEmailNotConfirmedError(message: string): boolean {
/* Keeps the existing auth calls (invitation sign-up, unconfirmed-email
* resend, post-login redirect) — only the presentation changes. */
export function LoginPage() {
const [email, setEmail] = useState('')
// Set when arriving from a finished password reset.
const resetState = useLocation().state as PasswordResetLoginState | null
const [email, setEmail] = useState(resetState?.email ?? '')
const [password, setPassword] = useState('')
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
Expand Down Expand Up @@ -129,6 +132,11 @@ export function LoginPage() {
This account was deleted. This device stopped sharing compute.
</p>
)}
{resetState?.passwordReset && (
<p role="status" className="mb-3 max-w-[360px] text-[14.5px] leading-[1.5] text-[var(--n-text-2)]">
Your password was changed. Sign in with your new password.
</p>
)}
<p className="mb-[22px] max-w-[360px] text-[14.5px] leading-[1.5] text-[var(--n-text-3)]">
{isInvitedSignup
? <>Joining <strong className="text-[var(--n-text-2)]">{invitationDetails?.organizationName}</strong> as {invitationDetails?.role}.</>
Expand Down Expand Up @@ -174,6 +182,11 @@ export function LoginPage() {
<Link to="/signup" className="font-medium text-[var(--n-accent-400)] hover:text-[var(--n-accent-300)]">
Sign up
</Link>
{' · '}
<Link to="/forgot-password" state={{ email }}
className="font-medium text-[var(--n-accent-400)] hover:text-[var(--n-accent-300)]">
Forgot password?
</Link>
</p>
)}
</form>
Expand Down
140 changes: 140 additions & 0 deletions src/pages/ResetPasswordPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { useEffect, useState } from 'react'
import { Link, useNavigate, useParams } from 'react-router-dom'
import { apiClient } from '@/lib/api'
import { useAuth } from '@/contexts/AuthContext'
import { validatePassword } from '@/lib/validation'

type LinkState = 'checking' | 'valid' | 'invalid'

// Read by LoginPage to say the password changed and prefill the email.
export interface PasswordResetLoginState {
passwordReset: true
email: string
}

const inputClass =
'rounded-[8px] border border-[var(--n-border)] bg-[var(--n-sidebar)] px-3 py-2.5 text-[15px] leading-[1.2] text-[var(--n-text)] outline-none placeholder:text-[var(--n-text-4)] focus:border-[var(--n-accent-800)]'

/* Opened from the emailed nodea:///reset-password/<token> link (see
* DeepLinkListener). Same layout as LoginPage. */
export function ResetPasswordPage() {
const { token } = useParams<{ token: string }>()
const navigate = useNavigate()
const { isAuthenticated, logout } = useAuth()

const [linkState, setLinkState] = useState<LinkState>('checking')
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [confirm, setConfirm] = useState('')
const [saving, setSaving] = useState(false)
const [error, setError] = useState('')

// Checking does not use the link up, so a second call (StrictMode) is harmless.
useEffect(() => {
if (!token) {
setLinkState('invalid')
return
}

let cancelled = false
apiClient
.checkPasswordReset(token)
.then(({ email }) => {
if (cancelled) return
setEmail(email)
setLinkState('valid')
})
.catch(() => {
if (!cancelled) setLinkState('invalid')
})
return () => {
cancelled = true
}
}, [token])

const submit = async (e: React.FormEvent) => {
e.preventDefault()
setError('')

const passwordError = validatePassword(password)
if (passwordError) {
setError(passwordError)
return
}
if (password !== confirm) {
setError('Passwords do not match')
return
}

setSaving(true)
try {
const result = await apiClient.resetPassword(token!, password)
// The reset signed the account out everywhere, this app included.
if (isAuthenticated) await logout().catch(() => {})
const state: PasswordResetLoginState = { passwordReset: true, email: result.email }
navigate('/login', { replace: true, state })
} catch (err) {
setError(err instanceof Error ? err.message : 'Could not reset your password')
setSaving(false)
}
}

return (
<div className="flex h-screen flex-col justify-center bg-[var(--n-bg)] px-[54px]">
<div className="n-drag absolute left-0 right-0 top-0 h-[46px]" data-tauri-drag-region />

<span className="flex h-[34px] w-[34px] items-center justify-center rounded-[10px] bg-[var(--n-accent)] text-[16px] font-semibold leading-none text-[var(--n-bg)]">
N
</span>

<h2 className="mb-1.5 mt-[22px] text-[25px] font-semibold leading-[1.25] tracking-[-0.02em] text-[var(--n-text)]">
{linkState === 'invalid' ? 'Link expired or invalid' : 'Choose a new password'}
</h2>

{linkState === 'checking' && (
<p className="max-w-[360px] text-[14.5px] leading-[1.5] text-[var(--n-text-3)]">Checking your link…</p>
)}

{linkState === 'invalid' && (
<>
<p className="mb-[22px] max-w-[360px] text-[14.5px] leading-[1.5] text-[var(--n-text-3)]">
This reset link has expired, was already used, or a newer one was sent.
</p>
<Link to="/forgot-password" className="text-[13.5px] font-medium text-[var(--n-accent-400)] hover:text-[var(--n-accent-300)]">
Send a new link
</Link>
</>
)}

{linkState === 'valid' && (
<>
<p className="mb-[22px] max-w-[360px] text-[14.5px] leading-[1.5] text-[var(--n-text-3)]">
For <strong className="text-[var(--n-text-2)]">{email}</strong>. You'll be signed out everywhere
and can sign in with it straight away.
</p>

<form onSubmit={submit} className="flex max-w-[360px] flex-col gap-2">
{error && <p role="alert" className="text-[13.5px] leading-[1.4] text-[var(--n-text-2)]">{error}</p>}

<input id="new-password" type="password" value={password} aria-label="New password"
onChange={(e) => setPassword(e.target.value)}
required autoFocus autoComplete="new-password"
placeholder="New password"
className={inputClass} />
<input id="confirm-password" type="password" value={confirm} aria-label="Confirm new password"
onChange={(e) => setConfirm(e.target.value)}
required autoComplete="new-password"
placeholder="Confirm new password"
className={inputClass} />
<p className="text-[12.5px] leading-[1.4] text-[var(--n-text-4)]">At least 8 characters, with a letter and a digit.</p>

<button type="submit" disabled={saving}
className="mt-1.5 rounded-[8px] border border-[var(--n-accent)] bg-transparent px-3 py-2.5 text-[15px] font-semibold leading-[1.2] text-[var(--n-accent-300)] transition-colors hover:bg-[rgba(145,132,217,.14)] disabled:opacity-45">
{saving ? 'Saving…' : 'Set new password'}
</button>
</form>
</>
)}
</div>
)
}
Loading
Loading