-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from MaximeDan/MDA-Implement_Next_Auth
Implement Authentication with Next-Auth
- Loading branch information
Showing
35 changed files
with
7,455 additions
and
3,158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.