mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-21 02:07:23 +02:00
♻️ Switch to next-auth for handling authentication (#899)
This commit is contained in:
parent
5f9e428432
commit
6fa66da681
65 changed files with 1514 additions and 1586 deletions
|
@ -1,15 +1,9 @@
|
|||
import type { IncomingMessage, ServerResponse } from "http";
|
||||
import { getIronSession } from "iron-session";
|
||||
import { withIronSessionApiRoute, withIronSessionSsr } from "iron-session/next";
|
||||
import {
|
||||
GetServerSideProps,
|
||||
GetServerSidePropsContext,
|
||||
NextApiHandler,
|
||||
} from "next";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { NextApiHandler } from "next";
|
||||
|
||||
import { sessionConfig } from "../session-config";
|
||||
import { createSSGHelperFromContext } from "../trpc/context";
|
||||
import { composeGetServerSideProps } from "./utils";
|
||||
|
||||
export function withSessionRoute(handler: NextApiHandler) {
|
||||
return withIronSessionApiRoute(handler, sessionConfig);
|
||||
|
@ -21,60 +15,3 @@ export const getSession = async (
|
|||
) => {
|
||||
return getIronSession(req, res, sessionConfig);
|
||||
};
|
||||
|
||||
export function withSessionSsr(
|
||||
handler: GetServerSideProps | GetServerSideProps[],
|
||||
options?: {
|
||||
onPrefetch?: (
|
||||
ssg: Awaited<ReturnType<typeof createSSGHelperFromContext>>,
|
||||
ctx: GetServerSidePropsContext,
|
||||
) => Promise<void>;
|
||||
},
|
||||
): GetServerSideProps {
|
||||
const composedHandler = Array.isArray(handler)
|
||||
? composeGetServerSideProps(...handler)
|
||||
: handler;
|
||||
|
||||
return withIronSessionSsr(async (ctx) => {
|
||||
const ssg = await createSSGHelperFromContext(ctx);
|
||||
await ssg.whoami.get.prefetch(); // always prefetch user
|
||||
if (options?.onPrefetch) {
|
||||
try {
|
||||
await options.onPrefetch(ssg, ctx);
|
||||
} catch {
|
||||
return {
|
||||
notFound: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
const res = await composedHandler(ctx);
|
||||
if ("props" in res) {
|
||||
return {
|
||||
...res,
|
||||
props: {
|
||||
...res.props,
|
||||
trpcState: ssg.dehydrate(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return res;
|
||||
}, sessionConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Require user to be logged in
|
||||
* @returns
|
||||
*/
|
||||
export const withAuth: GetServerSideProps = async (ctx) => {
|
||||
if (!ctx.req.session.user || ctx.req.session.user.isGuest) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: "/login",
|
||||
permanent: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return { props: {} };
|
||||
};
|
||||
|
|
|
@ -1,12 +1,20 @@
|
|||
import { EmailClient } from "@rallly/emails";
|
||||
import * as trpcNext from "@trpc/server/adapters/next";
|
||||
|
||||
import { createContext } from "../../trpc/context";
|
||||
import { appRouter } from "../../trpc/routers";
|
||||
import { withSessionRoute } from "../session";
|
||||
|
||||
export const trpcNextApiHandler = withSessionRoute(
|
||||
trpcNext.createNextApiHandler({
|
||||
export interface TRPCContext {
|
||||
user: { id: string; isGuest: boolean };
|
||||
emailClient: EmailClient;
|
||||
isSelfHosted: boolean;
|
||||
isEmailBlocked?: (email: string) => boolean;
|
||||
}
|
||||
|
||||
export const trpcNextApiHandler = (context: TRPCContext) => {
|
||||
return trpcNext.createNextApiHandler({
|
||||
router: appRouter,
|
||||
createContext,
|
||||
}),
|
||||
);
|
||||
createContext: async () => {
|
||||
return context;
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue