mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-07-09 15:08:06 +00:00
Edit Crud, add loaders during authentication
Took 2 hours 49 minutes
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import NextCrud, {PrismaAdapter} from '@premieroctet/next-crud';
|
||||
import prismaClient from '@/lib/prismaClient';
|
||||
import {NextApiRequest, NextApiResponse} from 'next';
|
||||
import {hashPassword} from '@/lib/PasswordTools';
|
||||
|
||||
type CreateUserQuery = {
|
||||
email: string
|
||||
password: string
|
||||
firstName: string
|
||||
lastName: string
|
||||
}
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
|
||||
const nextCrudHandler = await NextCrud({
|
||||
adapter: new PrismaAdapter({prismaClient: prismaClient}),
|
||||
});
|
||||
|
||||
// Hash le mot de passe quand on crée un utilisateur
|
||||
if (req.url === "/api/users" && req.method === "POST") {
|
||||
const {email, password, firstName, lastName} = req.body as CreateUserQuery;
|
||||
|
||||
if (!email || !password || !firstName || !lastName)
|
||||
return res.status(400).send({message: req.body});
|
||||
|
||||
const hashedPassword: string = await hashPassword(password);
|
||||
req.body = {...req.body, password: hashedPassword}
|
||||
}
|
||||
|
||||
return nextCrudHandler(req, res);
|
||||
}
|
||||
@@ -3,30 +3,37 @@ import CredentialsProvider from 'next-auth/providers/credentials';
|
||||
import {NextApiRequest, NextApiResponse} from 'next';
|
||||
import {LoginData} from '@/models/form/LoginData';
|
||||
import {isSamePassword} from '@/lib/PasswordTools';
|
||||
import {User} from '@prisma/client';
|
||||
import prismaClient from '@/lib/prismaClient';
|
||||
import {dmmf} from '.prisma/client/edge';
|
||||
|
||||
export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
||||
const providers = [
|
||||
CredentialsProvider({
|
||||
name: 'Credentials',
|
||||
credentials: {
|
||||
email: {label: 'Email', type: 'text', placeholder: 'adress@email.com'},
|
||||
password: {label: 'Password', type: 'password'},
|
||||
email: {
|
||||
label: 'Email',
|
||||
type: 'text',
|
||||
placeholder: 'adresse@email.com'
|
||||
},
|
||||
password: {
|
||||
label: 'Password',
|
||||
type: 'password',
|
||||
placeholder: 'mot de passe',
|
||||
},
|
||||
},
|
||||
async authorize(credentials) {
|
||||
async authorize(credentials: unknown) {
|
||||
const {email, password} = credentials as LoginData;
|
||||
if (!email || !password) return null;
|
||||
|
||||
// Appel à la base de donnée
|
||||
const user = await getUserByEmail(email);
|
||||
if (!user) return null;
|
||||
|
||||
// Vérification de la connexion
|
||||
if (user && await isSamePassword(password, user.password)) {
|
||||
return user;
|
||||
}
|
||||
|
||||
return null;
|
||||
const passwordIsValid = await isSamePassword(password, user.password);
|
||||
if (passwordIsValid) return user;
|
||||
else return null;
|
||||
},
|
||||
}),
|
||||
];
|
||||
@@ -46,8 +53,8 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
||||
});
|
||||
}
|
||||
|
||||
async function getUserByEmail(email: string) {
|
||||
async function getUserByEmail(email: string): Promise<null | User> {
|
||||
return prismaClient.user.findUnique({
|
||||
where: {email},
|
||||
where: {email}
|
||||
});
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
import type {NextApiRequest, NextApiResponse} from 'next';
|
||||
import CRUD from '@/models/api/CRUD';
|
||||
import {CreateUserQuery} from '@/models/api/UserQuery';
|
||||
import {hashPassword} from '@/lib/PasswordTools';
|
||||
import prismaClient from '@/lib/prismaClient';
|
||||
|
||||
export default function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse,
|
||||
) {
|
||||
switch (req.method) {
|
||||
case CRUD.CREATE:
|
||||
return createUser(req, res);
|
||||
case CRUD.READ:
|
||||
return readUser(req, res);
|
||||
default:
|
||||
return help(res);
|
||||
}
|
||||
}
|
||||
|
||||
function help(res: NextApiResponse) {
|
||||
res.status(400).send({message: 'error'}); // TODO add help message
|
||||
}
|
||||
|
||||
const prisma = prismaClient;
|
||||
|
||||
async function createUser(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {email, password, firstName, lastName} = req.body as CreateUserQuery;
|
||||
|
||||
if (!email || !password || !firstName || !lastName)
|
||||
return res.status(400).send({message: req.body});
|
||||
|
||||
const hashedPassword = await hashPassword(password);
|
||||
|
||||
const newUser = await prisma.user.create({
|
||||
data: {...req.body, password: hashedPassword},
|
||||
});
|
||||
|
||||
return res.status(201).send({message: 'createUser', newUser});
|
||||
}
|
||||
|
||||
async function readUser(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {id, email} = req.query as { id: string, email: string };
|
||||
|
||||
const user = (id)
|
||||
? await prisma.user.findUnique({where: {id}})
|
||||
: await prisma.user.findMany()
|
||||
;
|
||||
|
||||
return res.status(200).send({message: 'readUser', user});
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import {Grid, GridItem, Text, Box} from '@chakra-ui/react';
|
||||
import {useSession} from 'next-auth/react';
|
||||
import {useRouter} from 'next/router';
|
||||
|
||||
import type {Session} from '@/models/data_models/Session';
|
||||
import type {Session} from '@/models/auth/Session';
|
||||
import CardUser from '../components/layout/dashboard/card_user/CardUser';
|
||||
import LeftPanel from '../components/layout/dashboard/left_panel/LeftPanel';
|
||||
import Head from 'next/head';
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ export default function Home() {
|
||||
<>
|
||||
<Head><title>{websiteName}</title></Head>
|
||||
|
||||
<Navbar/>
|
||||
<Navbar variant={"fixed"}/>
|
||||
<HeroBanner/>
|
||||
<HomePresentation/>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user