cal/server/createRouter.ts
Omar López 0861d7cc61
Ends the war between tRPC and next-i18next (#939)
* Ends the war between tRPC and next-i18next

* Locale fixes

* Linting

* Linting

* trpc i18n (not working) (#942)

* simplify i18n handler and remove redundant(?) fn check

* split up viewer to a "logged in only" and "public"

* wip -- skip first render

Co-authored-by: Omar López <zomars@me.com>

* Linting

* I18n fixes

* We don't need serverSideTranslations in every page anymore

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Alex Johansson <alexander@n1s.se>
2021-10-14 13:57:49 +03:00

27 lines
622 B
TypeScript

import * as trpc from "@trpc/server";
import { Context } from "./createContext";
/**
* Helper function to create a router with context
*/
export function createRouter() {
return trpc.router<Context>();
}
export function createProtectedRouter() {
return createRouter().middleware(({ ctx, next }) => {
if (!ctx.user || !ctx.session) {
throw new trpc.TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
...ctx,
// infers that `user` and `session` are non-nullable to downstream procedures
session: ctx.session,
user: ctx.user,
},
});
});
}