feat(auth): add LoginCard and wire it into LoginView
LoginCard implements the spec from docs/Max_Composants_Frontend.md: inline per-field validation (email format, non-empty password), a show/hide password toggle, and idle/loading/error/success states. Uses the Lucide `Bot` icon instead of the doc's literal emoji avatar, since that contradicts the same doc's and Max_Design_System.md's "no emoji icons" rule. LoginView becomes a thin wrapper around LoginCard - the login window's close-on-success and listening-reactivation behavior already happen server-side in index.ts's auth-login handler, so nothing else is needed here. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
050aad9469
commit
ed32c21f24
@@ -0,0 +1,142 @@
|
|||||||
|
import { useState } from 'react'
|
||||||
|
import { Bot, Eye, EyeOff } from 'lucide-react'
|
||||||
|
|
||||||
|
export interface LoginCardProps {
|
||||||
|
onSuccess: (token: string) => void
|
||||||
|
onError?: (message: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FieldErrors {
|
||||||
|
email?: string
|
||||||
|
password?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Panneau de connexion flottant (docs/Max_Composants_Frontend.md section 1). Un avatar emoji est
|
||||||
|
* demande par la spec, mais ca contredit la regle globale "aucune icone emoji" du meme document et
|
||||||
|
* de Max_Design_System.md : on utilise donc l'icone Lucide `Bot` a la place.
|
||||||
|
*/
|
||||||
|
export default function LoginCard({ onSuccess, onError }: LoginCardProps): React.JSX.Element {
|
||||||
|
const [email, setEmail] = useState('')
|
||||||
|
const [password, setPassword] = useState('')
|
||||||
|
const [showPassword, setShowPassword] = useState(false)
|
||||||
|
const [fieldErrors, setFieldErrors] = useState<FieldErrors>({})
|
||||||
|
const [submitError, setSubmitError] = useState<string | null>(null)
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
|
const [success, setSuccess] = useState(false)
|
||||||
|
|
||||||
|
function validate(): FieldErrors {
|
||||||
|
const errors: FieldErrors = {}
|
||||||
|
if (!email.includes('@')) errors.email = 'Adresse email invalide'
|
||||||
|
if (password.length === 0) errors.password = 'Mot de passe requis'
|
||||||
|
return errors
|
||||||
|
}
|
||||||
|
|
||||||
|
const submit = (event: React.FormEvent): void => {
|
||||||
|
event.preventDefault()
|
||||||
|
if (loading) return
|
||||||
|
|
||||||
|
const errors = validate()
|
||||||
|
setFieldErrors(errors)
|
||||||
|
if (Object.keys(errors).length > 0) return
|
||||||
|
|
||||||
|
setLoading(true)
|
||||||
|
setSubmitError(null)
|
||||||
|
window.api
|
||||||
|
.login(email.trim(), password)
|
||||||
|
.then((result) => {
|
||||||
|
if (result.ok) {
|
||||||
|
setSuccess(true)
|
||||||
|
// window.api.login ne renvoie pas le jeton (il reste dans le process main, voir
|
||||||
|
// session.ts) : on signale juste le succes au composant parent.
|
||||||
|
onSuccess('')
|
||||||
|
} else {
|
||||||
|
setSubmitError(result.error)
|
||||||
|
onError?.(result.error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
const message = 'Erreur inattendue'
|
||||||
|
setSubmitError(message)
|
||||||
|
onError?.(message)
|
||||||
|
})
|
||||||
|
.finally(() => setLoading(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
return (
|
||||||
|
<div className="auth-panel">
|
||||||
|
<p className="auth-panel__success">
|
||||||
|
Connexion reussie. Cette fenetre se ferme automatiquement...
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="auth-panel">
|
||||||
|
<div className="auth-panel__brand">
|
||||||
|
<Bot size={20} aria-hidden="true" />
|
||||||
|
<span className="auth-panel__title">Connexion a Max</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form onSubmit={submit} noValidate>
|
||||||
|
<label className="auth-field">
|
||||||
|
<span className="auth-field__label">Email</span>
|
||||||
|
<input
|
||||||
|
type="email"
|
||||||
|
value={email}
|
||||||
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
|
autoFocus
|
||||||
|
className="auth-field__input"
|
||||||
|
/>
|
||||||
|
{fieldErrors.email && (
|
||||||
|
<span className="auth-field__error" role="alert">
|
||||||
|
{fieldErrors.email}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label className="auth-field">
|
||||||
|
<span className="auth-field__label">Mot de passe</span>
|
||||||
|
<div className="auth-field__password">
|
||||||
|
<input
|
||||||
|
type={showPassword ? 'text' : 'password'}
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
className="auth-field__input"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="auth-field__toggle"
|
||||||
|
aria-label={showPassword ? 'Masquer le mot de passe' : 'Afficher le mot de passe'}
|
||||||
|
onClick={() => setShowPassword((prev) => !prev)}
|
||||||
|
>
|
||||||
|
{showPassword ? <EyeOff size={16} /> : <Eye size={16} />}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{fieldErrors.password && (
|
||||||
|
<span className="auth-field__error" role="alert">
|
||||||
|
{fieldErrors.password}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</label>
|
||||||
|
|
||||||
|
{submitError && (
|
||||||
|
<p className="auth-panel__error" role="alert">
|
||||||
|
{submitError}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={loading}
|
||||||
|
aria-label="Se connecter"
|
||||||
|
className="auth-panel__submit"
|
||||||
|
>
|
||||||
|
{loading ? 'Connexion...' : 'Se connecter'}
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,74 +1,11 @@
|
|||||||
import { useState } from 'react'
|
import LoginCard from './LoginCard'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Point d'entree de la fenetre de connexion separee (voir src/main/auth/loginWindow.ts et la
|
||||||
|
* route #login dans main.tsx). La fermeture de la fenetre et la reactivation de l'ecoute vocale
|
||||||
|
* sont deja declenchees cote main process une fois la connexion reussie (voir le handler IPC
|
||||||
|
* "auth-login" dans src/main/index.ts) : rien d'autre a faire ici que d'afficher LoginCard.
|
||||||
|
*/
|
||||||
export default function LoginView(): React.JSX.Element {
|
export default function LoginView(): React.JSX.Element {
|
||||||
const [email, setEmail] = useState('')
|
return <LoginCard onSuccess={() => {}} />
|
||||||
const [password, setPassword] = useState('')
|
|
||||||
const [error, setError] = useState<string | null>(null)
|
|
||||||
const [submitting, setSubmitting] = useState(false)
|
|
||||||
const [success, setSuccess] = useState(false)
|
|
||||||
|
|
||||||
const canSubmit = email.trim() !== '' && password !== '' && !submitting
|
|
||||||
|
|
||||||
const submit = (event: React.FormEvent): void => {
|
|
||||||
event.preventDefault()
|
|
||||||
if (!canSubmit) return
|
|
||||||
setSubmitting(true)
|
|
||||||
setError(null)
|
|
||||||
window.api
|
|
||||||
.login(email.trim(), password)
|
|
||||||
.then((result) => {
|
|
||||||
if (result.ok) {
|
|
||||||
setSuccess(true)
|
|
||||||
} else {
|
|
||||||
setError(result.error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(() => setError('Erreur inattendue'))
|
|
||||||
.finally(() => setSubmitting(false))
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="auth-panel">
|
|
||||||
<h1 className="auth-panel__title">Connexion a Max</h1>
|
|
||||||
|
|
||||||
{success ? (
|
|
||||||
<p className="auth-panel__success">
|
|
||||||
Connexion reussie. Cette fenetre se ferme automatiquement...
|
|
||||||
</p>
|
|
||||||
) : (
|
|
||||||
<form onSubmit={submit}>
|
|
||||||
<label className="auth-field">
|
|
||||||
<span className="auth-field__label">Email</span>
|
|
||||||
<input
|
|
||||||
type="email"
|
|
||||||
value={email}
|
|
||||||
onChange={(e) => setEmail(e.target.value)}
|
|
||||||
autoFocus
|
|
||||||
className="auth-field__input"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label className="auth-field">
|
|
||||||
<span className="auth-field__label">Mot de passe</span>
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
value={password}
|
|
||||||
onChange={(e) => setPassword(e.target.value)}
|
|
||||||
className="auth-field__input"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<p className="auth-panel__error" role="alert">
|
|
||||||
{error}
|
|
||||||
</p>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<button type="submit" disabled={!canSubmit} className="auth-panel__submit">
|
|
||||||
{submitting ? 'Connexion...' : 'Se connecter'}
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,11 +94,18 @@
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.auth-panel__brand {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--space-2);
|
||||||
|
color: var(--accent);
|
||||||
|
margin-bottom: var(--space-5);
|
||||||
|
}
|
||||||
|
|
||||||
.auth-panel__title {
|
.auth-panel__title {
|
||||||
font-size: var(--text-md);
|
font-size: var(--text-md);
|
||||||
font-weight: var(--weight-semi);
|
font-weight: var(--weight-semi);
|
||||||
color: var(--text-1);
|
color: var(--text-1);
|
||||||
margin-bottom: var(--space-5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.auth-panel__success {
|
.auth-panel__success {
|
||||||
@@ -137,6 +144,39 @@
|
|||||||
border-color: var(--border-active);
|
border-color: var(--border-active);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.auth-field__password {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-field__password .auth-field__input {
|
||||||
|
padding-right: calc(var(--space-3) + 24px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-field__toggle {
|
||||||
|
position: absolute;
|
||||||
|
right: var(--space-2);
|
||||||
|
top: 50%;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
border: none;
|
||||||
|
background: none;
|
||||||
|
color: var(--text-2);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-field__toggle:hover {
|
||||||
|
color: var(--text-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-field__error {
|
||||||
|
display: block;
|
||||||
|
margin-top: var(--space-1);
|
||||||
|
font-size: var(--text-xs);
|
||||||
|
color: var(--red);
|
||||||
|
}
|
||||||
|
|
||||||
.auth-panel__error {
|
.auth-panel__error {
|
||||||
margin-bottom: var(--space-4);
|
margin-bottom: var(--space-4);
|
||||||
font-size: var(--text-sm);
|
font-size: var(--text-sm);
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||||
|
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||||
|
import LoginCard from '../../../src/renderer/src/components/LoginCard'
|
||||||
|
|
||||||
|
const login = vi.fn()
|
||||||
|
const onSuccess = vi.fn()
|
||||||
|
const onError = vi.fn()
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks()
|
||||||
|
;(window as unknown as { api: Record<string, unknown> }).api = { login }
|
||||||
|
})
|
||||||
|
|
||||||
|
function fillAndSubmit(email: string, password: string): void {
|
||||||
|
fireEvent.change(screen.getByLabelText('Email'), { target: { value: email } })
|
||||||
|
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: password } })
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('<LoginCard />', () => {
|
||||||
|
it('should show an inline error and not call the API when the email is invalid', async () => {
|
||||||
|
render(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||||
|
|
||||||
|
fillAndSubmit('not-an-email', 'password123')
|
||||||
|
|
||||||
|
expect(await screen.findByText('Adresse email invalide')).toBeInTheDocument()
|
||||||
|
expect(login).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should show an inline error and not call the API when the password is empty', async () => {
|
||||||
|
render(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||||
|
|
||||||
|
fillAndSubmit('william@example.com', '')
|
||||||
|
|
||||||
|
expect(await screen.findByText('Mot de passe requis')).toBeInTheDocument()
|
||||||
|
expect(login).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should toggle the password field between hidden and visible text', () => {
|
||||||
|
render(<LoginCard onSuccess={onSuccess} />)
|
||||||
|
|
||||||
|
const passwordInput = screen.getByLabelText<HTMLInputElement>('Mot de passe')
|
||||||
|
expect(passwordInput.type).toBe('password')
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Afficher le mot de passe' }))
|
||||||
|
expect(passwordInput.type).toBe('text')
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Masquer le mot de passe' }))
|
||||||
|
expect(passwordInput.type).toBe('password')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should call the API and show a success message on success', async () => {
|
||||||
|
login.mockResolvedValue({ ok: true })
|
||||||
|
render(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||||
|
|
||||||
|
fillAndSubmit('william@example.com', 'password123')
|
||||||
|
|
||||||
|
expect(login).toHaveBeenCalledWith('william@example.com', 'password123')
|
||||||
|
await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument())
|
||||||
|
expect(onSuccess).toHaveBeenCalled()
|
||||||
|
expect(onError).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should show the server error and call onError on failure', async () => {
|
||||||
|
login.mockResolvedValue({ ok: false, error: 'Email ou mot de passe incorrect' })
|
||||||
|
render(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||||
|
|
||||||
|
fillAndSubmit('william@example.com', 'mauvais')
|
||||||
|
|
||||||
|
await waitFor(() =>
|
||||||
|
expect(screen.getByText('Email ou mot de passe incorrect')).toBeInTheDocument()
|
||||||
|
)
|
||||||
|
expect(onError).toHaveBeenCalledWith('Email ou mot de passe incorrect')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should show a generic error when the IPC call itself rejects', async () => {
|
||||||
|
login.mockRejectedValue(new Error('IPC down'))
|
||||||
|
render(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||||
|
|
||||||
|
fillAndSubmit('william@example.com', 'password123')
|
||||||
|
|
||||||
|
await waitFor(() => expect(screen.getByText('Erreur inattendue')).toBeInTheDocument())
|
||||||
|
expect(onError).toHaveBeenCalledWith('Erreur inattendue')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should disable the submit button and show a loading label while submitting', async () => {
|
||||||
|
let resolveLogin: (value: { ok: true }) => void = () => {}
|
||||||
|
login.mockReturnValue(
|
||||||
|
new Promise((resolve) => {
|
||||||
|
resolveLogin = resolve
|
||||||
|
})
|
||||||
|
)
|
||||||
|
render(<LoginCard onSuccess={onSuccess} />)
|
||||||
|
|
||||||
|
fillAndSubmit('william@example.com', 'password123')
|
||||||
|
|
||||||
|
const submit = screen.getByRole('button', { name: 'Se connecter' })
|
||||||
|
expect(submit).toBeDisabled()
|
||||||
|
expect(screen.getByText('Connexion...')).toBeInTheDocument()
|
||||||
|
|
||||||
|
resolveLogin({ ok: true })
|
||||||
|
await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument())
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,71 +1,14 @@
|
|||||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
import { render, screen } from '@testing-library/react'
|
||||||
import LoginView from '../../../src/renderer/src/components/LoginView'
|
import LoginView from '../../../src/renderer/src/components/LoginView'
|
||||||
|
|
||||||
const login = vi.fn()
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
;(window as unknown as { api: Record<string, unknown> }).api = { login: vi.fn() }
|
||||||
;(window as unknown as { api: Record<string, unknown> }).api = { login }
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('LoginView', () => {
|
describe('<LoginView />', () => {
|
||||||
it('should keep the submit button disabled until both fields are filled', () => {
|
it('should render the LoginCard form', () => {
|
||||||
render(<LoginView />)
|
render(<LoginView />)
|
||||||
|
expect(screen.getByRole('button', { name: 'Se connecter' })).toBeInTheDocument()
|
||||||
const submit = screen.getByRole('button', { name: 'Se connecter' })
|
|
||||||
expect(submit).toBeDisabled()
|
|
||||||
|
|
||||||
fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } })
|
|
||||||
expect(submit).toBeDisabled()
|
|
||||||
|
|
||||||
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } })
|
|
||||||
expect(submit).toBeEnabled()
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should call window.api.login with the trimmed email and show a success message', async () => {
|
|
||||||
login.mockResolvedValue({ ok: true })
|
|
||||||
render(<LoginView />)
|
|
||||||
|
|
||||||
fireEvent.change(screen.getByLabelText('Email'), {
|
|
||||||
target: { value: ' william@example.com ' }
|
|
||||||
})
|
|
||||||
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } })
|
|
||||||
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
|
|
||||||
|
|
||||||
expect(login).toHaveBeenCalledWith('william@example.com', 'password123')
|
|
||||||
await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument())
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should display the error message returned by the API on failure', async () => {
|
|
||||||
login.mockResolvedValue({ ok: false, error: 'Email ou mot de passe incorrect' })
|
|
||||||
render(<LoginView />)
|
|
||||||
|
|
||||||
fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } })
|
|
||||||
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'mauvais' } })
|
|
||||||
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
|
|
||||||
|
|
||||||
await waitFor(() =>
|
|
||||||
expect(screen.getByRole('alert')).toHaveTextContent('Email ou mot de passe incorrect')
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should display a generic error message when the IPC call itself rejects', async () => {
|
|
||||||
login.mockRejectedValue(new Error('IPC down'))
|
|
||||||
render(<LoginView />)
|
|
||||||
|
|
||||||
fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } })
|
|
||||||
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } })
|
|
||||||
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
|
|
||||||
|
|
||||||
await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent('Erreur inattendue'))
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should not submit when the fields are empty', () => {
|
|
||||||
render(<LoginView />)
|
|
||||||
|
|
||||||
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
|
|
||||||
|
|
||||||
expect(login).not.toHaveBeenCalled()
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user