|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useRouter, useSearchParams } from "next/navigation"; |
| 4 | +import React, { useState } from "react"; |
| 5 | +import { SECRET_ROUTE } from "lib/routes"; |
| 6 | +import { signIn, useUser } from "lib/auth"; |
| 7 | + |
| 8 | +export default function LoginPage() { |
| 9 | + const router = useRouter(); |
| 10 | + const searchParams = useSearchParams(); |
| 11 | + const user = useUser(); |
| 12 | + |
| 13 | + const continueTo = searchParams?.get("continueTo") ?? SECRET_ROUTE; |
| 14 | + |
| 15 | + async function handleSubmit(event: React.FormEvent<HTMLFormElement>) { |
| 16 | + event.preventDefault(); |
| 17 | + const form = event.currentTarget; |
| 18 | + const email = form.email.value; |
| 19 | + const password = form.password.value; |
| 20 | + const rememberMe = form.rememberMe.checked; |
| 21 | + |
| 22 | + try { |
| 23 | + await signIn(email, password, rememberMe); |
| 24 | + router.replace(continueTo); |
| 25 | + } catch (error) { |
| 26 | + window.alert(error); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + if (user) { |
| 31 | + router.replace(continueTo); |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + return ( |
| 36 | + <main className="flex"> |
| 37 | + <section className="flex flex-col py-12 px-16"> |
| 38 | + <div className="text-xl tracking-wide mb-10">Sign In</div> |
| 39 | + |
| 40 | + <div className="mt-5 mx-5"> |
| 41 | + <form onSubmit={handleSubmit}> |
| 42 | + <div className="relative mt-6"> |
| 43 | + <input |
| 44 | + type="email" |
| 45 | + name="email" |
| 46 | + id="email" |
| 47 | + placeholder="Email Address" |
| 48 | + className="peer mt-1 w-full border-b-2 border-gray-300 px-0 py-1 placeholder:text-transparent focus:border-gray-500 focus:outline-none" |
| 49 | + autoComplete="email" |
| 50 | + /> |
| 51 | + <label |
| 52 | + htmlFor="email" |
| 53 | + className="pointer-events-none absolute top-0 left-0 origin-left -translate-y-1/2 transform text-sm text-gray-800 opacity-75 transition-all duration-100 ease-in-out peer-placeholder-shown:top-1/2 peer-placeholder-shown:text-base peer-placeholder-shown:text-gray-500 peer-focus:top-0 peer-focus:pl-0 peer-focus:text-sm peer-focus:text-gray-800" |
| 54 | + > |
| 55 | + Email Address |
| 56 | + </label> |
| 57 | + </div> |
| 58 | + <div className="relative mt-6"> |
| 59 | + <input |
| 60 | + type="password" |
| 61 | + name="password" |
| 62 | + id="password" |
| 63 | + required |
| 64 | + autoComplete="current-password" |
| 65 | + placeholder="Password" |
| 66 | + className="peer peer mt-1 w-full border-b-2 border-gray-300 px-0 py-1 placeholder:text-transparent focus:border-gray-500 focus:outline-none" |
| 67 | + /> |
| 68 | + <label |
| 69 | + htmlFor="password" |
| 70 | + className="pointer-events-none absolute top-0 left-0 origin-left -translate-y-1/2 transform text-sm text-gray-800 opacity-75 transition-all duration-100 ease-in-out peer-placeholder-shown:top-1/2 peer-placeholder-shown:text-base peer-placeholder-shown:text-gray-500 peer-focus:top-0 peer-focus:pl-0 peer-focus:text-sm peer-focus:text-gray-800" |
| 71 | + > |
| 72 | + Password |
| 73 | + </label> |
| 74 | + </div> |
| 75 | + <div className="text-sm my-12 font-semibold text-gray-700 hover:text-black underline"> |
| 76 | + <a href="/#">Forgot password?</a> |
| 77 | + </div> |
| 78 | + <div className="flex items-center justify-between"> |
| 79 | + <label |
| 80 | + htmlFor="remember_me" |
| 81 | + className="block text-sm text-gray-900" |
| 82 | + > |
| 83 | + Remember me |
| 84 | + </label> |
| 85 | + <input |
| 86 | + id="rememberMe" |
| 87 | + name="rememberMe" |
| 88 | + type="checkbox" |
| 89 | + className="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded" |
| 90 | + /> |
| 91 | + </div> |
| 92 | + <div className="my-12 flex justify-center"> |
| 93 | + <button |
| 94 | + type="submit" |
| 95 | + className="w-11/12 bg-blue-800 rounded-full p-1.5 text-white hover:bg-blue-900" |
| 96 | + > |
| 97 | + Sign in |
| 98 | + </button> |
| 99 | + </div> |
| 100 | + <div className="flex text-center text-sm text-gray-500"> |
| 101 | + <p>Don't have an account yet?</p> |
| 102 | + <a |
| 103 | + href="/signup" |
| 104 | + className="ml-1 font-semibold text-gray-600 hover:underline focus:text-gray-800 focus:outline-none" |
| 105 | + > |
| 106 | + Create Account |
| 107 | + </a> |
| 108 | + . |
| 109 | + </div> |
| 110 | + </form> |
| 111 | + </div> |
| 112 | + </section> |
| 113 | + </main> |
| 114 | + ); |
| 115 | +} |
0 commit comments