Skip to content

bug: Forward next-auth cookie session from request headers to trpc procedure #1259

@jpainam

Description

@jpainam

Provide environment information

N/A

Describe the bug

A common use case, is to call an api route, after a trpc complete, for example to send an email. API route is usually protected with

// api/emails/notify.ts

const session = await auth()
if(!session){
    return new Response("Unauthorized", {status:401})
}

Because request headers are not available in the trpc procedure, i'm unable to forward them to the fetch call. Is there a better way around it.

USE case

create: protectedProcedure
    .input(CreatePostSchema)
    .mutation(({ ctx, input }) => {
      const v = ctx.db.insert(Post).values(input);
      // WANT TO DO THIS.
      fetch(`/api/emails/notify`, {
           // i would like to forward the user session
     });
     return v;
    }),

Another solution will be to send this into a redis queue, and process these ops later. But that's not the point. I would like to make api calls from trpc procedure. This is just a simplified use case.

Link to reproduction

https://github.com/t3-oss/create-t3-turbo

To reproduce

N/A

Additional information

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions