Skip to content

Commit

Permalink
Merge pull request #7 from MaximeDan/MDA-Implement_Next_Auth
Browse files Browse the repository at this point in the history
Implement Authentication with Next-Auth
  • Loading branch information
alexandre-kakal authored May 23, 2024
2 parents bd640d9 + 8c679d1 commit 398cfd6
Show file tree
Hide file tree
Showing 35 changed files with 7,455 additions and 3,158 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
NODE_ENV="development"
DATABASE_PASSWORD="toto123"
DATABASE_PASSWORD="toto123"
NEXTAUTH_SECRET=mysecret
9,656 changes: 6,881 additions & 2,775 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"framer-motion": "^11.2.4",
"lucide-react": "^0.378.0",
"next": "14.2.3",
"next-auth": "^4.24.7",
"react": "^18",
"react-day-picker": "^8.10.1",
"react-dom": "^18",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ CREATE TABLE "Role" (
-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"lastName" TEXT NOT NULL,
"name" TEXT,
"lastName" TEXT,
"email" TEXT NOT NULL,
"username" TEXT NOT NULL,
"password" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"isVerified" BOOLEAN NOT NULL DEFAULT false,
"dateOfBirth" TIMESTAMP(3) NOT NULL,
"dateOfBirth" TIMESTAMP(3),
"avatar" TEXT,
"experience" INTEGER NOT NULL DEFAULT 0,

Expand Down Expand Up @@ -78,9 +78,9 @@ CREATE TABLE "Journey" (
"physicalDifficulty" INTEGER NOT NULL,
"lastCompletion" TIMESTAMP(3),
"mobilityImpaired" TEXT NOT NULL,
"PartiallySighted" TEXT NOT NULL,
"PartiallyDeaf" TEXT NOT NULL,
"CognitivelyImpaired" TEXT NOT NULL,
"partiallySighted" TEXT NOT NULL,
"partiallyDeaf" TEXT NOT NULL,
"cognitivelyImpaired" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

Expand Down
3 changes: 0 additions & 3 deletions prisma/migrations/migration_lock.toml

This file was deleted.

6 changes: 3 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ model Role {

model User {
id Int @id @default(autoincrement())
name String
lastName String
name String?
lastName String?
email String @unique
username String
password String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
isVerified Boolean @default(false)
dateOfBirth DateTime
dateOfBirth DateTime?
avatar String?
experience Int @default(0)
userRoles UserRole[] // liste des rôles de l'utilisateur
Expand Down
5 changes: 0 additions & 5 deletions src/api/hello/route.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/app/(auth)/inscription/layout.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import Register from "@/components/form/Register";

const Inscription = () => {
const RegisterPage = () => {
return (
<main>
<h1>Inscription</h1>
Expand All @@ -10,4 +10,4 @@ const Inscription = () => {
);
};

export default Inscription;
export default RegisterPage;
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import Login from "@/components/form/Login";

const Connexion = () => {
const SignInPage = () => {

return (
<main>
<h1>Connexion</h1>
Expand All @@ -10,4 +11,4 @@ const Connexion = () => {
);
};

export default Connexion;
export default SignInPage;
6 changes: 6 additions & 0 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import NextAuth from "next-auth";
import { authOptions } from "@/lib/authOptions";

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };
26 changes: 26 additions & 0 deletions src/app/api/auth/register/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextRequest, NextResponse } from "next/server";
import { register } from "@/services/userService";
import {RegisterUser} from "@/types/register";

async function handler(req: NextRequest): Promise<NextResponse> {
const data = await req.json();

try {
const userData: RegisterUser = {
email: data.email,
password: data.password,
username: data.username,
name: data.name,
lastName: data.lastName,
dateOfBirth: data.dateOfBirth,
};

const newUser = await register(userData);
return NextResponse.json({ user: newUser }, { status: 201 });
} catch (error: any) {
console.log(error.message)
return NextResponse.json({ message: error.message }, { status: 500 });
}
}

export { handler as POST };
2 changes: 1 addition & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function RootLayout({
children: React.ReactNode;
}>) {
return (
<html lang="en">
<html lang="fr">
<body className={`${inter.className} min-h-screen`}>{children}</body>
</html>
);
Expand Down
121 changes: 66 additions & 55 deletions src/components/form/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,79 @@
"use client";
import React from "react";
import { loginSchema } from "@/validators/loginSchema";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Button } from "@/components/ui/button";
import {loginSchema} from "@/validators/loginSchema";
import {z} from "zod";
import {zodResolver} from "@hookform/resolvers/zod";
import {useForm} from "react-hook-form";
import {Button} from "@/components/ui/button";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import {Input} from "@/components/ui/input";
import {signIn} from "next-auth/react";

type LoginForm = z.infer<typeof loginSchema>;

const Login = () => {
const form = useForm<LoginForm>({
resolver: zodResolver(loginSchema),
defaultValues: {
email: "",
password: "",
},
});
const form = useForm<LoginForm>({
resolver: zodResolver(loginSchema),
defaultValues: {
email: "[email protected]",
password: "password",
},
});

const onSubmit = async (data: LoginForm) => {
console.log(data);
};
const onSubmit = async (data: LoginForm) => {
try {
await signIn("credentials", {
redirect: true,
identifier: data.email,
password: data.password,
callbackUrl: "/",
cache: "no-cache",
});
} catch (error: any) {
console.error(error.message);
}
};

return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Mot de passe</FormLabel>
<FormControl>
<Input type="password" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Se connecter</Button>
</form>
</Form>
);
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="email"
render={({field}) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage/>
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({field}) => (
<FormItem>
<FormLabel>Mot de passe</FormLabel>
<FormControl>
<Input type="password" {...field} />
</FormControl>
<FormMessage/>
</FormItem>
)}
/>
<Button type="submit">Se connecter</Button>
</form>
</Form>
);
};

export default Login;
Loading

0 comments on commit 398cfd6

Please sign in to comment.