Cannot access 'createTRPCRouter' before initialization
can anyone please help why this erorr is coming and how i can resolve it
https://github.com/pradeep800/to-do-with-t3
Error
ReferenceError: Cannot access 'createTRPCRouter' before initialization
at Module.createTRPCRouter (webpack-internal:///(api)/./src/server/api/trpc.ts:6:65)
at eval (webpack-internal:///(api)/./src/server/api/routers/sigin.ts:23:64)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
GitHub
GitHub - pradeep800/to-do-with-t3
Contribute to pradeep800/to-do-with-t3 development by creating an account on GitHub.
45 Replies
Hello, im not an expert but whats
export const mergeRouter = t.mergeRouters;
?before i was creating router i was using merge router which i am not using even if i remove it it will be fine
can u go to ur network tab and check ur payload when u make the request
i don't why but there are lot of undefine
seems like ur form is not sending correct data
in github you can see i am using hand written response
and there is no trpc or typescript error and t3 stack is type safe right?
seeing a lot of bad practices in ur code besides the error
why is signIn and login different routers?
ur treating routers as endpoints
will it make any differnce?
for the current error no... but best practice
it was working before but when i start coding middleware i don't what happen
can you tell me what should i do for best practice should i put them in one file
i think best practice would be to make an authRouter and put both functions inside it
i will do it thank you
can you give me reset of trpc.ts i am trying to finding it i am unable to find it main repo
i think there is something wrong in trpc.ts
just comment code below it
https://github.com/pradeep800/to-do-with-t3/blob/main/src/server/api/trpc.ts#L95
GitHub
to-do-with-t3/trpc.ts at main · pradeep800/to-do-with-t3
Contribute to pradeep800/to-do-with-t3 development by creating an account on GitHub.
now
working
there is something wrong in after 95 line
are you able to figure out what is wrong with these last line of code ?
can u uncomment all and comment the merge line
what do you mean by that
merge line
like comment line 130
export const mergeRouter = t.mergeRouters;
i already did
but it is influencing any another line
ig u can try uncommenting the middleware line by line
see where it breaks
const authMiddleware = t.middleware(({ ctx, next }) => {
// let JWTtoken = ctx.req.headers.authorization?.replace("bearer ", "");
// console.log(JWTtoken);
// let JWT_SECRET = process.env.JWT_SECRET as string;
// let userInfo;
// console.log("hello there");
// try {
// if (JWTtoken) {
// userInfo = jwt.verify(JWTtoken, JWT_SECRET);
// } else {
// throw Error("not present");
// }
// } catch (err) {
// throw new TRPCError({
// code: "UNAUTHORIZED",
// message: "not_authenticated",
// });
// }
// const data = jwtPayload.parse(userInfo);
return next({
ctx: {
id: "hello",
name: "nope",
},
});
});
still code is working
on commented line there is some error
comment these
const authMiddleware = t.middleware(({ ctx, next }) => {
// let JWTtoken = ctx.req.headers.authorization?.replace("bearer ", "");
// console.log(JWTtoken);
// let JWT_SECRET = process.env.JWT_SECRET as string;
let userInfo;
// console.log("hello there");
const JWTtoken = "nothing";
const JWT_SECRET = "noting";
try {
if (JWTtoken) {
userInfo = jwt.verify(JWTtoken, JWT_SECRET);
} else {
throw Error("not present");
}
} catch (err) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "not_authenticated",
});
}
i think that file cannot excess env files what do you think?
?
try doing it through env file
u might not be able to do at all then u have to define it as public variable but thats not recommended
not working
what public variable btw
Basic Features: Environment Variables | Next.js
Learn to add and access environment variables in your Next.js application.
im not sure if this is the way tho
its not running in browser
im lost wait for someone else to help
But jwt key in public env can be excessed by anyone and then there is no point of authentication
yeah thats why im confused
+ its not in browser anyway
Ok
If anyone sees this can you tell me why env is not working in the trpc file???
just tested it, it works fine for me?
i can even pull db string from env works just fine
even using process.
yeah same here
const data = jwtPayload.parse(userInfo);
console.log(data); this statemnt is not not working
whats .parse?
whats userinfo?
you can see in image
JwtPayload.parser(userInfo);
i am using email in jwt but i am asking for name jwt parser
that was problem hopefully let me check
use safeparse func instead
yeah it start working now thank for you help
👍
how can i close this qeustion?
right click on the left
just do close post dont worry about it too much