From c1c48aff417cf29b63030dbe5165b888e95f4106 Mon Sep 17 00:00:00 2001
From: rharkor
Date: Mon, 4 Dec 2023 14:47:05 +0100
Subject: [PATCH] feat: possibility to desactivate sign up
---
.env.example | 9 +++--
env.mjs | 5 +++
src/api/auth/mutations.ts | 3 ++
src/app/[lang]/(sys-auth)/sign-in/page.tsx | 41 ++++++++++++--------
src/app/[lang]/(sys-auth)/sign-up/layout.tsx | 6 +++
src/components/profile/profile-details.tsx | 2 +-
src/components/profile/update-account.tsx | 7 +++-
src/components/ui/file-upload.tsx | 8 ++--
src/langs/en.json | 3 +-
src/langs/fr.json | 3 +-
src/lib/auth/index.ts | 21 ++++++++--
11 files changed, 74 insertions(+), 34 deletions(-)
diff --git a/.env.example b/.env.example
index 816d4813..f71cc9a2 100644
--- a/.env.example
+++ b/.env.example
@@ -9,9 +9,9 @@ NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=PGJwbCE+WiJILGZ+K1ZqU24mIX5tVH0=
GITHUB_CLIENT_ID=secret
GITHUB_CLIENT_SECRET=secret
-AUTH_ADMIN_EMAIL=secret
-AUTH_ADMIN_PASSWORD=secret
-NEXT_PUBLIC_IS_DEMO=false
+AUTH_ADMIN_EMAIL=test@mail.com
+AUTH_ADMIN_PASSWORD=Azerty123!
+NEXT_PUBLIC_IS_DEMO=true
NEXT_PUBLIC_DEMO_EMAIL=test@mail.com
NEXT_PUBLIC_DEMO_PASSWORD=Azerty123!
REDIS_HOST=localhost
@@ -32,4 +32,5 @@ NEXT_PUBLIC_AWS_BUCKET_NAME=secret
AWS_ACCESS_KEY_ID=secret
AWS_SECRET_ACCESS_KEY=secret
NEXT_PUBLIC_AWS_ENDPOINT=secret
-ENABLE_S3_SERVICE=false
\ No newline at end of file
+ENABLE_S3_SERVICE=false
+ENABLE_REGISTRATION=true
\ No newline at end of file
diff --git a/env.mjs b/env.mjs
index e70fdcd3..633da545 100644
--- a/env.mjs
+++ b/env.mjs
@@ -52,6 +52,10 @@ export const env = createEnv({
.enum(["true", "false"])
.optional()
.transform((value) => value === "true"),
+ ENABLE_REGISTRATION: z
+ .enum(["true", "false"])
+ .optional()
+ .transform((value) => value === "true"),
},
client: {
NEXT_PUBLIC_IS_DEMO: z
@@ -101,6 +105,7 @@ export const env = createEnv({
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
NEXT_PUBLIC_AWS_ENDPOINT: process.env.NEXT_PUBLIC_AWS_ENDPOINT,
ENABLE_S3_SERVICE: process.env.ENABLE_S3_SERVICE,
+ ENABLE_REGISTRATION: process.env.ENABLE_REGISTRATION,
},
onValidationError: (error) => {
console.error(error)
diff --git a/src/api/auth/mutations.ts b/src/api/auth/mutations.ts
index 98fd7f33..0175d361 100644
--- a/src/api/auth/mutations.ts
+++ b/src/api/auth/mutations.ts
@@ -14,6 +14,9 @@ import { env } from "env.mjs"
export const register = async ({ input }: apiInputFromSchema) => {
const { email, password, username } = input
try {
+ if (env.ENABLE_REGISTRATION === false) {
+ return ApiError(throwableErrorsMessages.registrationDisabled)
+ }
const hashedPassword = await hash(password, 12)
const user = await prisma.user.create({
diff --git a/src/app/[lang]/(sys-auth)/sign-in/page.tsx b/src/app/[lang]/(sys-auth)/sign-in/page.tsx
index 554a9811..9ae18100 100644
--- a/src/app/[lang]/(sys-auth)/sign-in/page.tsx
+++ b/src/app/[lang]/(sys-auth)/sign-in/page.tsx
@@ -4,6 +4,7 @@ import { LoginUserAuthForm } from "@/components/auth/login-user-auth-form"
import { authRoutes } from "@/lib/auth/constants"
import { getDictionary } from "@/lib/langs"
import { cn } from "@/lib/utils"
+import { env } from "env.mjs"
import { Locale } from "i18n-config"
import PrivacyAcceptance from "../privacy-acceptance"
import Providers from "../providers"
@@ -21,14 +22,16 @@ export default async function SignInPage({
return (
-
+ {env.ENABLE_REGISTRATION && (
+
+ )}
@@ -38,15 +41,19 @@ export default async function SignInPage({
-
-
-
-
-
- {dictionary.auth.orContinueWith}
-
-
-
+ {env.ENABLE_REGISTRATION && (
+ <>
+
+
+
+
+
+ {dictionary.auth.orContinueWith}
+
+
+
+ >
+ )}
diff --git a/src/app/[lang]/(sys-auth)/sign-up/layout.tsx b/src/app/[lang]/(sys-auth)/sign-up/layout.tsx
index 5fa0b077..57f08019 100644
--- a/src/app/[lang]/(sys-auth)/sign-up/layout.tsx
+++ b/src/app/[lang]/(sys-auth)/sign-up/layout.tsx
@@ -1,4 +1,7 @@
import { Metadata } from "next"
+import { redirect } from "next/navigation"
+import { authRoutes } from "@/lib/auth/constants"
+import { env } from "env.mjs"
export const metadata: Metadata = {
title: "Sign-up",
@@ -6,5 +9,8 @@ export const metadata: Metadata = {
}
export default function SignupLayout({ children }: { children: React.ReactNode }) {
+ if (!env.ENABLE_REGISTRATION) {
+ redirect(authRoutes.signIn[0])
+ }
return <>{children}>
}
diff --git a/src/components/profile/profile-details.tsx b/src/components/profile/profile-details.tsx
index 96f57548..2c15a5c0 100644
--- a/src/components/profile/profile-details.tsx
+++ b/src/components/profile/profile-details.tsx
@@ -16,7 +16,7 @@ export default async function ProfileDetails({
{dictionary.profilePage.profileDetails.updateAccountDescription}
-
+
)
}
diff --git a/src/components/profile/update-account.tsx b/src/components/profile/update-account.tsx
index 0885f0e8..601abf17 100644
--- a/src/components/profile/update-account.tsx
+++ b/src/components/profile/update-account.tsx
@@ -23,10 +23,10 @@ type INonSensibleForm = z.infer>
export default function UpdateAccount({
dictionary,
- hasVerifiedEmail,
+ sessionHasVerifiedEmail,
}: {
dictionary: TDictionary
- hasVerifiedEmail: boolean
+ sessionHasVerifiedEmail: boolean
}) {
const router = useRouter()
const utils = trpc.useUtils()
@@ -34,6 +34,9 @@ export default function UpdateAccount({
const { update } = useSession()
const account = useAccount(dictionary)
+ const hasVerifiedEmail =
+ account.data?.user.emailVerified === undefined ? sessionHasVerifiedEmail : account.data.user.emailVerified
+
const updateUserMutation = trpc.me.updateUser.useMutation({
onError: (error) => handleMutationError(error, dictionary, router),
onSuccess: async (data) => {
diff --git a/src/components/ui/file-upload.tsx b/src/components/ui/file-upload.tsx
index 20494ea9..09d29fcc 100644
--- a/src/components/ui/file-upload.tsx
+++ b/src/components/ui/file-upload.tsx
@@ -50,11 +50,11 @@ export default function FileUpload({