mirror of
https://github.com/Fayorg/calendrier-avant.git
synced 2026-05-27 17:18:38 +02:00
add: full authentication with NextAuth
This commit is contained in:
45
app/page.tsx
45
app/page.tsx
@@ -1,49 +1,20 @@
|
||||
'use client';
|
||||
|
||||
import { Poppins } from 'next/font/google';
|
||||
import { Check } from 'lucide-react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
import logo from '../images/logo.svg';
|
||||
import Image from 'next/image';
|
||||
import { Input } from '@components/ui/input';
|
||||
import { white } from 'next/dist/lib/picocolors';
|
||||
import { signIn } from 'next-auth/react';
|
||||
import { LoginForm } from '@/components/forms/LoginForm';
|
||||
import { getAuthServerSession } from '@/lib/authenticate';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export default function Home() {
|
||||
const [password, setPassword] = useState('');
|
||||
const router = useRouter();
|
||||
export default async function Home() {
|
||||
const session = await getAuthServerSession();
|
||||
|
||||
useEffect(() => {
|
||||
if (localStorage.getItem('@password')) {
|
||||
router.push('/play');
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
|
||||
// localStorage.setItem('@password', password);
|
||||
// router.push('/play');
|
||||
|
||||
console.log('Trying to sign in');
|
||||
const result = await signIn('credentials', {
|
||||
key: password,
|
||||
callbackUrl: '/play',
|
||||
});
|
||||
if (session != null) {
|
||||
redirect('/play');
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={'w-full h-screen text-[#F0F0F0] bg-black p-12 flex flex-col items-center justify-center gap-y-28'}>
|
||||
<Image src={logo} alt={'Logo'} width={100} height={200} className={'mx-auto w-full md:w-[400px]'} />
|
||||
<form onSubmit={handleSubmit} className={'flex flex-row gap-4 w-full md:w-[400px]'}>
|
||||
<Input type="password" placeholder="Mot de passe" id="password" name="password" value={password} onChange={(e) => setPassword(e.target.value)}></Input>
|
||||
<input type="submit" value="Submit" id="submit" className={'hidden'} />
|
||||
<label htmlFor="submit" className={'w-[54px] h-[54px] bg-secondary rounded-[20px] contents-none grid place-content-center'}>
|
||||
<Check width={24} height={24} color={'white'} />
|
||||
</label>
|
||||
</form>
|
||||
<LoginForm />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
47
app/play/TodayTest.tsx
Normal file
47
app/play/TodayTest.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
'use client';
|
||||
|
||||
import { getActiveTestWithGrade } from '@/actions/mangeTest';
|
||||
import { TestCard } from '@/components/custom';
|
||||
import { Session } from 'next-auth';
|
||||
import { signOut } from 'next-auth/react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import Image from 'next/image';
|
||||
import LogOut from '@images/logout.svg';
|
||||
import { GradingForm } from '@/components/forms/GradingForm';
|
||||
import YourGrade from '@images/your-grade.svg';
|
||||
|
||||
export function TodayTest({ session }: { session: Session }) {
|
||||
const [activeTest, setActiveTest] = useState<{ data: any | null; error: Error | null; isLoading: boolean }>({ isLoading: true, data: null, error: null });
|
||||
|
||||
useEffect(() => {
|
||||
getActiveTestWithGrade(new Date(), session.user.id)
|
||||
.catch((err) => setActiveTest({ data: null, error: err, isLoading: false }))
|
||||
.then((data) => setActiveTest({ data, error: null, isLoading: false }));
|
||||
}, [session.user.id]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<TestCard data={activeTest.data} error={activeTest.error} isLoading={activeTest.isLoading} />
|
||||
{activeTest.data &&
|
||||
(activeTest.data.grades[0] ? (
|
||||
<div className={'w-full md:w-[400px]'}>
|
||||
<div className={'flex flex-row justify-evenly'}>
|
||||
<Image src={YourGrade} alt={'Your Grade'} width={100} />
|
||||
<span className={'w-[54px] h-[54px] bg-accent rounded-[20px] contents-none grid place-content-center'}>{activeTest.data.grades[0].grade}</span>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<GradingForm session={session} testId={activeTest.data.id} />
|
||||
))}
|
||||
<div>
|
||||
<button
|
||||
onClick={async () => {
|
||||
await signOut();
|
||||
}}
|
||||
>
|
||||
<Image src={LogOut} alt={'Logo'} width={50} height={50} className={'mx-auto w-full md:w-[400px]'} />
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
12
app/play/layout.tsx
Normal file
12
app/play/layout.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import { getAuthServerSession } from '@/lib/authenticate';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export default async function Layout({ children }: { children: React.ReactNode }) {
|
||||
const session = await getAuthServerSession();
|
||||
|
||||
if (session == null) {
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
@@ -1,43 +1,35 @@
|
||||
'use client';
|
||||
|
||||
import { GradingForm, TestCard } from '@/components/custom';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useState, useEffect } from 'react';
|
||||
import useSWR from 'swr';
|
||||
import { fetcher } from '@/lib/fetcher';
|
||||
import Image from 'next/image';
|
||||
import Logo from '/images/logo.svg';
|
||||
import LogOut from '@images/logout.svg';
|
||||
import { getAuthServerSession } from '@/lib/authenticate';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getActiveTest } from '@/actions/mangeTest';
|
||||
import { TodayTest } from './TodayTest';
|
||||
|
||||
export default function Play() {
|
||||
const router = useRouter();
|
||||
const [password, setPassword] = useState<string | null>('');
|
||||
export default async function Play() {
|
||||
const session = await getAuthServerSession();
|
||||
|
||||
const { data, error, isLoading } = useSWR('/api/test?key=' + password, fetcher);
|
||||
if (session == null) {
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
// useEffect(() => {
|
||||
// const pass = localStorage.getItem('@password');
|
||||
// setPassword(pass);
|
||||
// if (!pass) {
|
||||
// router.push('/');
|
||||
// }
|
||||
// }, [router]);
|
||||
const now = new Date();
|
||||
const todayTest = await getActiveTest(now);
|
||||
|
||||
return (
|
||||
<div className={'w-full h-[100vh] text-[#F0F0F0] bg-black p-12 flex flex-col items-center justify-center gap-y-28'}>
|
||||
<Image src={Logo} alt={'Logo'} width={100} height={200} className={'mx-auto w-full md:w-[400px]'} />
|
||||
<TestCard data={data} error={error} isLoading={isLoading} />
|
||||
{data && data.status == 200 && password && <GradingForm password={password} data={data} />}
|
||||
<div>
|
||||
<TodayTest session={session} />
|
||||
{/* <TestCard data={data} error={error} isLoading={isLoading} /> */}
|
||||
{/* {data && data.status == 200 && password && <GradingForm password={password} data={data} />} */}
|
||||
{/* <div>
|
||||
<button
|
||||
onClick={() => {
|
||||
localStorage.clear();
|
||||
router.push('/');
|
||||
onClick={async () => {
|
||||
await signOut();
|
||||
}}
|
||||
>
|
||||
<Image src={LogOut} alt={'Logo'} width={50} height={50} className={'mx-auto w-full md:w-[400px]'} />
|
||||
</button>
|
||||
</div>
|
||||
</div> */}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user