# Authentication Domain

Handles user authentication (login and registration) against the configured server endpoint. Provides the login, register, and user-bar UI components.

## Module map

```
authentication/
├── application/
│   └── services/
│       └── authentication.service.ts    HTTP login/register against the active server endpoint
│
├── domain/
│   └── models/
│       └── authentication.model.ts      LoginResponse interface
│
├── feature/
│   ├── login/                       Login form component
│   ├── register/                    Registration form component
│   └── user-bar/                    Displays current user or login/register links
│
└── index.ts                         Barrel exports
```

## Service overview

`AuthenticationService` resolves the API base URL from `ServerDirectoryFacade`, then makes POST requests for login and registration. It does not hold session state itself; after a successful login the calling component dispatches `UsersActions.authenticateUser`, and the users effects prepare the local persistence boundary before exposing the new user in the NgRx store.

```mermaid
graph TD
    Login[LoginComponent]
    Register[RegisterComponent]
    UserBar[UserBarComponent]
    Auth[AuthenticationService]
    SD[ServerDirectoryFacade]
    Store[NgRx Store]

    Login --> Auth
    Register --> Auth
    UserBar --> Store
    Auth --> SD
    Login --> Store

    click Auth "application/services/authentication.service.ts" "HTTP login/register" _blank
    click Login "feature/login/" "Login form" _blank
    click Register "feature/register/" "Registration form" _blank
    click UserBar "feature/user-bar/" "Current user display" _blank
    click SD "../server-directory/application/server-directory.facade.ts" "Resolves API URL" _blank
```

## Login flow

```mermaid
sequenceDiagram
    participant User
    participant Login as LoginComponent
    participant Auth as AuthenticationService
    participant SD as ServerDirectoryFacade
    participant API as Server API
    participant Store as NgRx Store
    participant Effects as UsersEffects

    User->>Login: Submit credentials
    Login->>Auth: login(username, password)
    Auth->>SD: getApiBaseUrl()
    SD-->>Auth: https://server/api
    Auth->>API: POST /api/auth/login
    API-->>Auth: { userId, displayName }
    Auth-->>Login: success
    Login->>Store: UsersActions.authenticateUser
    Store->>Effects: prepare persisted user scope
    Effects->>Store: reset stale room/user/message state
    Effects->>Store: UsersActions.setCurrentUser
```

## Registration flow

Registration follows the same pattern but posts to `/api/auth/register` with an additional `displayName` field. On success the user is treated as logged in and the same authenticated-user transition runs, switching the browser persistence layer to that user's local scope before the app reloads rooms and user state.

## User bar

`UserBarComponent` reads the current user from the NgRx store. When logged in it shows the user's display name; when not logged in it shows links to the login and register views.
