Define a basic login form with redirection to the Dashboard
All checks were successful
Check usage of free licenses / build-static-assets (pull_request) Successful in 47s
Add copyright notice / copyright_notice (pull_request) Successful in 1m2s
Playwright Tests / test (pull_request) Successful in 4m32s

This commit is contained in:
Manuel Bustillo 2024-12-01 16:31:56 +01:00
parent 15144b478e
commit 9b3443addf
3 changed files with 65 additions and 1 deletions

View File

@ -0,0 +1,15 @@
import { getCsrfToken } from '@/app/lib/utils';
export function login({ email, password, onLogin }: { email: string, password: string, onLogin: () => void }) {
console.log(email, password);
return fetch("/api/default/users/sign_in", {
method: 'POST',
body: JSON.stringify({ user: { email, password } }),
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': getCsrfToken(),
}
})
.then(onLogin)
.catch((error) => console.error(error));
}

View File

@ -2,11 +2,26 @@
import Link from 'next/link';
import styles from '@/app/ui/home.module.css';
import LoginForm from '@/app/ui/components/login-form';
export default function Page() {
return (
<main className="flex min-h-screen flex-col p-6">
<div className="flex flex-row">
<div className="w-1/2">
Already have an account? Sign in
<LoginForm />
</div>
<div className="w-1/2">
Don't have an account? Register now!
</div>
</div>
</main>
);
}

View File

@ -0,0 +1,34 @@
'use client';
import { FloatLabel } from 'primereact/floatlabel';
import { InputText } from 'primereact/inputtext';
import { login } from '../../api/authentication';
import { useState } from 'react';
import { classNames } from './button';
import { useRouter } from 'next/navigation'
export default function LoginForm() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const router = useRouter()
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}
onClick={() => login({ email: email, password: password, onLogin: () => router.push('/dashboard') })}>
Sign in
</button>
</div>
)
}