1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import { useState } from "react"
import { useRouter } from "next/router"
import { medusaClient } from "../lib/medusa-client"
import { setStoredToken } from "../lib/storefront"
export default function LoginPage() {
const router = useRouter()
const [form, setForm] = useState({ email: "", password: "" })
const [status, setStatus] = useState("")
const [isLoading, setIsLoading] = useState(false)
const handleChange = (event) => {
const { name, value } = event.target
setForm((prev) => ({ ...prev, [name]: value }))
}
const handleSubmit = async (event) => {
event.preventDefault()
setStatus("")
setIsLoading(true)
try {
const { access_token: accessToken } = await medusaClient.auth.getToken({
email: form.email,
password: form.password,
})
setStoredToken(accessToken)
medusaClient.setToken(accessToken)
setStatus("Connexion réussie.")
router.push("/")
} catch (error) {
setStatus("Identifiants invalides ou indisponibles.")
} finally {
setIsLoading(false)
}
}
return (
<div style={{ maxWidth: "420px", margin: "0 auto" }}>
<h1>Se connecter</h1>
<form onSubmit={handleSubmit} style={{ display: "grid", gap: "1rem" }}>
<label>
Email
<input
name="email"
type="email"
value={form.email}
onChange={handleChange}
required
style={{ width: "100%", padding: "0.5rem", marginTop: "0.5rem" }}
/>
</label>
<label>
Mot de passe
<input
name="password"
type="password"
value={form.password}
onChange={handleChange}
required
style={{ width: "100%", padding: "0.5rem", marginTop: "0.5rem" }}
/>
</label>
<button
type="submit"
disabled={isLoading}
style={{
border: "1px solid #ccc",
background: "#fff",
borderRadius: "6px",
padding: "0.6rem",
cursor: "pointer",
}}
>
{isLoading ? "Connexion..." : "Se connecter"}
</button>
{status && <p>{status}</p>}
</form>
</div>
)
}
|