[GH-ISSUE #181] Need an option to require email verification before create session while in signup or login #8162

Closed
opened 2026-04-13 03:15:33 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @qasimali09 on GitHub (Oct 15, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/181

Originally created by @qasimali09 on GitHub (Oct 15, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/181
GiteaMirror added the locked label 2026-04-13 03:15:33 -05:00
Author
Owner

@Bekacru commented on GitHub (Oct 16, 2024):

You can do this using database hooks. Check if the user isn't verified and return false to not allow session creation.

https://www.better-auth.com/docs/concepts/database#database-hooks

<!-- gh-comment-id:2415783573 --> @Bekacru commented on GitHub (Oct 16, 2024): You can do this using database hooks. Check if the user isn't verified and return false to not allow session creation. https://www.better-auth.com/docs/concepts/database#database-hooks
Author
Owner

@PawelPotempa commented on GitHub (Oct 16, 2024):

Just to add onto that, as it's also a feature I'm interested in. Is that what you had in mind @Bekacru?

session: {
    create: {
      before: async (session) => {
          const user = await db.query.user.findFirst({
              where: (user, { eq }) => eq(user.id, session.userId)
          });
          if (!user?.emailVerified && user) {
              return false;
        }
     }
  }
},

If so - it does prevent session from being created, but at the same time it prevents the verification email from being sent.
{message: "Failed to create session"}

I've also tried sending the email manually instead, but got the exact same response.

if (!user?.emailVerified && user) {
          await auth.api.sendVerificationEmail({
              body: {
                  email: user.email,
              },
          });
          return false;
      }
<!-- gh-comment-id:2417977324 --> @PawelPotempa commented on GitHub (Oct 16, 2024): Just to add onto that, as it's also a feature I'm interested in. Is that what you had in mind @Bekacru? ``` session: { create: { before: async (session) => { const user = await db.query.user.findFirst({ where: (user, { eq }) => eq(user.id, session.userId) }); if (!user?.emailVerified && user) { return false; } } } }, ``` If so - it does prevent session from being created, but at the same time it prevents the verification email from being sent. `{message: "Failed to create session"}` I've also tried sending the email manually instead, but got the exact same response. ``` if (!user?.emailVerified && user) { await auth.api.sendVerificationEmail({ body: { email: user.email, }, }); return false; } ```
Author
Owner

@Bekacru commented on GitHub (Oct 17, 2024):

Good catch! For the next release, we should send the email verification before creating the session.

<!-- gh-comment-id:2418558181 --> @Bekacru commented on GitHub (Oct 17, 2024): Good catch! For the next release, we should send the email verification before creating the session.
Author
Owner

@PawelPotempa commented on GitHub (Oct 17, 2024):

I saw that it has been fixed in the 0.4.12-beta.6, yet one issue still persists. The api still throws an error, despite successfully sending the verification email. I've tried catching it or throwing my own error instead of returning false, but with nothing major to show for it. Moreover, it seems counterintuitive for the API to return a 400 status code when the process appears to have executed correctly.

I think this might call for a more robust implementation, perhaps a config option to the betterAuth init function, such as session: {requireEmailVerification:true} would make sense?

<!-- gh-comment-id:2419977587 --> @PawelPotempa commented on GitHub (Oct 17, 2024): I saw that it has been fixed in the 0.4.12-beta.6, yet one issue still persists. The api still throws an error, despite successfully sending the verification email. I've tried catching it or throwing my own error instead of returning false, but with nothing major to show for it. Moreover, it seems counterintuitive for the API to return a 400 status code when the process appears to have executed correctly. I think this might call for a more robust implementation, perhaps a config option to the betterAuth init function, such as `session: {requireEmailVerification:true}` would make sense?
Author
Owner

@PawelPotempa commented on GitHub (Oct 19, 2024):

I think this feature broke again with one of the releases. On @latest it seems to ignore the requireEmailVerification: true option - no type errors, but signing up with credentials does create a session again.

<!-- gh-comment-id:2423945426 --> @PawelPotempa commented on GitHub (Oct 19, 2024): I think this feature broke again with one of the releases. On @latest it seems to ignore the `requireEmailVerification: true` option - no type errors, but signing up with credentials does create a session again.
Author
Owner

@Bekacru commented on GitHub (Oct 19, 2024):

I think this feature broke again with one of the releases. On @latest it seems to ignore the requireEmailVerification: true option - no type errors, but signing up with credentials does create a session again.

It seems like that was moved to autoSignIn: false, but requireEmailVerification should still enforce it as well. Will be corrected in the next release!

<!-- gh-comment-id:2423962983 --> @Bekacru commented on GitHub (Oct 19, 2024): > I think this feature broke again with one of the releases. On @latest it seems to ignore the `requireEmailVerification: true` option - no type errors, but signing up with credentials does create a session again. It seems like that was moved to `autoSignIn: false`, but `requireEmailVerification` should still enforce it as well. Will be corrected in the next release!
Author
Owner

@platform-kit commented on GitHub (Dec 30, 2024):

@Bekacru why are features being renamed? Is this project unstable?

<!-- gh-comment-id:2565968413 --> @platform-kit commented on GitHub (Dec 30, 2024): @Bekacru why are features being renamed? Is this project unstable?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8162