Skip to content

@barcoding/auth-core

The auth-core package handles authentication and authorization across all applications.

Overview

  • Package: @barcoding/auth-core
  • Location: web/projects/packages/@barcoding/auth-core/
  • Used By: All three application instances
  • Dependencies: @barcoding/core, @barcoding/sdk

Key Features

Authentication

  • JWT token management
  • Login/logout functionality
  • Token refresh
  • Session management

Authorization

  • Role-based access control (RBAC)
  • Permission checking
  • Route guards
  • ACL service

HTTP Interceptors

  • Automatic token injection
  • Error handling
  • Request/response transformation

Main Exports

AuthService

Authentication service:

typescript
import { AuthService } from '@barcoding/auth-core';

constructor(private authService: AuthService) {}

// Login
this.authService.login(username, password).subscribe(
  user => console.log('Logged in', user),
  error => console.error('Login failed', error)
);

// Logout
this.authService.logout();

// Check auth status
this.authService.isAuthenticated();

AuthGuard

Route protection:

typescript
import { AuthGuard } from '@barcoding/auth-core';

const routes: Routes = [
  {
    path: 'dashboard',
    component: DashboardComponent,
    canActivate: [AuthGuard]
  }
];

AuthState (NGXS)

Authentication state:

typescript
import { AuthState } from '@barcoding/auth-core';
import { Select } from '@ngxs/store';

@Select(AuthState.user) user$: Observable<User>;
@Select(AuthState.isAuthenticated) isAuthenticated$: Observable<boolean>;
@Select(AuthState.permissions) permissions$: Observable<string[]>;

AclService

Permission checking:

typescript
import { AclService } from '@barcoding/auth-core';

constructor(private aclService: AclService) {}

// Check permission
if (this.aclService.can('tickets.create')) {
  // User can create tickets
}

// Check role
if (this.aclService.hasRole('admin')) {
  // User is admin
}

Usage Example

typescript
import { Component, OnInit } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { AuthState, Login, Logout } from '@barcoding/auth-core';

@Component({
  selector: 'app-login',
  template: `
    <div *ngIf="!(isAuthenticated$ | async)">
      <form (ngSubmit)="login()">
        <input [(ngModel)]="username" name="username">
        <input [(ngModel)]="password" name="password" type="password">
        <button type="submit">Login</button>
      </form>
    </div>
    <div *ngIf="isAuthenticated$ | async">
      <p>Welcome, {{ (user$ | async)?.name }}!</p>
      <button (click)="logout()">Logout</button>
    </div>
  `
})
export class LoginComponent {
  @Select(AuthState.user) user$: Observable<User>;
  @Select(AuthState.isAuthenticated) isAuthenticated$: Observable<boolean>;

  username = '';
  password = '';

  constructor(private store: Store) {}

  login() {
    this.store.dispatch(new Login({
      username: this.username,
      password: this.password
    }));
  }

  logout() {
    this.store.dispatch(new Logout());
  }
}

Coming Soon

This page is under construction. More details will be added about:

  • Complete API reference
  • Permission system details
  • Token refresh mechanism
  • Custom guards
  • Best practices

For now, refer to:

Syneo/Barcoding Documentation