Skip to content

Commit

Permalink
feat: possibility to desactivate sign up
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Dec 4, 2023
1 parent f35001e commit c1c48af
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 34 deletions.
9 changes: 5 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=[email protected]
AUTH_ADMIN_PASSWORD=Azerty123!
NEXT_PUBLIC_IS_DEMO=true
NEXT_PUBLIC_DEMO_EMAIL=[email protected]
NEXT_PUBLIC_DEMO_PASSWORD=Azerty123!
REDIS_HOST=localhost
Expand All @@ -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
ENABLE_S3_SERVICE=false
ENABLE_REGISTRATION=true
5 changes: 5 additions & 0 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/api/auth/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { env } from "env.mjs"
export const register = async ({ input }: apiInputFromSchema<typeof signUpSchema>) => {
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({
Expand Down
41 changes: 24 additions & 17 deletions src/app/[lang]/(sys-auth)/sign-in/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,14 +22,16 @@ export default async function SignInPage({

return (
<main className="container relative m-auto grid min-h-screen flex-1 flex-col items-center justify-center px-2 lg:max-w-none lg:grid-cols-2 lg:px-0">
<Button
as={Link}
href={authRoutes.signUp[0]}
className={cn("absolute right-4 top-4 md:right-8 md:top-8")}
variant="ghost"
>
{dictionary.toSignUp}
</Button>
{env.ENABLE_REGISTRATION && (
<Button
as={Link}
href={authRoutes.signUp[0]}
className={cn("absolute right-4 top-4 md:right-8 md:top-8")}
variant="ghost"
>
{dictionary.toSignUp}
</Button>
)}
<div className="hidden h-full bg-muted lg:block"></div>
<div className="lg:p-8">
<div className="mx-auto flex w-full flex-col justify-center space-y-6 sm:w-[350px]">
Expand All @@ -38,15 +41,19 @@ export default async function SignInPage({
</div>
<div className="grid gap-6">
<LoginUserAuthForm searchParams={searchParams} dictionary={dictionary} />
<div className="relative">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-background px-2 text-muted-foreground">{dictionary.auth.orContinueWith}</span>
</div>
</div>
<Providers dictionary={dictionary} searchParams={searchParams} />
{env.ENABLE_REGISTRATION && (
<>
<div className="relative">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-background px-2 text-muted-foreground">{dictionary.auth.orContinueWith}</span>
</div>
</div>
<Providers dictionary={dictionary} searchParams={searchParams} />
</>
)}
</div>
<PrivacyAcceptance dictionary={dictionary} />
</div>
Expand Down
6 changes: 6 additions & 0 deletions src/app/[lang]/(sys-auth)/sign-up/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
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",
description: "Create an account",
}

export default function SignupLayout({ children }: { children: React.ReactNode }) {
if (!env.ENABLE_REGISTRATION) {
redirect(authRoutes.signIn[0])
}
return <>{children}</>
}
2 changes: 1 addition & 1 deletion src/components/profile/profile-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default async function ProfileDetails({
{dictionary.profilePage.profileDetails.updateAccountDescription}
</p>
</header>
<UpdateAccount dictionary={dictionary} hasVerifiedEmail={hasVerifiedEmail} />
<UpdateAccount dictionary={dictionary} sessionHasVerifiedEmail={hasVerifiedEmail} />
</section>
)
}
7 changes: 5 additions & 2 deletions src/components/profile/update-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ type INonSensibleForm = z.infer<ReturnType<typeof nonSensibleSchema>>

export default function UpdateAccount({
dictionary,
hasVerifiedEmail,
sessionHasVerifiedEmail,
}: {
dictionary: TDictionary
hasVerifiedEmail: boolean
sessionHasVerifiedEmail: boolean
}) {
const router = useRouter()
const utils = trpc.useUtils()

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) => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/ui/file-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export default function FileUpload({
<div
{...getRootProps()}
className={cn(
"flex h-[250px] cursor-pointer flex-col items-center justify-center gap-4 rounded-medium border border-dashed border-transparent bg-muted/50 p-2 px-6 text-foreground transition-all",
"flex h-[250px] cursor-pointer flex-col items-center justify-center gap-4 rounded-medium border border-dashed border-transparent bg-muted/20 p-2 px-6 text-foreground transition-all",
{
"hover:border-primary hover:bg-muted/70 focus:border-primary focus:bg-muted/70": !disabled,
"border-primary bg-muted/70": isDragAccept,
"border-danger bg-danger/70": isDragReject,
"hover:border-primary hover:bg-muted/40 focus:border-primary focus:bg-muted/40": !disabled,
"border-primary bg-muted/50": isDragAccept,
"border-danger bg-danger/40": isDragReject,
},
className
)}
Expand Down
3 changes: 2 additions & 1 deletion src/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
"emailServiceDisabled": "Email service disabled.",
"unavailableWithOAuth": "This feature is not available with OAuth providers.",
"s3ServiceDisabled": "S3 service disabled.",
"noFileSelected": "No file selected."
"noFileSelected": "No file selected.",
"registrationDisabled": "Registration disabled."
},
"email": "Email",
"emailPlaceholder": "[email protected]",
Expand Down
3 changes: 2 additions & 1 deletion src/langs/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
"emailServiceDisabled": "Le service d'email est désactivé.",
"unavailableWithOAuth": "Cette fonctionnalité n'est pas disponible avec ce type de connexion.",
"s3ServiceDisabled": "Le service S3 est désactivé.",
"noFileSelected": "Aucun fichier sélectionné."
"noFileSelected": "Aucun fichier sélectionné.",
"registrationDisabled": "L'inscription est désactivée."
},
"email": "Email",
"emailPlaceholder": "[email protected]",
Expand Down
21 changes: 17 additions & 4 deletions src/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,17 @@ export const providers: Provider[] = [
}
},
}),
GithubProvider({
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
}),
]

if (env.ENABLE_REGISTRATION) {
providers.push(
GithubProvider({
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
})
)
}

export const providersByName: {
[key: string]: Provider | undefined
} = providers.reduce<{
Expand Down Expand Up @@ -236,4 +241,12 @@ export const nextAuthOptions: NextAuthOptions = {
session: {
strategy: "jwt", //? Strategy database could not work with credentials provider for security reasons
},
logger: {
error(code, metadata) {
logger.error("error", code, metadata)
},
warn(code) {
logger.warn("warn", code)
},
},
}

0 comments on commit c1c48af

Please sign in to comment.