Jazon
Jazon
Explore posts from servers
TtRPC
Created by Jazon on 3/26/2024 in #❓-help
Has anyone passed supabase user to the tRPC context in Expo?
I am trying to pass the logged in user to the context of tRPC. To be able to use it in the routers and create a protected procedure. However, my application keeps on crashing as soon as I pass my user to the context (when a page is loaded where a call to the API is done). It looks like it has something to do with react-native-async-storage. Any ideas?
@react-native-async-storage/async-storage/lib/commonjs/AsyncStorage.js:63
return createPromise(() => window.localStorage.getItem(key), callback);
^
ReferenceError: window is not defined
@react-native-async-storage/async-storage/lib/commonjs/AsyncStorage.js:63
return createPromise(() => window.localStorage.getItem(key), callback);
^
ReferenceError: window is not defined
import { TRPCError, initTRPC } from "@trpc/server"
import { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch"
import { supabase } from "~/lib/supabase";

export async function createContext({ req }: FetchCreateContextFnOptions) {

const {
data: { user },
} = await supabase.auth.getUser();

return {
user
}
}

const t = initTRPC.context<typeof createContext>().create({
errorFormatter({ shape }) {
return shape
},
})

export const createTRPCRouter = t.router
export const publicProcedure = t.procedure

const enforceUserIsAuthed = t.middleware( async ({ ctx, next }) => {

if (!ctx.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

return next({
ctx: {
...ctx,
user: ctx.user
},
});
});

export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
import { TRPCError, initTRPC } from "@trpc/server"
import { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch"
import { supabase } from "~/lib/supabase";

export async function createContext({ req }: FetchCreateContextFnOptions) {

const {
data: { user },
} = await supabase.auth.getUser();

return {
user
}
}

const t = initTRPC.context<typeof createContext>().create({
errorFormatter({ shape }) {
return shape
},
})

export const createTRPCRouter = t.router
export const publicProcedure = t.procedure

const enforceUserIsAuthed = t.middleware( async ({ ctx, next }) => {

if (!ctx.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}

return next({
ctx: {
...ctx,
user: ctx.user
},
});
});

export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
2 replies