Setting cookies with custom plugin auth

Hi, I'm having a few issues with setting this up the way that I want it to but I may be approaching it from the wrong way. I am using another application that I want users to be able to single click sign on into but it is not an Idp or oauth provider. Instead it providers a token in the URL that I can decrypt.

The current imagined workflow is as follows:
  1. User clicks button
  2. Gets sent to /api/auth/test/sso
  3. Create a new plugin that creates a new auth endpoint to decrypt it, create a new user if doesnt exists.
  4. Redirect the user to the authenticated
    /dashboard
    page.
I have created a very simple poc which assumes that all the verification is complete.

import { BetterAuthPlugin, createAuthEndpoint } from "better-auth/plugins";

export const tutorcruncherPlugin = () => {
  return {
    id: "test",
    endpoints: {
      sso: createAuthEndpoint(
        "/test/sso",
        {
          method: "GET",
        },
        async (ctx) => {
          // Verify stuff - assume that it passes
          
          // this is a sample user that I added for testing.
          const user = await ctx.context.internalAdapter.findUserById(
            "Bvptc4rkZplmGgzFmx4DylRqVgJbjjIv",
          );

          const session = await ctx.context.internalAdapter.createSession(
            "Bvptc4rkZplmGgzFmx4DylRqVgJbjjIv",
            ctx.request,
          );

          ctx.context.setNewSession({
            session: session,
            user: user!,
          });

          return ctx.redirect("/dashboard");
        },
      ),
    },
  } satisfies BetterAuthPlugin;
};


The above code does not add the cookies into the browser and does not set them either. I have added the nextCookies plugin as well as the methods ctx.setCookie from https://www.better-auth.com/docs/concepts/hooks#cookies.

Any help would be greatly appreciated - losing my mind over this.
Better Auth Hooks let you customize BetterAuth's behavior
Was this page helpful?