How to force a user logout or delete a user based on login info?

So, this is my current setup for Discord based OAuth:
export const auth = betterAuth({
  ...  
  socialProviders: {
    discord: {
      clientId: process.env.DISCORD_CLIENT_ID as string,
      clientSecret: process.env.DISCORD_CLIENT_SECRET as string,
      scope: ["identify", "guilds.members.read"],
    },
  },
  hooks: {
    after: createAuthMiddleware(async (ctx) => {
      // if (ctx.path.startsWith("/sign-up")) {
      const newSession = ctx.context.newSession;
      if (newSession) {
        try {
          const accessToken = await auth.api.getAccessToken({
            body: {
              providerId: "discord", // or any other provider id
              userId: newSession.user.id,
            },
            headers: ctx.headers,
          });
          console.log(accessToken);
          if (!accessToken.accessToken) {
            ctx.redirect("/error");
            console.log("");
          }
          const token = accessToken.accessToken ? accessToken.accessToken : "";
          const response = await isUserInGuild(
            token,
            process.env.DISCORD_GUILD_ID as string,
          );
          // Check if the user is in the guild
          console.log("User in guild:", response);
          if (!isUserInGuild) {
            // If not, redirect to an error page or show a message
            ctx.redirect("/error?error=acces_denied");
            console.log("User is not in the guild");
          } else {
            // If the user is in the guild, proceed with the sign-up
            ctx.redirect("/");
            console.log("User is in the guild");
          }
        } catch (error) {
          console.log("ERROR: ", error);
        }
      }
      // }
    }),
  },
});


I assume that I am doing something wrong with the ctx.redirect here, and should be throwing some sort of error that forces the user to be logged out. I don't think my Google-fu is working for me, so what should I be doing here?
Was this page helpful?