
* Makes userRequired middleware * Prevent users from changing usernames to premium ones * refactor on zomars' branch (#801) * rename `profile` -> `mutation` * `createProtectedRouter()` helper * move profile mutation to `viewer.` * simplify checkUsername * Auto scrolls to error when there is one * Renames username helpers * Follows db convention Co-authored-by: Alex Johansson <alexander@n1s.se> Co-authored-by: Bailey Pumfleet <pumfleet@hey.com>
25 lines
561 B
TypeScript
25 lines
561 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) {
|
|
throw new trpc.TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
return next({
|
|
ctx: {
|
|
...ctx,
|
|
// infers that `user` is non-nullable to downstream procedures
|
|
user: ctx.user,
|
|
},
|
|
});
|
|
});
|
|
}
|