Skip to content

Commit

Permalink
added withauth to easier logout users
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Aug 25, 2023
1 parent 116c3b0 commit a095226
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 18 deletions.
27 changes: 27 additions & 0 deletions src/components/auth/withAuth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GetServerSideProps } from "next";
import { getSession } from "next-auth/react";

export function withAuth(gssp: GetServerSideProps): GetServerSideProps {
return async (context) => {
const { user } = (await getSession(context)) || {};

if (!user) {
return {
redirect: { statusCode: 302, destination: "/auth/login" },
};
}
// ssp (server side props)
const gsspData = await gssp(context);

if (!("props" in gsspData)) {
throw new Error("invalid getSSP result");
}

return {
props: {
...gsspData.props,
user,
},
};
};
}
7 changes: 4 additions & 3 deletions src/pages/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Controller from "./controller";
import Mail from "./mail";
import Notification from "./notification";
import { useTranslations } from "next-intl";
import { type GetStaticPropsContext } from "next";
import { GetServerSidePropsContext } from "next";
import { withAuth } from "~/components/auth/withAuth";

const AdminSettings = () => {
const router = useRouter();
Expand Down Expand Up @@ -74,7 +75,7 @@ const AdminSettings = () => {
AdminSettings.getLayout = function getLayout(page: ReactElement) {
return <LayoutAuthenticated>{page}</LayoutAuthenticated>;
};
export async function getStaticProps(context: GetStaticPropsContext) {
export const getServerSideProps = withAuth(async (context: GetServerSidePropsContext) => {
return {
props: {
// You can get the messages from anywhere you like. The recommended
Expand All @@ -84,5 +85,5 @@ export async function getStaticProps(context: GetStaticPropsContext) {
messages: (await import(`../../locales/${context.locale}/common.json`)).default,
},
};
}
});
export default AdminSettings;
7 changes: 4 additions & 3 deletions src/pages/central/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import cn from "classnames";
import NetworkHelpText from "~/components/modules/networkHelp";
import { InviteMemberByMail } from "~/components/modules/inviteMemberbyMail";
import { useTranslations } from "next-intl";
import { type GetStaticPropsContext } from "next/types";
import { GetServerSidePropsContext } from "next/types";
import NetworkDescription from "../../components/modules/networkDescription";
import NetworkName from "~/components/modules/networkName";
import { withAuth } from "~/components/auth/withAuth";

const CentralNetworkById = () => {
const t = useTranslations("networkById");
Expand Down Expand Up @@ -266,7 +267,7 @@ CentralNetworkById.getLayout = function getLayout(page: ReactElement) {
return <LayoutAuthenticated>{page}</LayoutAuthenticated>;
};

export async function getServerSideProps(context: GetStaticPropsContext) {
export const getServerSideProps = withAuth(async (context: GetServerSidePropsContext) => {
return {
props: {
// You can get the messages from anywhere you like. The recommended
Expand All @@ -276,5 +277,5 @@ export async function getServerSideProps(context: GetStaticPropsContext) {
messages: (await import(`../../locales/${context.locale}/common.json`)).default,
},
};
}
});
export default CentralNetworkById;
5 changes: 3 additions & 2 deletions src/pages/central/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CentralNetworkTable } from "../../components/modules/ztCentral/centralN
import { globalSiteTitle } from "~/utils/global";
import { useTranslations } from "next-intl";
import { type GetServerSidePropsContext } from "next";
import { withAuth } from "~/components/auth/withAuth";

const CentralNetworks: NextPageWithLayout = () => {
const t = useTranslations("networks");
Expand Down Expand Up @@ -100,7 +101,7 @@ const CentralNetworks: NextPageWithLayout = () => {
CentralNetworks.getLayout = function getLayout(page: ReactElement) {
return <LayoutAuthenticated>{page}</LayoutAuthenticated>;
};
export async function getServerSideProps(context: GetServerSidePropsContext) {
export const getServerSideProps = withAuth(async (context: GetServerSidePropsContext) => {
return {
props: {
// You can get the messages from anywhere you like. The recommended
Expand All @@ -110,5 +111,5 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
messages: (await import(`../../locales/${context.locale}/common.json`)).default,
},
};
}
});
export default CentralNetworks;
8 changes: 5 additions & 3 deletions src/pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Head from "next/head";
import type { ReactElement } from "react";
import { type ReactElement } from "react";
import { LayoutAuthenticated } from "~/components/layouts/layout";
import type { NextPageWithLayout } from "../_app";
import { globalSiteTitle } from "~/utils/global";
import { type GetServerSidePropsContext } from "next";
import { withAuth } from "~/components/auth/withAuth";

const Dashboard: NextPageWithLayout = () => {
const title = `${globalSiteTitle} - Dashboard`;

return (
<>
<Head>
Expand Down Expand Up @@ -57,7 +59,7 @@ Dashboard.getLayout = function getLayout(page: ReactElement) {
return <LayoutAuthenticated>{page}</LayoutAuthenticated>;
};

export async function getServerSideProps(context: GetServerSidePropsContext) {
export const getServerSideProps = withAuth(async (context: GetServerSidePropsContext) => {
return {
props: {
// You can get the messages from anywhere you like. The recommended
Expand All @@ -67,5 +69,5 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
messages: (await import(`../../locales/${context.locale}/common.json`)).default,
},
};
}
});
export default Dashboard;
7 changes: 4 additions & 3 deletions src/pages/network/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import cn from "classnames";
import NetworkHelpText from "~/components/modules/networkHelp";
import { InviteMemberByMail } from "~/components/modules/inviteMemberbyMail";
import { useTranslations } from "next-intl";
import { type GetStaticPropsContext } from "next/types";
import { GetServerSidePropsContext } from "next/types";
import NetworkName from "~/components/modules/networkName";
import NetworkDescription from "~/components/modules/networkDescription";
import { withAuth } from "~/components/auth/withAuth";
// import DebugMirror from "~/components/modules/debugController";

const NetworkById = () => {
Expand Down Expand Up @@ -299,7 +300,7 @@ NetworkById.getLayout = function getLayout(page: ReactElement) {
return <LayoutAuthenticated>{page}</LayoutAuthenticated>;
};

export async function getServerSideProps(context: GetStaticPropsContext) {
export const getServerSideProps = withAuth(async (context: GetServerSidePropsContext) => {
return {
props: {
// You can get the messages from anywhere you like. The recommended
Expand All @@ -309,5 +310,5 @@ export async function getServerSideProps(context: GetStaticPropsContext) {
messages: (await import(`../../locales/${context.locale}/common.json`)).default,
},
};
}
});
export default NetworkById;
5 changes: 3 additions & 2 deletions src/pages/network/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useTranslations } from "next-intl";
import { type GetServerSidePropsContext } from "next";
import toast from "react-hot-toast";
import { ErrorData } from "~/types/errorHandling";
import { withAuth } from "~/components/auth/withAuth";

const Networks: NextPageWithLayout = () => {
const t = useTranslations("networks");
Expand Down Expand Up @@ -120,7 +121,7 @@ const Networks: NextPageWithLayout = () => {
Networks.getLayout = function getLayout(page: ReactElement) {
return <LayoutAuthenticated>{page}</LayoutAuthenticated>;
};
export async function getServerSideProps(context: GetServerSidePropsContext) {
export const getServerSideProps = withAuth(async (context: GetServerSidePropsContext) => {
return {
props: {
// You can get the messages from anywhere you like. The recommended
Expand All @@ -130,5 +131,5 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
messages: (await import(`../../locales/${context.locale}/common.json`)).default,
},
};
}
});
export default Networks;
15 changes: 13 additions & 2 deletions src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { type User as IUser } from "@prisma/client";
declare module "next-auth" {
interface Session extends DefaultSession {
user: IUser;
error: string;
update: {
name?: string;
email?: string;
Expand Down Expand Up @@ -162,9 +163,19 @@ export const authOptions: NextAuthOptions = {
}
return token;
},
session: ({ session, token }) => {
session: async ({ session, token }) => {
if (!token.id) return null;

// Check the user exists in the database
const user = await prisma.user.findFirst({
where: { id: token.id },
});

if (!user) {
// If the user does not exist, return null to log them out
return null;
}

session.user = { ...token } as IUser;
return session;
},
Expand All @@ -177,7 +188,7 @@ export const authOptions: NextAuthOptions = {
},
},
pages: {
signIn: "/",
signIn: "/auth/login",
},
debug: false,
};
Expand Down

0 comments on commit a095226

Please sign in to comment.