Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove signup page and button if registration is closed. #122

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/components/layouts/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { globalSiteTitle } from "~/utils/global";
import Link from "next/link";
import { useRouter } from "next/router";
import { User } from "@prisma/client";
import { api } from "~/utils/api";

type TUser = {
user: User;
Expand All @@ -18,6 +19,9 @@ interface Props {

export const LayoutPublic = ({ children }: Props): JSX.Element => {
const router = useRouter();
const { data: options, isLoading: loadingRegistration } =
api.settings.registrationAllowed.useQuery();

const currentPath = router.pathname;
return (
<div className="outer-content">
Expand All @@ -27,14 +31,16 @@ export const LayoutPublic = ({ children }: Props): JSX.Element => {
</div>

<div className="m-3 mx-0 flex w-10/12 justify-end">
<Link
href={
currentPath.includes("/auth/register") ? "/auth/login" : "/auth/register"
}
className="btn"
>
{currentPath === "/auth/register" ? "Login" : "Sign Up"}
</Link>
{options?.enableRegistration && !loadingRegistration ? (
<Link
href={
currentPath.includes("/auth/register") ? "/auth/login" : "/auth/register"
}
className="btn"
>
{currentPath === "/auth/register" ? "Login" : "Sign Up"}
</Link>
) : null}
</div>
</div>
{children}
Expand Down
21 changes: 21 additions & 0 deletions src/pages/auth/register/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Head from "next/head";
import React, { ReactElement } from "react";
import { LayoutPublic } from "~/components/layouts/layout";
import RegisterForm from "~/components/modules/registerForm";
import { prisma } from "~/server/db";
import { globalSiteTitle } from "~/utils/global";

const Register = () => {
Expand Down Expand Up @@ -50,9 +51,29 @@ interface Props {
export const getServerSideProps: GetServerSideProps<Props> = async (
context: GetServerSidePropsContext,
) => {
const options = await prisma.globalOptions.findFirst({
where: {
id: 1,
},
select: {
enableRegistration: true,
},
});
// easy check to see if the invite probably is a jwt token
const isJwt = !!context.query?.invite && context.query?.invite.length > 50;

const session = await getSession(context);
const messages = (await import(`~/locales/${context.locale}/common.json`)).default;

// redirect user to 404 if registration is disabled
if (!options?.enableRegistration && !isJwt) {
return {
redirect: {
destination: "/404",
permanent: false,
},
};
}
if (!session || !("user" in session)) {
return { props: { messages } };
}
Expand Down
12 changes: 11 additions & 1 deletion src/server/api/routers/settingsRouter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import { createTRPCRouter, protectedProcedure, publicProcedure } from "~/server/api/trpc";

export const settingsRouter = createTRPCRouter({
registrationAllowed: publicProcedure.query(async ({ ctx }) => {
return await ctx.prisma.globalOptions.findFirst({
where: {
id: 1,
},
select: {
enableRegistration: true,
},
});
}),
// Set global options
getAllOptions: protectedProcedure.query(async ({ ctx }) => {
return await ctx.prisma.globalOptions.findFirst({
Expand Down
Loading