mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-06 09:59:00 +02:00
🐛 Handle missing session in trpc api
If the user doesn’t have a session then we redirect to login screen from the server
This commit is contained in:
parent
2b211217a1
commit
d3c721a916
2 changed files with 13 additions and 14 deletions
|
@ -4,7 +4,6 @@ import "../../style.css";
|
||||||
import { Toaster } from "@rallly/ui/toaster";
|
import { Toaster } from "@rallly/ui/toaster";
|
||||||
import type { Viewport } from "next";
|
import type { Viewport } from "next";
|
||||||
import { Inter } from "next/font/google";
|
import { Inter } from "next/font/google";
|
||||||
import { redirect } from "next/navigation";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import { TimeZoneChangeDetector } from "@/app/[locale]/timezone-change-detector";
|
import { TimeZoneChangeDetector } from "@/app/[locale]/timezone-change-detector";
|
||||||
|
@ -33,10 +32,6 @@ export default async function Root({
|
||||||
}) {
|
}) {
|
||||||
const session = await getServerSession();
|
const session = await getServerSession();
|
||||||
|
|
||||||
if (!session) {
|
|
||||||
redirect("/login");
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<html lang={locale} className={inter.className}>
|
<html lang={locale} className={inter.className}>
|
||||||
<body>
|
<body>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { createServerSideHelpers } from "@trpc/react-query/server";
|
import { createServerSideHelpers } from "@trpc/react-query/server";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
import superjson from "superjson";
|
import superjson from "superjson";
|
||||||
|
|
||||||
import { getServerSession } from "@/auth";
|
import { getServerSession } from "@/auth";
|
||||||
|
@ -18,7 +19,7 @@ async function createContext(): Promise<TRPCContext> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = {
|
return {
|
||||||
user: {
|
user: {
|
||||||
id: session.user.id,
|
id: session.user.id,
|
||||||
isGuest: session.user.email === null,
|
isGuest: session.user.email === null,
|
||||||
|
@ -27,8 +28,6 @@ async function createContext(): Promise<TRPCContext> {
|
||||||
getEmailClient: () => getEmailClient(session.user.locale ?? undefined),
|
getEmailClient: () => getEmailClient(session.user.locale ?? undefined),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,9 +35,14 @@ async function createContext(): Promise<TRPCContext> {
|
||||||
* @description use this function to call tRPC procedures server-side and hydrate `react-query`'s cache
|
* @description use this function to call tRPC procedures server-side and hydrate `react-query`'s cache
|
||||||
* @see https://trpc.io/docs/client/nextjs/server-side-helpers#1-internal-router
|
* @see https://trpc.io/docs/client/nextjs/server-side-helpers#1-internal-router
|
||||||
*/
|
*/
|
||||||
export const createSSRHelper = async () =>
|
export const createSSRHelper = async () => {
|
||||||
createServerSideHelpers({
|
try {
|
||||||
|
return createServerSideHelpers({
|
||||||
router: appRouter,
|
router: appRouter,
|
||||||
ctx: await createContext(),
|
ctx: await createContext(),
|
||||||
transformer: superjson,
|
transformer: superjson,
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
return redirect("/login");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue