# AuthService

**Kind:** Class

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

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

`AuthService` manages client-side authentication state and access checks for the PAMS web application. It exposes methods for login verification, token access, branch switching, OTP and trusted-machine flows, and route activation decisions.

**Implements:** `CanActivate`

## Methods

| Method | Signature | Returns |
|---|---|---|
| `getToken` | `getToken()` | `string` |
| `canActivate` | `canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)` | `boolean` |
| `switchBranch` | `switchBranch(branchId: number)` | `string` |
| `login` | `login(usernameInput: string, passwordInput: string, rememberMe: boolean, cookiesCode: string)` | `void` |
| `saveUserLoggedInInformation` | `saveUserLoggedInInformation()` | `void` |
| `verify` | `verify(code: undefined, id: undefined)` | `void` |
| `verifiedOTPlogin` | `verifiedOTPlogin(username: undefined, password: undefined, id: undefined, grantCode: undefined, grantIP: undefined, isTwoFactorVerified: undefined)` | `void` |
| `trustedMachine` | `trustedMachine(grantCode: string)` | `void` |
| `useJwtHelper` | `useJwtHelper()` | `void` |
| `verifyLogin` | `verifyLogin(url: string)` | `boolean` |
| `getSubscriptionPackages` | `getSubscriptionPackages()` | `void` |
| `HasPermission` | `HasPermission(data: string, ID: number, SecurityAccessTypeID: number)` | `any` |
| `GetCompany` | `GetCompany(domain: string)` | `void` |
| `HasPermission2` | `HasPermission2(PersonUserID: number, SecurityAccessTypeID: number, PageID: number)` | `any` |
| `userPermissionsForNewList` | `userPermissionsForNewList()` | `void` |
| `getPermissionsForSheet` | `getPermissionsForSheet(moduleId: number, objId: number)` | `any` |
| `setListOfNewPermission` | `setListOfNewPermission(value: ListOfNewResponse)` | `void` |
| `setExportLogs` | `setExportLogs(moduleId: number)` | `void` |
| `checkErrorResponse` | `checkErrorResponse(Response: undefined, module: string)` | `string` |
| `clearStoredData` | `clearStoredData()` | `void` |
| `getNotificationCount` | `getNotificationCount()` | `Observable<any>` |
| `logOut` | `logOut()` | `void` |

## Properties

| Property | Type |
|---|---|
| `jwtHelper` | `any` |
| `userPremissions` | `UserPremissions[]` |
| `_listOfNewPermission` | `ListOfNewResponse` |
| `listOfNewPermission` | `any` |
| `SubscriptionPackages` | `number[]` |

## 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 `url === 'home' || url === ''`.
- `AuthService` stops the work with an early return when `branchId === 0`.
- `AuthService` stops the work with an early return when `url === '' && this.LoggedIn`.
- `AuthService` stops the work with an early return when `SecurityAccessType`.
- `AuthService` stops the work with an early return when `module == 'users'`.

## Diagram

```mermaid
graph LR
  Client[Client Component] --> AuthService[AuthService]
  AuthService --> Login[login]
  AuthService --> Verification[verify / verifyLogin]
  AuthService --> OTP[verifiedOTPlogin]
  AuthService --> TrustedMachine[trustedMachine]
  AuthService --> Token[getToken]
  AuthService --> RouteGuard[canActivate]
  AuthService --> Branch[switchBranch]
  AuthService --> Jwt[useJwtHelper]
  AuthService --> Session[saveUserLoggedInInformation]
```

## Usage

```ts
import { AuthService } from './services/security/auth.service';

export class AccountComponent {
  constructor(private readonly authService: AuthService) {}

  signIn(): void {
    this.authService.login();
  }

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

  getCurrentToken(): string {
    return this.authService.getToken();
  }

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

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

## AI Coding Instructions

- Keep authentication and session-related operations inside `AuthService` rather than duplicating token or login-state logic in components.
- Call `canActivate()` or `verifyLogin()` before allowing protected client-side actions or navigation.
- Use `getToken()` as the single access point for the current authentication token.
- Preserve the existing login flow when changing `login()`, `verify()`, `verifiedOTPlogin()`, or `trustedMachine()`, since these methods may depend on shared session state.
- Update branch-related behavior through `switchBranch()` so branch state remains consistent with the authenticated user context.

## Used by

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

### Imported by (117)

- `AppRoutingModule` — `Pams/Web/Pams.Web/Client/app/app-routing.module.ts`:1
- `AppComponent` — `Pams/Web/Pams.Web/Client/app/app.component.ts`:1
- `MrqInquiryComponent` — `Pams/Web/Pams.Web/Client/app/components/MRQ/mrq-details/mrq-inquiry/mrq-inquiry.component.ts`:1
- `BlogsComponent` — `Pams/Web/Pams.Web/Client/app/components/blogs/blogs.component.ts`:1
- `AccountManagersComponent` — `Pams/Web/Pams.Web/Client/app/components/configuration/account-managers/account-managers.component.ts`:1
- `CompanyCurrencyComponent` — `Pams/Web/Pams.Web/Client/app/components/configuration/commercial/company-currency/company-currency.component.ts`:1
- `DeliveryTermsComponent` — `Pams/Web/Pams.Web/Client/app/components/configuration/commercial/delivery-terms/delivery-terms.component.ts`:1
- `GuaranteeTypesComponent` — `Pams/Web/Pams.Web/Client/app/components/configuration/commercial/guarantee-types/guarantee-types.component.ts`:1

…and 109 more.
