Is it possible to have custom login logic? #1274

Closed
opened 2026-03-13 08:30:51 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @sangdth on GitHub (May 27, 2025).

Is this suited for github?

  • Yes, this is suited for github

In next-auth it provides me a authorize method to do my custom logic like this:

export const { handlers, signIn, signOut, auth } = NextAuth({
  providers: [
    Credentials({
      credentials: {
        email: {},
        password: {},
      },
      authorize: async (credentials) => {
        ...
        // logic to verify if the user exists
        user = await getUserFromDb(credentials.email, pwHash)
 
        if (!user) {
          // No user found, so this is their first attempt to login
          // Optionally, this is also the place you could do a user registration
          throw new Error("Invalid credentials.")
        }
 
        // return user object with their profile data
        return user
      },
    }),
  ],
})

Can we have similar function in better-auth?

Describe the solution you'd like

I would like to have a way to customise login logic, e.g. return user object.

Describe alternatives you've considered

I tried to find but unfortunately can not find out.

Additional context

No response

Originally created by @sangdth on GitHub (May 27, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. In next-auth it provides me a `authorize` method to do my custom logic like this: ``` export const { handlers, signIn, signOut, auth } = NextAuth({ providers: [ Credentials({ credentials: { email: {}, password: {}, }, authorize: async (credentials) => { ... // logic to verify if the user exists user = await getUserFromDb(credentials.email, pwHash) if (!user) { // No user found, so this is their first attempt to login // Optionally, this is also the place you could do a user registration throw new Error("Invalid credentials.") } // return user object with their profile data return user }, }), ], }) ``` Can we have similar function in better-auth? ### Describe the solution you'd like I would like to have a way to customise login logic, e.g. return `user` object. ### Describe alternatives you've considered I tried to find but unfortunately can not find out. ### Additional context _No response_
Author
Owner

@Kinfe123 commented on GitHub (May 27, 2025):

you can make sure to use the database hook for more customization - https://www.better-auth.com/docs/reference/options#databasehooks

@Kinfe123 commented on GitHub (May 27, 2025): you can make sure to use the database hook for more customization - https://www.better-auth.com/docs/reference/options#databasehooks
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1274