Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to best run redirect on TRPCError({ code: "UNAUTHORIZED" }); #1229

Closed
jpainam opened this issue Oct 21, 2024 · 1 comment
Closed

Where to best run redirect on TRPCError({ code: "UNAUTHORIZED" }); #1229

jpainam opened this issue Oct 21, 2024 · 1 comment

Comments

@jpainam
Copy link

jpainam commented Oct 21, 2024

throw new TRPCError({ code: "UNAUTHORIZED" });

Hi.
In api/trpc.ts, we have this

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

I'm wondering where we need to catch this error, to run a nextjs redirect to a custom login page.

I've added this in the nextjs/app/api/[...route]/route.ts

...
const handler = auth(async (req) => {
  try {
 
    const heads = new Headers(req.headers);
   // added custom headers
    const response = await fetchRequestHandler({
      endpoint: "/api/trpc",
      router: appRouter,
      req,
      createContext: () =>
        createTRPCContext({
          session: req.auth,
          headers: heads,
        }),
      onError({ error, path }) {
        console.error(`>>> tRPC Error on '${path}'`, error);
        // added this
        if (error.code == "UNAUTHORIZED") {
          redirect("/auth/login");
        }
      },
    });

    setCorsHeaders(response);
    return response;
  } catch (e) {
    console.error(">>> tRPC Error", e);
    const error = e as TRPCError;
     // added this
    if (error.code == "UNAUTHORIZED") {
      redirect("/auth/login");
    } else {
      throw e;
    }
  }
});

export { handler as GET, handler as POST };

But it doesn't work.

If i add redirect in api/trpc.ts, it shows this (even after adding redirect in the finally block).

o [TRPCError]: UNAUTHORIZED
    at /app/apps/nextjs/.next/server/chunks/6532.js:1:1126
    at r (/app/apps/nextjs/.next/server/chunks/2381.js:1:90577)
    at next (/app/apps/nextjs/.next/server/chunks/2381.js:1:90689)
    at /app/apps/nextjs/.next/server/chunks/6532.js:1:915
    at r (/app/apps/nextjs/.next/server/chunks/2381.js:1:90577)
[TRPC] student.count took 0ms to execute
    at e (/app/apps/nextjs/.next/server/chunks/2381.js:1:90919)
    at /app/apps/nextjs/.next/server/chunks/2381.js:4:1361
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'UNAUTHORIZED',
  [cause]: undefined
}
Error: NEXT_REDIRECT
    at c (/app/apps/nextjs/.next/server/chunks/3145.js:1:55251)
    ... 8 lines matching cause stack trace ...
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'INTERNAL_SERVER_ERROR',
  name: 'TRPCError',
  digest: '212336672',
  [cause]: Error: NEXT_REDIRECT
      at c (/app/apps/nextjs/.next/server/chunks/3145.js:1:55251)
      at l (/app/apps/nextjs/.next/server/chunks/3145.js:1:55466)
      at /app/apps/nextjs/.next/server/chunks/6532.js:1:1407
      at r (/app/apps/nextjs/.next/server/chunks/2381.js:1:90577)
      at next (/app/apps/nextjs/.next/server/chunks/2381.js:1:90689)
      at /app/apps/nextjs/.next/server/chunks/6532.js:1:915
      at r (/app/apps/nextjs/.next/server/chunks/2381.js:1:90577)
      at e (/app/apps/nextjs/.next/server/chunks/2381.js:1:90919)
      at /app/apps/nextjs/.next/server/chunks/2381.js:4:1361
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
    digest: 'NEXT_REDIRECT;replace;/auth/login;307;',
    mutableCookies: f {
      _parsed: [Map],
      _headers: Headers {
        'set-cookie': '__Secure-authjs.session-token=eyJhbGcih=/'
      }
    }
  }
}```
@jpainam
Copy link
Author

jpainam commented Oct 21, 2024

BYW, if I don't redirect. I get this

o [TRPCError]: UNAUTHORIZED
    at /app/apps/nextjs/.next/server/chunks/6532.js:1:1097
    at r (/app/apps/nextjs/.next/server/chunks/2381.js:1:90577)
    at next (/app/apps/nextjs/.next/server/chunks/2381.js:1:90689)
    at /app/apps/nextjs/.next/server/chunks/6532.js:1:904
    at r (/app/apps/nextjs/.next/server/chunks/2381.js:1:90577)
    at e (/app/apps/nextjs/.next/server/chunks/2381.js:1:90919)
    at /app/apps/nextjs/.next/server/chunks/2381.js:4:1361
    at async v (/app/apps/nextjs/.next/server/chunks/6921.js:2:48950) {
  code: 'UNAUTHORIZED',
  digest: '3447701378',
  [cause]: undefined
}

I'm using a custom login page.

@t3-oss t3-oss locked and limited conversation to collaborators Oct 21, 2024
@juliusmarminge juliusmarminge converted this issue into discussion #1230 Oct 21, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant