Guest User

Login.js

a guest
Jan 12th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use client'
  2. import { useState } from 'react';
  3. import { account } from "@/utils/appwrite"
  4. import { usePathname, useRouter } from 'next/navigation';
  5.  
  6. export const metadata = {
  7.   title: 'D&DMCT | Login',
  8.   description: 'D&D Character Management Tool Login page'
  9. }
  10.  
  11. export default function LoginPage() {
  12.   const [email, setEmail] = useState('');
  13.   const [password, setPassword] = useState('');
  14.   const [loading, setLoading] = useState(false);
  15.  
  16.   const router = useRouter();
  17.  
  18.   const IsLoggedIn = async () =>{
  19.     try{
  20.       var res = await account.get()
  21.     }
  22.     catch (e){
  23.       console.log("IsLogged In: " + e)
  24.     }
  25.   }
  26.  
  27.   async function handleLogin() {
  28.     try {
  29.       setLoading(true);
  30.       await account.createEmailSession(email, password);
  31.       setPassword('');
  32.       setLoading(false);
  33.       IsLoggedIn();
  34.       // Redirect to the desired page after successful login
  35.       redirectTo('./characterpage');
  36.     } catch (error) {
  37.       console.error(error);
  38.       setLoading(false);
  39.     }
  40.   }
  41.  
  42.   async function handleRegister() {
  43.     try {
  44.       setLoading(true);
  45.       await account.create(ID.unique(),email,password)
  46.       setPassword('');
  47.       setLoading(false);
  48.       IsLoggedIn();
  49.       // Redirect to the desired page after successful login
  50.       redirectTo('./characterpage');
  51.     } catch (error) {
  52.       console.error(error);
  53.  
  54.       setLoading(false);
  55.     }
  56.   }
  57.  
  58.   const redirectTo = (path) => {
  59.     router.replace(path);
  60.   };
  61.  
  62.   return (
  63.     <div className='flex h-screen justify-center items-center'>
  64.       <div className="text-black bg-white p-8 rounded shadow-md h-82 w-96">
  65.       <h1 className="text-3xl font-semibold mb-4">Login</h1>
  66.       <form>
  67.         {/* email Input */}
  68.         <div className="mb-4">
  69.           <label htmlFor="email" className="block text-sm font-medium text-gray-600">
  70.             Email
  71.           </label>
  72.           <input
  73.             id="email"
  74.             type="text"
  75.             placeholder="Enter your email"
  76.             value={email}
  77.             onChange={(e) => setEmail(e.target.value)}
  78.             className="w-full mt-1 p-2 border border-gray-300 rounded focus:outline-none focus:border-blue-500"
  79.           />
  80.         </div>
  81.  
  82.         {/* Password Input */}
  83.         <div className="mb-4">
  84.           <label htmlFor="password" className="block text-sm font-medium text-gray-600">
  85.             Password
  86.           </label>
  87.           <input
  88.             id="password"
  89.             type="password"
  90.             placeholder="Enter your password"
  91.             value={password}
  92.             onChange={(e) => setPassword(e.target.value)}
  93.             className="w-full mt-1 p-2 border border-gray-300 rounded focus:outline-none focus:border-blue-500"
  94.           />
  95.         </div>
  96.       <div className='flex justify-center items-center'>
  97.          {/* Login Button with Spinner */}
  98.          <button
  99.           type="button"
  100.           onClick={handleLogin}
  101.           className={`relative bg-blue-500 text-white p-2 rounded hover:bg-blue-600 focus:outline-none focus:bg-blue-600 ${
  102.             loading && 'opacity-50 cursor-not-allowed'
  103.           }`}
  104.           disabled={loading}
  105.         >
  106.           {loading && (
  107.             <div className="spinner absolute inset-0 flex items-center justify-center">
  108.               <div className="animate-spin rounded-full h-6 w-6 border-t-2 border-blue-300 border-r-0 border-b-0 border-l-0"></div>
  109.             </div>
  110.           )}
  111.           {loading ? 'Loading...' : 'Login'}
  112.         </button>
  113.         {/* Register button */}
  114.         <button
  115.           type="button"
  116.           onClick={handleRegister}
  117.           className={`relative ml-2 bg-blue-500 text-white p-2 rounded hover:bg-blue-600 focus:outline-none focus:bg-blue-600 ${
  118.             loading && 'opacity-50 cursor-not-allowed'
  119.           }`}
  120.           disabled={loading}
  121.         >
  122.           {loading && (
  123.             <div className="spinner absolute inset-0 flex items-center justify-center">
  124.               <div className="animate-spin rounded-full h-6 w-6 border-t-2 border-blue-300 border-r-0 border-b-0 border-l-0"></div>
  125.             </div>
  126.           )}
  127.           {loading ? 'Loading...' : 'Register'}
  128.         </button>
  129.       </div>
  130.        
  131.  
  132.       </form>
  133.     </div>
  134.     </div>  
  135.   );
  136. }
Advertisement
Add Comment
Please, Sign In to add comment