Angular Better Auth Wrapper

0.10.3 · active · verified Wed Apr 22

ngx-better-auth is an Angular 20+ wrapper library designed to integrate with the Better Auth authentication system. It provides a reactive approach to session management, leveraging Angular's signal primitives for real-time session status (`session`, `isLoggedIn`). The library offers a clean dependency injection setup using observables and includes modern Angular guards (`canActivate`, `hasRole`) for route protection, ensuring a streamlined development experience for authentication flows. Currently at version `0.10.3`, it maintains a regular release cadence with frequent bug fixes and feature enhancements, closely tracking Angular and better-auth major versions. Its key differentiator is the direct integration with Better Auth's plugin ecosystem and a focus on leveraging modern Angular features like signals for session state.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `ngx-better-auth` using `provideBetterAuth` in an Angular 20+ application's `app.config.ts`, including Better Auth client plugins. It also shows basic usage of `AuthService` within a component to access session status and user information reactively via signals.

import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideBetterAuth } from 'ngx-better-auth';
import { environment } from './environments/environment';
import { usernameClient, twoFactorClient, adminClient } from 'better-auth/client/plugins';
import { routes } from './app.routes';
import { AuthService } from 'ngx-better-auth';
import { Component, inject } from '@angular/core';

const accessControl = { /* ... your access control setup ... */ };
const admin = ['admin'];
const moderator = ['moderator'];
const user = ['user'];

export const appConfig: ApplicationConfig = {
  providers: [
    provideRouter(routes),
    provideBetterAuth({
      baseURL: environment.apiUrl,
      basePath: '/auth',
      plugins: [
        usernameClient(),
        twoFactorClient({
          onTwoFactorRedirect: () => {
            window.location.href = '/two-factor-auth';
          },
        }),
        adminClient({
          ac: accessControl,
          roles: {
            admin,
            moderator,
            user
          }
        })
      ]
    })
  ]
};

// Example component usage
@Component({
    selector: 'app-root',
    standalone: true,
    template: `
        <h1>Welcome, {{ userName() ?? 'Guest' }}</h1>
        <p>Logged In: {{ isLoggedIn() }}</p>
        <button *ngIf="!isLoggedIn()" (click)="signIn()">Sign In</button>
        <button *ngIf="isLoggedIn()" (click)="signOut()">Sign Out</button>
    `
})
export class AppComponent {
    private readonly authService = inject(AuthService);

    isLoggedIn = this.authService.isLoggedIn;
    userName = () => this.authService.session()?.user?.name ?? 'Loading...';

    signIn() { /* ... implementation for sign-in ... */ }
    signOut() {
      this.authService.signOut();
    }
}

view raw JSON →