Higher Quality, Stronger Performance, Increased Stability, Better Developer Experience, discover everything we've shipped recently!

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 BETA 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

You need to be running Strapi 5.45.0 or higher to use this plugin.

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

Usage

1. Create a Better Auth config file

Create the Better Auth config file and add the following content.

// config/better-auth.ts
import { betterAuth } from "better-auth";
import { strapiAdapter } from '@strapi-community/plugin-better-auth';

const auth = () => betterAuth({
  database: strapiAdapter(),
  trustedOrigins: ['http://localhost:3000'],
  advanced: {
    database: {
      generateId: 'serial',
    },
  },
});

export default auth;

2. Generate the content types

Run the content type generation command to bootstrap the default content types.

npx auth@latest generate --config config/better-auth.ts

Tip: Every time you install a new Better Auth plugin, you have to run this command again.

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/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.

import { auth } from '@/lib/auth.ts';

// In a Strapi controller
export default {
  async customMethod(ctx) {
    // 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. Some plugins require you to run the generate CLI in order to make the required schema changes. To do that you have to manually specify the location of the Better Auth config file.

npx auth@latest generate --config config/better-auth.ts

Resources

Authors

License

See the LICENSE file for licensing information.

Install now

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

STATS

15 GitHub stars351 weekly downloads

Last updated

7 days ago

Strapi Version

5.45.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.