add locale to trpc context (#897)
parent
7488f29dc9
commit
8cbf880af6
|
@ -1,3 +1,4 @@
|
||||||
|
import { Maybe } from "@trpc/server";
|
||||||
import parser from "accept-language-parser";
|
import parser from "accept-language-parser";
|
||||||
import { IncomingMessage } from "http";
|
import { IncomingMessage } from "http";
|
||||||
|
|
||||||
|
@ -6,9 +7,15 @@ import prisma from "@lib/prisma";
|
||||||
|
|
||||||
import { i18n } from "../../../next-i18next.config";
|
import { i18n } from "../../../next-i18next.config";
|
||||||
|
|
||||||
export const getOrSetUserLocaleFromHeaders = async (req: IncomingMessage) => {
|
export function getLocaleFromHeaders(req: IncomingMessage): string {
|
||||||
|
const preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"]) as Maybe<string>;
|
||||||
|
|
||||||
|
return preferredLocale ?? i18n.defaultLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getOrSetUserLocaleFromHeaders = async (req: IncomingMessage): Promise<string> => {
|
||||||
const session = await getSession({ req });
|
const session = await getSession({ req });
|
||||||
const preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"]);
|
const preferredLocale = parser.pick(i18n.locales, req.headers["accept-language"]) as Maybe<string>;
|
||||||
|
|
||||||
if (session?.user?.id) {
|
if (session?.user?.id) {
|
||||||
const user = await prisma.user.findUnique({
|
const user = await prisma.user.findUnique({
|
||||||
|
|
|
@ -2,12 +2,14 @@
|
||||||
import * as trpc from "@trpc/server";
|
import * as trpc from "@trpc/server";
|
||||||
import { Maybe } from "@trpc/server";
|
import { Maybe } from "@trpc/server";
|
||||||
import * as trpcNext from "@trpc/server/adapters/next";
|
import * as trpcNext from "@trpc/server/adapters/next";
|
||||||
|
import { NextApiRequest } from "next";
|
||||||
|
|
||||||
import { getSession, Session } from "@lib/auth";
|
import { getSession, Session } from "@lib/auth";
|
||||||
|
import { getLocaleFromHeaders } from "@lib/core/i18n/i18n.utils";
|
||||||
import prisma from "@lib/prisma";
|
import prisma from "@lib/prisma";
|
||||||
import { defaultAvatarSrc } from "@lib/profile";
|
import { defaultAvatarSrc } from "@lib/profile";
|
||||||
|
|
||||||
async function getUserFromSession(session: Maybe<Session>) {
|
async function getUserFromSession({ session, req }: { session: Maybe<Session>; req: NextApiRequest }) {
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -30,6 +32,7 @@ async function getUserFromSession(session: Maybe<Session>) {
|
||||||
createdDate: true,
|
createdDate: true,
|
||||||
hideBranding: true,
|
hideBranding: true,
|
||||||
avatar: true,
|
avatar: true,
|
||||||
|
locale: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -42,11 +45,14 @@ async function getUserFromSession(session: Maybe<Session>) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const avatar = user.avatar || defaultAvatarSrc({ email });
|
const avatar = user.avatar || defaultAvatarSrc({ email });
|
||||||
|
|
||||||
|
const locale = user.locale ?? getLocaleFromHeaders(req);
|
||||||
return {
|
return {
|
||||||
...user,
|
...user,
|
||||||
avatar,
|
avatar,
|
||||||
email,
|
email,
|
||||||
username,
|
username,
|
||||||
|
locale,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,10 +64,13 @@ export const createContext = async ({ req, res }: trpcNext.CreateNextContextOpti
|
||||||
// for API-response caching see https://trpc.io/docs/caching
|
// for API-response caching see https://trpc.io/docs/caching
|
||||||
const session = await getSession({ req });
|
const session = await getSession({ req });
|
||||||
|
|
||||||
|
const user = await getUserFromSession({ session, req });
|
||||||
|
const locale = user?.locale ?? getLocaleFromHeaders(req);
|
||||||
return {
|
return {
|
||||||
prisma,
|
prisma,
|
||||||
session,
|
session,
|
||||||
user: await getUserFromSession(session),
|
user,
|
||||||
|
locale,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue