mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-07-09 15:08:06 +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";
|
import Carousel from "./Carousel";
|
||||||
|
|
||||||
export default function CardUser(props) {
|
export default function CardUser(props) {
|
||||||
|
|||||||
@@ -9,19 +9,16 @@ import {
|
|||||||
Badge,
|
Badge,
|
||||||
Text,
|
Text,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
// Here we have used react-icons package for the icons
|
|
||||||
import {
|
import {
|
||||||
BiLeftArrowAlt,
|
BiLeftArrowAlt,
|
||||||
BiRightArrowAlt,
|
BiRightArrowAlt,
|
||||||
BiHeart,
|
BiHeart,
|
||||||
// RxCross1,
|
|
||||||
} from "react-icons/bi";
|
} from "react-icons/bi";
|
||||||
|
|
||||||
import { RxCross1 } from "react-icons/rx";
|
import { RxCross1 } from "react-icons/rx";
|
||||||
// And react-slick as our Carousel Lib
|
|
||||||
import Slider from "react-slick";
|
import Slider from "react-slick";
|
||||||
|
|
||||||
// Settings for the slider
|
|
||||||
const settings = {
|
const settings = {
|
||||||
dots: true,
|
dots: true,
|
||||||
arrows: false,
|
arrows: false,
|
||||||
@@ -36,8 +33,6 @@ const settings = {
|
|||||||
|
|
||||||
export default function Carousel(props) {
|
export default function Carousel(props) {
|
||||||
const { borderRadiusImg, user } = 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
|
// C'est l'utilisateur qui est login
|
||||||
const actualUser = {
|
const actualUser = {
|
||||||
@@ -54,8 +49,6 @@ export default function Carousel(props) {
|
|||||||
const heightPhoto = "75vh";
|
const heightPhoto = "75vh";
|
||||||
const heightText = "25vh";
|
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 top = useBreakpointValue({ base: "90%", md: "50%" });
|
||||||
const side = useBreakpointValue({ base: "30%", md: "10px" });
|
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 NextAuth from 'next-auth';
|
||||||
import CredentialsProvider from "next-auth/providers/credentials";
|
import CredentialsProvider from 'next-auth/providers/credentials';
|
||||||
import {PrismaClient} from '@prisma/client';
|
|
||||||
import {NextApiRequest, NextApiResponse} from 'next';
|
import {NextApiRequest, NextApiResponse} from 'next';
|
||||||
import {LoginData} from '@/models/form/LoginData';
|
import {LoginData} from '@/models/form/LoginData';
|
||||||
import {isSamePassword} from '@/lib/PasswordTools';
|
import {isSamePassword} from '@/lib/PasswordTools';
|
||||||
|
import prismaClient from '@/lib/prismaClient';
|
||||||
const prisma = new PrismaClient();
|
import {dmmf} from '.prisma/client/edge';
|
||||||
|
|
||||||
export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const providers = [
|
const providers = [
|
||||||
CredentialsProvider({
|
CredentialsProvider({
|
||||||
name: "Credentials",
|
name: 'Credentials',
|
||||||
credentials: {
|
credentials: {
|
||||||
email: { label: "Email", type: "text", placeholder: "adress@email.com" },
|
email: {label: 'Email', type: 'text', placeholder: 'adress@email.com'},
|
||||||
password: { label: "Password", type: "password" }
|
password: {label: 'Password', type: 'password'},
|
||||||
},
|
},
|
||||||
async authorize(credentials) {
|
async authorize(credentials) {
|
||||||
const {email, password} = credentials as LoginData;
|
const {email, password} = credentials as LoginData;
|
||||||
if (!email || !password) return null;
|
if (!email || !password) return null;
|
||||||
|
|
||||||
// Appel à la base de donnée
|
// Appel à la base de donnée
|
||||||
const user = await prisma.user.findUnique({
|
const user = await getUserByEmail(email);
|
||||||
where: {email}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Vérification de la connexion
|
// Vérification de la connexion
|
||||||
if (user && await isSamePassword(password, user.password)) {
|
if (user && await isSamePassword(password, user.password)) {
|
||||||
@@ -31,23 +28,26 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
return await NextAuth(req, res, {
|
return await NextAuth(req, res, {
|
||||||
providers,
|
providers,
|
||||||
session: {strategy: "jwt",},
|
session: {strategy: 'jwt'},
|
||||||
secret: process.env.NEXTAUTH_SECRET,
|
secret: process.env.NEXTAUTH_SECRET,
|
||||||
callbacks: {
|
callbacks: {
|
||||||
async session({ session, token }: { session: any; token: any }) {
|
async session({session, token}: { session: any; token: any }) {
|
||||||
const user = await prisma.user.findFirst({
|
session.token = token.sub;
|
||||||
where: { email: session.user.email }
|
session.user = await getUserByEmail(session.user.email);
|
||||||
});
|
|
||||||
|
|
||||||
session.token = token.sub
|
return session;
|
||||||
session.user = user
|
|
||||||
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 type {NextApiRequest, NextApiResponse} from 'next';
|
||||||
import CRUD from '@/utils/CRUD';
|
import CRUD from '@/utils/CRUD';
|
||||||
import {CreateUserQuery} from '@/models/api/user';
|
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 {hashPassword} from '@/lib/PasswordTools';
|
||||||
|
import prismaClient from '@/lib/prismaClient';
|
||||||
|
|
||||||
export default function handler(
|
export default function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse,
|
res: NextApiResponse,
|
||||||
) {
|
) {
|
||||||
switch (req.method) {
|
switch (req.method) {
|
||||||
case CRUD.CREATE: return createUser(req, res);
|
case CRUD.CREATE:
|
||||||
case CRUD.READ: return readUser(req, res);
|
return createUser(req, res);
|
||||||
default: return help(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
|
res.status(400).send({message: 'error'}); // TODO add help message
|
||||||
}
|
}
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = prismaClient;
|
||||||
|
|
||||||
async function createUser(req: NextApiRequest, res: NextApiResponse) {
|
async function createUser(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const {email, password, firstName, lastName} = req.body as CreateUserQuery;
|
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)
|
if (!email || !password || !firstName || !lastName)
|
||||||
return res.status(400).send({message: req.body});
|
return res.status(400).send({message: req.body});
|
||||||
|
|
||||||
const hashedPassword = await hashPassword(password)
|
const hashedPassword = await hashPassword(password);
|
||||||
|
|
||||||
const newUser = await prisma.user.create({
|
const newUser = await prisma.user.create({
|
||||||
data: {...req.body, password: hashedPassword},
|
data: {...req.body, password: hashedPassword},
|
||||||
@@ -39,9 +40,9 @@ async function createUser(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function readUser(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.findUnique({where: {id}})
|
||||||
: await prisma.user.findMany()
|
: 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