✨ We just launched fimo.ai - an AI Website Builder to create websites in minutes - Try it now

Strapi plugin logo for Better Auth

Better Auth

A Better Auth database adapter that uses Strapi as the database backend

Strapi + Better Auth

A Better Auth database adapter that uses Strapi as the database backend. This package allows you to integrate Better Auth's authentication system with your Strapi application seamlessly.

!CAUTION
This plugin is in ALPHA state. It is by no means considered stable and should not be used in production. If you want to contribute to it's development, please contact any of the maintainers.

Features

  • ✅ Full Better Auth database adapter implementation
  • ✅ Uses Strapi's document service for data management
  • ✅ Supports all Better Auth core features (users, sessions, accounts, verification)
  • ✅ All Better Auth endpoints available through Strapi API
  • ✅ Works with Strapi v5+

Installation

npm install @strapi-community/plugin-better-auth
# or
yarn add @strapi-community/plugin-better-auth
# or
pnpm add @strapi-community/plugin-better-auth

Usage

1. Install the plugin in your Strapi application

Add the plugin to your Strapi plugins configuration:

// config/plugins.ts
export default {
  'better-auth': {
    enabled: true,
  }
}

2. Customize the Better Auth options

The Better Auth options can be customized through the plugin config:

// config/plugins.ts
export default {
  'better-auth': {
    enabled: true,
    config: {
      betterAuthOptions: {
        emailAndPassword: {
          enabled: true,
          requireEmailVerification: true,
        },
        socialProviders: {
          github: {
            clientId: process.env.GITHUB_CLIENT_ID!,
            clientSecret: process.env.GITHUB_CLIENT_SECRET!,
          },
          google: {
            clientId: process.env.GOOGLE_CLIENT_ID!,
            clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
          },
        },
        session: {
          expiresIn: 60 * 60 * 24 * 7, // 7 days
        },
      },
    },
  },
};

3. Client implementation

Call the Better Auth API from your front-end:

// In your React/Vue/Svelte app
import { createAuthClient } from 'better-auth/react'; // or /vue, /svelte

const authClient = createAuthClient({
  baseURL: 'http://localhost:1337/api/better-auth',
});

// Sign up
await authClient.signUp.email({
  email: 'user@example.com',
  password: 'password123',
  name: 'John Doe',
});

4. Server side authentication

Use the Better Auth session to authenticate your users in custom Strapi controllers.

// In a Strapi controller
export default {
  async customMethod(ctx) {
    // Access the Better Auth instance
    // @ts-expect-error - Accessing custom property
    const auth = strapi.betterAuth;
    
    // Use Better Auth API methods
    const session = await auth.api.getSession({
      headers: ctx.request.headers,
    });
    
    if (session) {
      // User is authenticated
      console.log('User:', session.user);
    }
  }
}

Supported plugins

Any Better Auth plugin may be used with Strapi + Better Auth, but only a subset are considered supported for the integration. Strapi + Better Auth works out of the box for these without any required schema changes.

PluginTested
Two Factor
Username
Anonymous
Email OTP
Generic OAuth
JWT
Magic Link
One Tap
Passkey
Phone Number

Resources

Authors

License

See the LICENSE file for licensing information.

Install now

npm install @strapi-community/plugin-better-auth

STATS

7 GitHub stars4 weekly downloads

Last updated

43 days ago

Strapi Version

5.0.0 and above

Author

github profile image for Boaz Poolman
Boaz Poolman

Useful links

Create your own plugin

Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.