mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-05-13 17:21:53 +00:00
Add protected pages, optimise Prisma instances
Took 38 minutes
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { CardBody, Card, Image, Flex, Box } from "@chakra-ui/react";
|
||||
import { Card, Flex, Box } from "@chakra-ui/react";
|
||||
import Carousel from "./Carousel";
|
||||
|
||||
export default function CardUser(props) {
|
||||
|
||||
@@ -9,19 +9,16 @@ import {
|
||||
Badge,
|
||||
Text,
|
||||
} from "@chakra-ui/react";
|
||||
// Here we have used react-icons package for the icons
|
||||
|
||||
import {
|
||||
BiLeftArrowAlt,
|
||||
BiRightArrowAlt,
|
||||
BiHeart,
|
||||
// RxCross1,
|
||||
} from "react-icons/bi";
|
||||
|
||||
import { RxCross1 } from "react-icons/rx";
|
||||
// And react-slick as our Carousel Lib
|
||||
import Slider from "react-slick";
|
||||
|
||||
// Settings for the slider
|
||||
const settings = {
|
||||
dots: true,
|
||||
arrows: false,
|
||||
@@ -36,8 +33,6 @@ const settings = {
|
||||
|
||||
export default function Carousel(props) {
|
||||
const { borderRadiusImg, user } = props;
|
||||
// As we have used custom buttons, we need a reference variable to
|
||||
// change the state
|
||||
|
||||
// C'est l'utilisateur qui est login
|
||||
const actualUser = {
|
||||
@@ -54,8 +49,6 @@ export default function Carousel(props) {
|
||||
const heightPhoto = "75vh";
|
||||
const heightText = "25vh";
|
||||
|
||||
// These are the breakpoints which changes the position of the
|
||||
// buttons as the screen size changes
|
||||
const top = useBreakpointValue({ base: "90%", md: "50%" });
|
||||
const side = useBreakpointValue({ base: "30%", md: "10px" });
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import {PrismaClient} from '@prisma/client';
|
||||
|
||||
const prismaClient = new PrismaClient();
|
||||
export default prismaClient;
|
||||
@@ -1,28 +1,25 @@
|
||||
import NextAuth from "next-auth"
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import {PrismaClient} from '@prisma/client';
|
||||
import NextAuth from 'next-auth';
|
||||
import CredentialsProvider from 'next-auth/providers/credentials';
|
||||
import {NextApiRequest, NextApiResponse} from 'next';
|
||||
import {LoginData} from '@/models/form/LoginData';
|
||||
import {isSamePassword} from '@/lib/PasswordTools';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
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",
|
||||
name: 'Credentials',
|
||||
credentials: {
|
||||
email: { label: "Email", type: "text", placeholder: "adress@email.com" },
|
||||
password: { label: "Password", type: "password" }
|
||||
email: {label: 'Email', type: 'text', placeholder: 'adress@email.com'},
|
||||
password: {label: 'Password', type: 'password'},
|
||||
},
|
||||
async authorize(credentials) {
|
||||
const {email, password} = credentials as LoginData;
|
||||
if (!email || !password) return null;
|
||||
|
||||
// Appel à la base de donnée
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {email}
|
||||
});
|
||||
const user = await getUserByEmail(email);
|
||||
|
||||
// Vérification de la connexion
|
||||
if (user && await isSamePassword(password, user.password)) {
|
||||
@@ -31,23 +28,26 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
return null;
|
||||
},
|
||||
})
|
||||
}),
|
||||
];
|
||||
|
||||
return await NextAuth(req, res, {
|
||||
providers,
|
||||
session: {strategy: "jwt",},
|
||||
session: {strategy: 'jwt'},
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
callbacks: {
|
||||
async session({ session, token }: { session: any; token: any }) {
|
||||
const user = await prisma.user.findFirst({
|
||||
where: { email: session.user.email }
|
||||
});
|
||||
async session({session, token}: { session: any; token: any }) {
|
||||
session.token = token.sub;
|
||||
session.user = await getUserByEmail(session.user.email);
|
||||
|
||||
session.token = token.sub
|
||||
session.user = user
|
||||
return session
|
||||
return session;
|
||||
},
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
async function getUserByEmail(email: string) {
|
||||
return prismaClient.user.findUnique({
|
||||
where: {email},
|
||||
});
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
|
||||
type Data = {
|
||||
name: string
|
||||
}
|
||||
|
||||
export default function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<Data>
|
||||
) {
|
||||
res.status(200).json({ name: 'John Doe' })
|
||||
}
|
||||
+11
-10
@@ -1,19 +1,20 @@
|
||||
import type {NextApiRequest, NextApiResponse} from 'next';
|
||||
import CRUD from '@/utils/CRUD';
|
||||
import {CreateUserQuery} from '@/models/api/user';
|
||||
import {PrismaClient} from '@prisma/client';
|
||||
import {LoginData} from '@/models/form/LoginData';
|
||||
import {RegisterData} from '@/models/form/RegisterData';
|
||||
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);
|
||||
case CRUD.CREATE:
|
||||
return createUser(req, res);
|
||||
case CRUD.READ:
|
||||
return readUser(req, res);
|
||||
default:
|
||||
return help(res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +22,7 @@ function help(res: NextApiResponse) {
|
||||
res.status(400).send({message: 'error'}); // TODO add help message
|
||||
}
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
const prisma = prismaClient;
|
||||
|
||||
async function createUser(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {email, password, firstName, lastName} = req.body as CreateUserQuery;
|
||||
@@ -29,7 +30,7 @@ async function createUser(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!email || !password || !firstName || !lastName)
|
||||
return res.status(400).send({message: req.body});
|
||||
|
||||
const hashedPassword = await hashPassword(password)
|
||||
const hashedPassword = await hashPassword(password);
|
||||
|
||||
const newUser = await prisma.user.create({
|
||||
data: {...req.body, password: hashedPassword},
|
||||
@@ -39,9 +40,9 @@ async function createUser(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
async function readUser(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {id} = req.query as {id: string}
|
||||
const {id, email} = req.query as { id: string, email: string };
|
||||
|
||||
const user = (req.query.id)
|
||||
const user = (id)
|
||||
? await prisma.user.findUnique({where: {id}})
|
||||
: await prisma.user.findMany()
|
||||
;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import {Text} from '@chakra-ui/react';
|
||||
import {useSession} from 'next-auth/react';
|
||||
import {useRouter} from 'next/router';
|
||||
|
||||
export default function Dashboard() {
|
||||
const router = useRouter();
|
||||
const {data: session, status} = useSession();
|
||||
if (status === "unauthenticated") router.push("/login");
|
||||
|
||||
return (
|
||||
<Text>Dashboard de {session.user.firstName} {session.user.lastName}</Text>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user