2024-12-01 15:33:24 +00:00
|
|
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
|
|
|
|
2024-12-01 16:31:56 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import { FloatLabel } from 'primereact/floatlabel';
|
|
|
|
import { InputText } from 'primereact/inputtext';
|
2024-12-01 17:47:42 +01:00
|
|
|
import { login } from '../../api/authentication';
|
2024-12-01 17:29:57 +01:00
|
|
|
import { useState, useEffect } from 'react';
|
2024-12-01 16:31:56 +01:00
|
|
|
import { classNames } from './button';
|
|
|
|
import { useRouter } from 'next/navigation'
|
2024-12-01 17:29:57 +01:00
|
|
|
import { User } from '../../lib/definitions';
|
2024-12-01 18:02:25 +01:00
|
|
|
import { getSlug } from '@/app/lib/utils';
|
2024-12-01 16:31:56 +01:00
|
|
|
|
|
|
|
export default function LoginForm() {
|
|
|
|
const [email, setEmail] = useState("");
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
|
2024-12-01 17:29:57 +01:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const [currentUser, setCurrentUser] = useState<User | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
localStorage.setItem('currentUser', JSON.stringify(currentUser));
|
|
|
|
}, [currentUser]);
|
2024-12-01 16:31:56 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="card flex justify-evenly py-5">
|
|
|
|
<FloatLabel>
|
|
|
|
<InputText id="email" type="email" className='rounded-sm' onChange={(e) => setEmail(e.target.value)} />
|
|
|
|
<label htmlFor="email">Email</label>
|
|
|
|
</FloatLabel>
|
|
|
|
<FloatLabel>
|
|
|
|
<InputText id="password" type="password" className='rounded-sm' onChange={(e) => setPassword(e.target.value)} />
|
|
|
|
<label htmlFor="password">Password</label>
|
|
|
|
</FloatLabel>
|
|
|
|
<button
|
|
|
|
className={classNames('primary')}
|
|
|
|
disabled={email.length == 0 || password.length == 0}
|
2024-12-01 17:29:57 +01:00
|
|
|
onClick={() => login({
|
|
|
|
email: email,
|
|
|
|
password: password,
|
2024-12-01 17:47:42 +01:00
|
|
|
onLogin: (user) => {
|
|
|
|
setCurrentUser(user);
|
2024-12-01 18:02:25 +01:00
|
|
|
router.push(`${getSlug()}/dashboard`)
|
2024-12-01 17:29:57 +01:00
|
|
|
}
|
|
|
|
})}>
|
2024-12-01 16:31:56 +01:00
|
|
|
Sign in
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|