Better AuthBA
Better Authβ€’8mo ago
Kyosuke

How to set ExpireAt for user

Hello Better Auth community πŸ˜†

Im trying to add a datetime to set an expire date for certain users.
I was able to cancel a creation of new session in sign-in but having trouble canceling an update of a session.

This is my codes

const prisma = new PrismaClient();
export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "mysql",
  }),
  user: {
    additionalFields: {
      expiresAt: {
        type: "date",
        required: false,
        input: false,
      },
    },
  },
  databaseHooks: {
    session: {
      create: {
        before: async (session) => {
          const user = await prisma.user.findUnique({
            where: { id: session.userId },
          });
          if (user.expiresAt && user.expiresAt < new Date()) {
            throw new APIError("FORBIDDEN", {
              message: "η„‘εŠΉγͺγ‚’γ‚«γ‚¦γƒ³γƒˆγ§γ™",
            });
          }
        },
      },
      update: {
        before: async (session, context) => {
          console.log("session", session);
          console.log("context", context);
          return true;
        },
      },
    },
  },
  emailAndPassword: {
    enabled: true,
  },
  plugins: [admin()],
  session: {
    // ζœ€εΎŒγ«δ½Ώη”¨γ—γ¦γ‹γ‚‰24ζ™‚ι–“γ§γ‚»γƒƒγ‚·γƒ§γƒ³γŒεˆ‡γ‚Œγ‚‹
    // expiresIn: 60 * 60 * 24, // 1 day
    // updateAge: 60 * 60, // 1 hour

    expiresIn: 60 * 60, // 1 day
    updateAge: 10, // 1 hour
  },
});


  • session in update.before doesnt contain a session.id
  • context in update.before is null
Are there are any other ways I can achive my goal?
Was this page helpful?