|
| 1 | +"use client"; |
| 2 | +import { useRouter } from "next/navigation"; |
| 3 | + |
| 4 | +export default function Login() { |
| 5 | + const router = useRouter(); |
| 6 | + async function handleSubmit(e: React.FormEvent<HTMLFormElement>) { |
| 7 | + e.preventDefault(); |
| 8 | + |
| 9 | + const formData = new FormData(e.currentTarget); |
| 10 | + |
| 11 | + const res = await fetch("http://127.0.0.1:8000/api/auth/login/", { |
| 12 | + method: "POST", |
| 13 | + headers: { |
| 14 | + "Content-Type": "application/json", |
| 15 | + }, |
| 16 | + body: JSON.stringify({ |
| 17 | + username: formData.get("username"), |
| 18 | + email: formData.get("email"), |
| 19 | + password: formData.get("password"), |
| 20 | + }), |
| 21 | + }); |
| 22 | + |
| 23 | + const data = await res.json(); |
| 24 | + if (!res.ok) { |
| 25 | + alert("Invalid login"); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + localStorage.setItem("access", data.access); |
| 30 | + localStorage.setItem("refresh", data.refresh); |
| 31 | + |
| 32 | + const token = localStorage.getItem("access"); |
| 33 | + |
| 34 | + const userFetch = await fetch( |
| 35 | + "http://127.0.0.1:8000/api/planner/protected", |
| 36 | + { |
| 37 | + headers: { |
| 38 | + Authorization: `Bearer ${token}`, |
| 39 | + }, |
| 40 | + }, |
| 41 | + ); |
| 42 | + |
| 43 | + const user = await userFetch.json(); |
| 44 | + router.push(`/${user.user_id}/tasks`); |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className="flex min-h-screen flex-col justify-center bg-gray-900 px-6 py-12 lg:px-8"> |
| 49 | + <div className="sm:mx-auto sm:w-full sm:max-w-sm"> |
| 50 | + <h2 className="mt-10 text-center text-2xl/9 font-bold tracking-tight text-white"> |
| 51 | + Log in to your account |
| 52 | + </h2> |
| 53 | + </div> |
| 54 | + |
| 55 | + <div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm"> |
| 56 | + <form onSubmit={handleSubmit} className="space-y-6"> |
| 57 | + <label className="block text-sm/6 font-medium text-gray-100"> |
| 58 | + Username |
| 59 | + </label> |
| 60 | + <div className="mt-2"> |
| 61 | + <input |
| 62 | + id="username" |
| 63 | + type="text" |
| 64 | + name="username" |
| 65 | + required |
| 66 | + autoComplete="username" |
| 67 | + className="block w-full rounded-md bg-white/5 px-3 py-1.5 text-base text-white outline-1 -outline-offset-1 outline-white/10 placeholder:text-gray-500 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-500 sm:text-sm/6" |
| 68 | + /> |
| 69 | + </div> |
| 70 | + |
| 71 | + <label className="block text-sm/6 font-medium text-gray-100"> |
| 72 | + Email |
| 73 | + </label> |
| 74 | + <div className="mt-2"> |
| 75 | + <input |
| 76 | + id="email" |
| 77 | + type="email" |
| 78 | + name="email" |
| 79 | + required |
| 80 | + autoComplete="email" |
| 81 | + className="block w-full rounded-md bg-white/5 px-3 py-1.5 text-base text-white outline-1 -outline-offset-1 outline-white/10 placeholder:text-gray-500 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-500 sm:text-sm/6" |
| 82 | + /> |
| 83 | + </div> |
| 84 | + |
| 85 | + <div> |
| 86 | + <div className="flex items-center justify-between"> |
| 87 | + <label className="block text-sm/6 font-medium text-gray-100"> |
| 88 | + Password |
| 89 | + </label> |
| 90 | + <div className="text-sm"> |
| 91 | + <a className="font-semibold text-indigo-400 hover:text-indigo-300"> |
| 92 | + Forgot password? |
| 93 | + </a> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + <div className="mt-2"> |
| 97 | + <input |
| 98 | + id="password" |
| 99 | + type="password" |
| 100 | + name="password" |
| 101 | + required |
| 102 | + className="block w-full rounded-md bg-white/5 px-3 py-1.5 text-base text-white outline-1 -outline-offset-1 outline-white/10 placeholder:text-gray-500 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-500 sm:text-sm/6" |
| 103 | + /> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + |
| 107 | + <div> |
| 108 | + <button |
| 109 | + type="submit" |
| 110 | + className="flex w-full justify-center rounded-md bg-indigo-500 px-3 py-1.5 text-sm/6 font-semibold text-white hover:bg-indigo-400 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500" |
| 111 | + > |
| 112 | + Sign in |
| 113 | + </button> |
| 114 | + </div> |
| 115 | + </form> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +} |
0 commit comments