# DEFINING GLOBAL TYPES

To ensure Type Safety throughout the application and allow the [**TypeScript**](https://www.typescriptlang.org/) compiler to recognize the new global authentication functions (such as `handleLogin` and `handleRegister`), we must extend the global type declarations.

### Implementation

Open the file `web/src/types.d.ts` and add the following type declarations starting from `handleLogin` down to `useSignOut`. These definitions inform **TypeScript** about the existence, arguments, and return types of these functions, preventing compilation errors in the views and hooks.

{% code overflow="wrap" lineNumbers="true" %}

```typescript
declare function handleLogin(
    username: string,
    password: string
): Promise<{ success: boolean; message: string; }>;

declare function handleRegister(
    username: string,
    password: string,
    accountData: unknown,
): Promise<{ success: boolean; message: string; }>;

declare function useCurrentUser(): { username: string; accountData: unknown } | null;

declare function useIsAuthenticated(): boolean;
declare function useSignOut(): void;
```

{% endcode %}

{% hint style="info" %}
***Note**: The existing utility declarations (e.g., `openGalleryPicker`, `startCall`, `useSettings`, etc.) already present in `types.d.ts` should remain untouched. You only need to append the new authentication declarations listed above.*
{% endhint %}

### Functions Reference

#### `handleLogin`

```typescript
const response = await handleLogin(
    'username', 
    'password'
);
console.log(response);
```

**Arguments**

<table><thead><tr><th width="120" align="center">Name</th><th width="100" align="center">Type</th><th width="100" align="center">Optional</th><th>Description</th></tr></thead><tbody><tr><td align="center"><code>username</code></td><td align="center">string</td><td align="center">❌</td><td>The username to attempt login with.</td></tr><tr><td align="center"><code>password</code></td><td align="center">string</td><td align="center">❌</td><td>The user's password.</td></tr></tbody></table>

**Returns**

<table><thead><tr><th width="120" align="center">Name</th><th width="95" align="center">Type</th><th>Description</th></tr></thead><tbody><tr><td align="center"><code>success</code></td><td align="center">boolean</td><td>The <code>success</code> boolean is <code>true</code> upon success.</td></tr><tr><td align="center"><code>message</code></td><td align="center">string</td><td>The <code>message</code> string provides an error description (if unsuccessful) or a confirmation message.</td></tr></tbody></table>

#### `handleRegister`

**Example**

```typescript
const response = await handleRegister(
    'newuser',
    'securepassword',
    {
        email: 'test@example.com',
        age: 30
    }
);
console.log(response);
```

**Arguments**

<table data-header-hidden><thead><tr><th width="120" align="center">Name</th><th width="100" align="center">Type</th><th width="100" align="center">Optional</th><th>Description</th></tr></thead><tbody><tr><td align="center"><code>username</code></td><td align="center">string</td><td align="center">❌</td><td>The username for the new registration.</td></tr><tr><td align="center"><code>password</code></td><td align="center">string</td><td align="center">❌</td><td>The new user's password.</td></tr><tr><td align="center"><code>accountData</code></td><td align="center">unknown</td><td align="center">✅</td><td>Optional additional account data to be saved (e.g., email, date of birth). Can be <code>null</code>.</td></tr></tbody></table>

**Returns**

<table><thead><tr><th width="120" align="center">Name</th><th width="95" align="center">Type</th><th>Description</th></tr></thead><tbody><tr><td align="center"><code>success</code></td><td align="center">boolean</td><td>The <code>success</code> boolean is <code>true</code> upon success.</td></tr><tr><td align="center"><code>message</code></td><td align="center">string</td><td>The <code>message</code> string provides an error description (if unsuccessful) or a confirmation message.</td></tr></tbody></table>

#### `useCurrentUser`

**Example**

```typescript
const user = useCurrentUser();
if (user) {
    console.log(`Logged in as: ${user.username}`);
}
```

**Returns**

<table><thead><tr><th width="120" align="center">Name</th><th width="100" align="center">Type</th><th>Description</th></tr></thead><tbody><tr><td align="center"><code>username</code></td><td align="center">string</td><td>Current login username</td></tr><tr><td align="center"><code>accountData</code></td><td align="center">unknown</td><td>Return data provided while registering</td></tr></tbody></table>
