npm package: @lissa-ems/auth-service
Description: Centralized authentication service for LISSA apps
Latest version: 1.1.1

# @lissa-ems/auth-service

Centralized cross-app logout service for LISSA Angular applications.

## Installation

```bash
npm install @lissa-ems/auth-service
```

## Usage

### Angular 9-15 (Module-based)

#### 1. Configure in app.module.ts

```typescript
import { NgModule } from '@angular/core';
import { LissaAuthService, AUTH_CONFIG } from '@lissa-ems/auth-service';

@NgModule({
  providers: [
    {
      provide: AUTH_CONFIG,
      useValue: {
        authUrl: 'https://auth.example.com'
      }
    },
    {
      provide: LissaAuthService,
      useFactory: (config: any) => new LissaAuthService(config),
      deps: [AUTH_CONFIG]
    }
  ]
})
export class AppModule { }
```

#### 2. Use in app.component.ts

```typescript
import { Component, OnInit } from '@angular/core';
import { LissaAuthService } from '@lissa-ems/auth-service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
  constructor(private lissaAuth: LissaAuthService) {}

  ngOnInit() {
    this.lissaAuth.startSessionCheck();
  }
}
```

### Angular 16+ (Standalone)

#### 1. Configure in app.config.ts

```typescript
import { ApplicationConfig } from '@angular/core';
import { AUTH_CONFIG, LissaAuthService } from '@lissa-ems/auth-service';

export const appConfig: ApplicationConfig = {
  providers: [
    {
      provide: AUTH_CONFIG,
      useValue: {
        authUrl: 'https://auth.example.com'
      }
    },
    {
      provide: LissaAuthService,
      useFactory: (config: any) => new LissaAuthService(config),
      deps: [AUTH_CONFIG]
    }
  ]
};
```

#### 2. Use in app.component.ts

```typescript
import { Component, OnInit, inject } from '@angular/core';
import { LissaAuthService } from '@lissa-ems/auth-service';

@Component({
  selector: 'app-root',
  standalone: true,
  templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
  private lissaAuth = inject(LissaAuthService);

  ngOnInit() {
    this.lissaAuth.startSessionCheck();
  }
}
```

### Common Usage in Components

```typescript
import { Component } from '@angular/core';
import { LissaAuthService } from '@lissa-ems/auth-service';

@Component({
  selector: 'app-navbar',
  template: `
    <button *ngIf="isLoggedIn()" (click)="logout()">Logout</button>
  `
})
export class NavbarComponent {
  constructor(private lissaAuth: LissaAuthService) {}

  logout() {
    this.lissaAuth.logout().subscribe();
  }

  isLoggedIn(): boolean {
    return this.lissaAuth.isAuthenticated();
  }

  getCurrentUser() {
    return this.lissaAuth.getUser();
  }
}
```

### Auth App Configuration

In your centralized authentication app, add logout detection:

```typescript
// auth-app/app.component.ts
export class AppComponent implements OnInit {
  constructor(private lissaAuth: LissaAuthService) {}

  ngOnInit() {
    // Handle logout from other apps
    this.lissaAuth.handleLogoutFromOtherApp();
    
    // Start session monitoring
    this.lissaAuth.startSessionCheck();
  }
}
```

## API Reference

### Methods

| Method | Description | Returns |
|--------|-------------|----------|
| `logout()` | Logout user and redirect to auth app | `Observable<any>` |
| `startSessionCheck(intervalMs?)` | Start monitoring for cross-app logout | `void` |
| `stopSessionCheck()` | Stop session monitoring | `void` |
| `isAuthenticated()` | Check if user is logged in | `boolean` |
| `getUser()` | Get current user data | `CurrentUser \| null` |
| `getAccessToken()` | Get current access token | `string \| null` |
| `checkTokenValidity()` | Validate current token | `Observable<any>` |
| `handleLogoutFromOtherApp()` | Handle logout from other apps (auth app only) | `void` |

### Configuration

| Property | Type | Description |
|----------|------|-------------|
| `authUrl` | `string` | URL of your centralized authentication app |

## Features

- **Cross-app logout**: Logout from one app logs out all apps
- **Session monitoring**: Automatic token validation and logout detection
- **Angular compatibility**: Supports Angular 9.x through 21.x
- **Simple setup**: Only requires auth app URL configuration
- **Lightweight**: No external dependencies beyond Angular and RxJS

## How It Works

1. **User logs out**: Any app calls `logout()` method
2. **Session cleared**: localStorage and sessionStorage are cleared
3. **Redirect to auth app**: User is redirected to auth app with `?logout=true`
4. **Auth app logout**: Auth app detects parameter and performs complete logout
5. **Cross-app detection**: Other apps detect missing session via polling and auto-logout

## Requirements

- Angular 9.x - 21.x
- RxJS 6.x - 8.x
- User data stored in `localStorage` as `current_user` with `accessToken` property