# AuthService

**Kind:** Class

**Source:** `Pams/Web/Pams.WebApp/Client/app/services/security/auth.service.ts` (line 35)

**Part of:** [Pams](subsystem-pams)

`AuthService` manages client-side authentication state, JWT helper setup, branch switching, company lookup, and permission checks in the Pams web application. Route-related code can call `canActivate()` and `verifyLogin()` to determine whether a user session is valid.

**Implements:** `CanActivate`

## Methods

| Method | Signature | Returns |
|---|---|---|
| `canActivate` | `canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)` | `boolean` |
| `switchBranch` | `switchBranch(branchId: number)` | `string` |
| `login` | `login(usernameInput: string, passwordInput: string)` | `void` |
| `useJwtHelper` | `useJwtHelper()` | `void` |
| `verifyLogin` | `verifyLogin(url: string)` | `boolean` |
| `HasPermission` | `HasPermission(data: string, ID: number, SecurityAccessTypeID: number)` | `any` |
| `GetCompany` | `GetCompany(domain: string)` | `void` |
| `HasPermission2` | `HasPermission2(PersonUserID: number, SecurityAccessTypeID: number, PageID: number)` | `any` |
| `logOut` | `logOut()` | `void` |

## Properties

| Property | Type |
|---|---|
| `userPremissions` | `UserPremissions[]` |
| `jwtHelper` | `JwtHelper` |

## Where it refuses work

- `AuthService` stops the work with an early return when `item == undefined || item == null`.
- `AuthService` stops the work with an early return when `branchId == 0`.
- `AuthService` stops the work with an early return when `url == "" && this.LoggedIn`.

## Diagram

```mermaid
graph LR
    UI[Application UI] --> AuthService[AuthService]
    AuthService --> Login[login]
    AuthService --> Jwt[useJwtHelper]
    AuthService --> Verify[verifyLogin]
    AuthService --> Guard[canActivate]
    AuthService --> Branch[switchBranch]
    AuthService --> Company[GetCompany]
    AuthService --> Permissions[HasPermission / HasPermission2]
    AuthService --> Logout[logOut]
    Guard --> Routes[Protected Routes]
```

## Usage

```ts
import { Component } from "@angular/core";
import { AuthService } from "../services/security/auth.service";

@Component({
  selector: "app-account-menu",
  template: `
    <button *ngIf="isLoggedIn()" (click)="changeBranch()">
      Switch branch
    </button>

    <button (click)="signOut()">Log out</button>
  `
})
export class AccountMenuComponent {
  constructor(private readonly authService: AuthService) {}

  isLoggedIn(): boolean {
    return this.authService.verifyLogin();
  }

  canAccessProtectedRoute(): boolean {
    return this.authService.canActivate();
  }

  changeBranch(): string {
    return this.authService.switchBranch();
  }

  signOut(): void {
    this.authService.logOut();
  }
}
```

## AI Coding Instructions

- Keep authentication and authorization checks routed through `AuthService` rather than duplicating session logic in components.
- Preserve the existing method names and casing, including `HasPermission`, `HasPermission2`, and `GetCompany`.
- Call `verifyLogin()` or `canActivate()` before rendering or navigating to authenticated areas.
- Keep JWT-related changes aligned with `useJwtHelper()`, `login()`, and `logOut()` so session state is cleared consistently.

## Used by

2 references from 2 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

### Imported by (2)

- `MaritalStatusService` — `Pams/Web/Pams.WebApp/Client/app/services/common/marital-status.service.ts`:1
- `SharedServicesModule` — `Pams/Web/Pams.WebApp/Client/app/services/shared-service.module.ts`:1
