-
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.
feat(auth): login with credentials & getCurrentUser
- Loading branch information
1 parent
3a235c8
commit 5fd5f55
Showing
15 changed files
with
323 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { authOptions } from "@/pages/api/auth/[...nextauth]"; | ||
import { getServerSession } from "next-auth/next"; | ||
import prisma from "@/app/libs/prismadb"; | ||
|
||
export async function getSession() { | ||
return await getServerSession(authOptions); | ||
} | ||
|
||
export default async function getCurrentUser() { | ||
try { | ||
const session = await getSession(); | ||
|
||
if (!session?.user?.email) { | ||
return null; | ||
} | ||
|
||
const currentUser = await prisma.user.findUnique({ | ||
where: { | ||
email: session.user.email as string, | ||
}, | ||
}); | ||
|
||
if (!currentUser) { | ||
return null; | ||
} | ||
|
||
return { | ||
...currentUser, | ||
createdAt: currentUser.createdAt.toISOString(), | ||
updatedAt: currentUser.updatedAt.toISOString(), | ||
emailVerified: currentUser.emailVerified?.toISOString() || null, | ||
}; | ||
} catch (error) { | ||
console.log("error: ", error); | ||
return null; | ||
} | ||
} |
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 @@ | ||
export { default as getCurrentUser } from "./getCurrentUser"; |
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,117 @@ | ||
"use client"; | ||
|
||
import axios from "axios"; | ||
import React from "react"; | ||
import { FieldValues, SubmitHandler, useForm } from "react-hook-form"; | ||
import { FcGoogle } from "react-icons/fc"; | ||
import { AiFillGithub } from "react-icons/ai"; | ||
import { ROUTE } from "@/app/api/constant"; | ||
import { useRegisterModal, useLoginModal } from "@/app/hooks"; | ||
import { toast } from "react-hot-toast"; | ||
import Modal from "."; | ||
import Heading from "../heading"; | ||
import Input from "../inputs"; | ||
import InputPassword from "../inputs/password"; | ||
import Button from "../button"; | ||
import { signIn } from "next-auth/react"; | ||
import { useRouter } from "next/navigation"; | ||
const LoginModal = () => { | ||
const router = useRouter(); | ||
const registerModal = useRegisterModal(); | ||
const loginModal = useLoginModal(); | ||
const [isLoading, setIsLoading] = React.useState(false); | ||
|
||
const { | ||
register, | ||
handleSubmit, | ||
setError, | ||
formState: { errors }, | ||
} = useForm<FieldValues>({ | ||
defaultValues: { | ||
email: "", | ||
password: "", | ||
}, | ||
}); | ||
|
||
const onSubmit: SubmitHandler<FieldValues> = (data) => { | ||
setIsLoading(true); | ||
|
||
signIn("credentials", { | ||
...data, | ||
redirect: false, | ||
}).then((callback) => { | ||
setIsLoading(false); | ||
|
||
if (callback?.ok) { | ||
toast.success("Logged in successfully!"); | ||
router.refresh(); | ||
loginModal.onClose(); | ||
} | ||
if (callback?.error) { | ||
setError("password", { | ||
message: callback.error, | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
disabled={isLoading} | ||
isLoading={isLoading} | ||
isOpen={loginModal.isOpen} | ||
title="Login" | ||
actionLabel="Continue" | ||
onClose={loginModal.onClose} | ||
onSubmit={handleSubmit(onSubmit)} | ||
body={ | ||
<div className="flex flex-col gap-4"> | ||
<Heading title="Welcome back" subTitle="Login to your account!" /> | ||
<Input | ||
id="email" | ||
label="Email" | ||
disabled={isLoading} | ||
register={register} | ||
errors={errors} | ||
required | ||
type="email" | ||
/> | ||
<InputPassword | ||
id="password" | ||
label="Password" | ||
disabled={isLoading} | ||
register={register} | ||
errors={errors} | ||
required | ||
/> | ||
</div> | ||
} | ||
footer={ | ||
<div className="flex flex-col gap-4 mt-3"> | ||
<hr /> | ||
<Button outline onClick={() => {}} icon={FcGoogle}> | ||
Continue with Google | ||
</Button> | ||
<Button outline onClick={() => {}} icon={AiFillGithub}> | ||
Continue with Github | ||
</Button> | ||
<div className="text-neutral-500 text-center mt-4 font-light"> | ||
<div className="justify-center flex flex-row items-center gap-2"> | ||
<div>Already have an account?</div> | ||
<div | ||
className="text-neutral-800 cursor-pointer hover:underline" | ||
onClick={() => { | ||
registerModal.onClose(); | ||
}} | ||
> | ||
Login | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
export default LoginModal; |
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
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
Oops, something went wrong.