-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivo
committed
Apr 20, 2023
1 parent
0fccc0a
commit d3a9d1b
Showing
24 changed files
with
891 additions
and
72 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
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,7 @@ | ||
# Environment variables declared in this file are automatically made available to Prisma. | ||
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema | ||
|
||
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. | ||
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings | ||
|
||
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/visualdynamics" |
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,23 @@ | ||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
enum Role { | ||
ADMIN | ||
USER | ||
} | ||
|
||
model User { | ||
id String @id @default(cuid()) | ||
email String @unique | ||
username String @unique | ||
password String | ||
name String? | ||
active Boolean @default(false) | ||
role Role @default(USER) | ||
} |
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,8 @@ | ||
// i18next.d.ts | ||
import "i18next"; | ||
|
||
declare module "i18next" { | ||
interface CustomTypeOptions { | ||
returnNull: false; | ||
} | ||
} |
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,17 @@ | ||
import { User as PUser } from "@prisma/client"; | ||
import NextAuth, { DefaultSession } from "next-auth"; | ||
|
||
declare module "next-auth" { | ||
interface Session extends DefaultSession { | ||
user: Omit<PUser, "password">; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface User extends Omit<PUser, "password"> {} | ||
} | ||
|
||
declare module "next-auth/jwt" { | ||
/** Returned by the `jwt` callback and `getToken`, when using JWT sessions */ | ||
interface JWT { | ||
user?: Omit<PUser, "password">; | ||
} | ||
} |
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,110 @@ | ||
import { SubmitHandler, useForm } from "react-hook-form"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { Lock, LogIn, LogOut, User } from "lucide-react"; | ||
import { signIn, signOut, useSession } from "next-auth/react"; | ||
import { useTranslation } from "next-i18next"; | ||
|
||
import { | ||
AuthFormSchema, | ||
AuthFormSchemaType | ||
} from "@app/schemas/components/auth/auth.zod"; | ||
|
||
import { Button } from "../Button"; | ||
import { TextButton } from "../Button/Text"; | ||
import { Input } from "../Input"; | ||
|
||
export function Auth() { | ||
const { data: session, status } = useSession(); | ||
const { | ||
register, | ||
reset, | ||
formState: { errors }, | ||
handleSubmit | ||
} = useForm<AuthFormSchemaType>({ | ||
resolver: zodResolver(AuthFormSchema) | ||
}); | ||
const { t } = useTranslation(["navigation"]); | ||
|
||
const handleAuth: SubmitHandler<AuthFormSchemaType> = async ({ | ||
identifier, | ||
password | ||
}) => { | ||
signIn("credentials", { | ||
identifier, | ||
password, | ||
redirect: false | ||
}).then(() => reset()); | ||
}; | ||
|
||
if (status === "authenticated") { | ||
return ( | ||
<div className="flex w-full gap-x-2"> | ||
<User className="stroke-primary-950 my-auto h-7 w-7" /> | ||
<div className="flex flex-col gap-y-1 flex-1"> | ||
<div className="flex gap-x-1"> | ||
<p className="uppercase -mb-2 text-sm text-primary-950"> | ||
{session.user.username} | ||
</p> | ||
<small className="text-xs -mb-2 text-zinc-500"> | ||
{t(`navigation:auth.role.${session.user.role.toLowerCase()}`)} | ||
</small> | ||
</div> | ||
<small className="text-xs text-zinc-500">{session.user.email}</small> | ||
</div> | ||
<TextButton | ||
LeftIcon={LogOut} | ||
onClick={() => | ||
signOut({ | ||
redirect: false | ||
}) | ||
} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
if (status === "loading") { | ||
return <h1>loading</h1>; | ||
} | ||
|
||
return ( | ||
<> | ||
<div className="flex gap-x-1.5 w-full -mb-4 text-primary-950 transition-all duration-500"> | ||
<LogIn className="h-5 w-5 my-auto stroke-[3]" /> | ||
<h3 className="font-medium text-xl">{t("navigation:forms.submit")}</h3> | ||
</div> | ||
<form | ||
className="flex flex-col w-full gap-y-2.5" | ||
onSubmit={handleSubmit(handleAuth)} | ||
> | ||
<Input | ||
error={errors.identifier} | ||
label={t("navigation:forms.identifier.title")} | ||
placeholder={t("navigation:forms.identifier.placeholder")} | ||
{...register("identifier")} | ||
/> | ||
<Input | ||
error={errors.password} | ||
label={t("navigation:forms.password.title")} | ||
placeholder={t("navigation:forms.password.placeholder")} | ||
type="password" | ||
{...register("password")} | ||
/> | ||
<Button | ||
LeftIcon={LogIn} | ||
type="submit" | ||
> | ||
{t("navigation:forms.submit")} | ||
</Button> | ||
<TextButton | ||
className="text-sm" | ||
iconClassName="h-4 w-4" | ||
LeftIcon={Lock} | ||
type="button" | ||
> | ||
{t("navigation:forms.lost-password")} | ||
</TextButton> | ||
</form> | ||
</> | ||
); | ||
} |
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.