File size: 9,751 Bytes
3998131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# API Client Usage Guide

This guide shows how to use the API client for making authenticated requests to the Nepal Justice Weaver backend.

## Backend API Information

- **Base URL:** `http://localhost:8000`
- **API Prefix:** `/api/v1`
- **Authentication:** Supabase Auth with JWT tokens

## Basic Setup

The API client is already configured and can be imported from `@/lib/api-client`:

```typescript
import { apiClient } from "@/lib/api-client"
import { setAuthData, getAccessToken, clearAuthData } from "@/lib/auth-utils"
```

## Authentication

### Register (Sign Up)
**Endpoint:** `POST /api/v1/auth/signup`

The registration now accepts only basic information:

```typescript
import { apiClient } from "@/lib/api-client"
import { setAuthData } from "@/lib/auth-utils"

try {
  const response = await apiClient.register(
    "john@example.com",      // email
    "securePassword123",      // password (min 6 characters)
    "John Doe"                // full_name
  )

  // Response: { access_token, refresh_token?, user }
  setAuthData(response.access_token, response.refresh_token, response.user)

  console.log("Registered successfully:", response.user)
} catch (error) {
  console.error("Registration failed:", error.message)
}
```

### Login
**Endpoint:** `POST /api/v1/auth/login`

```typescript
import { apiClient } from "@/lib/api-client"
import { setAuthData } from "@/lib/auth-utils"

try {
  const response = await apiClient.login(
    "john@example.com",
    "securePassword123"
  )

  // Store authentication data
  setAuthData(response.access_token, response.refresh_token, response.user)

  console.log("Logged in successfully:", response.user)
} catch (error) {
  console.error("Login failed:", error.message)
}
```

### Logout
```typescript
import { clearAuthData } from "@/lib/auth-utils"

// Clear all authentication data from localStorage
clearAuthData()

// Optionally redirect to login page
router.push("/login")
```

## Protected Features

These features require authentication (access token must be stored in localStorage).

### 1. Letter Generation
**Endpoint:** `POST /api/v1/letter/generate` (requires auth)

Generate legal letters based on user input:

```typescript
try {
  const response = await apiClient.post("/api/v1/letter/generate", {
    letterType: "complaint",
    recipientName: "District Court",
    subject: "Property Dispute",
    details: "Description of the legal matter..."
  })

  console.log("Generated letter:", response)
} catch (error) {
  console.error("Letter generation failed:", error.message)
}
```

### 2. Law Explanation Chatbot
**Endpoint:** `POST /api/v1/law-explanation/chat` (requires auth)

Get explanations about Nepali laws:

```typescript
try {
  const response = await apiClient.post("/api/v1/law-explanation/chat", {
    query: "What are my rights as a tenant in Nepal?",
    context: "rental agreement dispute"
  })

  console.log("Chatbot response:", response)
} catch (error) {
  console.error("Chatbot request failed:", error.message)
}
```

### 3. Bias Detection
**Endpoint:** `POST /api/v1/bias-detection/analyze` (requires auth)

Analyze legal documents or text for potential bias:

```typescript
try {
  const response = await apiClient.post("/api/v1/bias-detection/analyze", {
    text: "Legal document text to analyze for bias...",
    documentType: "court_decision"
  })

  console.log("Bias analysis:", response)
} catch (error) {
  console.error("Bias detection failed:", error.message)
}
```

## Generic Protected Requests

The API client provides generic methods for protected endpoints:

### GET Request
```typescript
try {
  const data = await apiClient.get("/api/v1/some-endpoint")
  console.log("Data:", data)
} catch (error) {
  console.error("Failed:", error.message)
}
```

### POST Request
```typescript
try {
  const result = await apiClient.post("/api/v1/some-endpoint", {
    field1: "value1",
    field2: "value2"
  })
  console.log("Result:", result)
} catch (error) {
  console.error("Failed:", error.message)
}
```

### PUT Request
```typescript
try {
  const updated = await apiClient.put("/api/v1/some-endpoint", {
    field: "updated value"
  })
  console.log("Updated:", updated)
} catch (error) {
  console.error("Failed:", error.message)
}
```

### DELETE Request
```typescript
try {
  const result = await apiClient.delete("/api/v1/some-endpoint")
  console.log("Deleted:", result)
} catch (error) {
  console.error("Failed:", error.message)
}
```

## Using in React Components

### Example: Fetching Data on Component Mount
```typescript
"use client"

import { useEffect, useState } from "react"
import { apiClient } from "@/lib/api-client"

export default function ProfilePage() {
  const [profile, setProfile] = useState(null)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState(null)

  useEffect(() => {
    const fetchProfile = async () => {
      try {
        const data = await apiClient.get("/users/me")
        setProfile(data)
      } catch (err) {
        setError(err.message)
      } finally {
        setLoading(false)
      }
    }

    fetchProfile()
  }, [])

  if (loading) return <div>Loading...</div>
  if (error) return <div>Error: {error}</div>

  return <div>Welcome, {profile?.name}</div>
}
```

### Example: Form Submission
```typescript
"use client"

import { useState } from "react"
import { apiClient } from "@/lib/api-client"

export default function CreateQueryForm() {
  const [title, setTitle] = useState("")
  const [description, setDescription] = useState("")
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState(null)

  const handleSubmit = async (e) => {
    e.preventDefault()
    setLoading(true)
    setError(null)

    try {
      const result = await apiClient.post("/legal-queries", {
        title,
        description
      })
      console.log("Query created:", result)
      // Reset form or redirect
      setTitle("")
      setDescription("")
    } catch (err) {
      setError(err.message)
    } finally {
      setLoading(false)
    }
  }

  return (
    <form onSubmit={handleSubmit}>
      <input
        value={title}
        onChange={(e) => setTitle(e.target.value)}
        placeholder="Title"
      />
      <textarea
        value={description}
        onChange={(e) => setDescription(e.target.value)}
        placeholder="Description"
      />
      {error && <div className="error">{error}</div>}
      <button type="submit" disabled={loading}>
        {loading ? "Submitting..." : "Submit"}
      </button>
    </form>
  )
}
```

## Authentication Utilities

The `@/lib/auth-utils` module provides helper functions for managing authentication:

### Store Authentication Data
```typescript
import { setAuthData } from "@/lib/auth-utils"

// Store access token, refresh token, and user info
setAuthData(accessToken, refreshToken, userData)
```

### Get Access Token
```typescript
import { getAccessToken } from "@/lib/auth-utils"

const token = getAccessToken()
if (token) {
  console.log("User is authenticated")
}
```

### Get Refresh Token
```typescript
import { getRefreshToken } from "@/lib/auth-utils"

const refreshToken = getRefreshToken()
```

### Get User Data
```typescript
import { getUser } from "@/lib/auth-utils"

const user = getUser()
if (user) {
  console.log("User email:", user.email)
}
```

### Check Authentication Status
```typescript
import { isAuthenticated } from "@/lib/auth-utils"

if (isAuthenticated()) {
  // User has valid access token
  // Allow access to protected features
} else {
  // Redirect to login
  router.push("/login")
}
```

### Clear Authentication (Logout)
```typescript
import { clearAuthData } from "@/lib/auth-utils"

// Remove all auth data from localStorage
clearAuthData()
```

## Token Storage Details

Authentication data is stored in localStorage with these keys:
- `access_token` - JWT access token from Supabase
- `refresh_token` - JWT refresh token (optional)
- `user` - JSON stringified user object

The access token is automatically:
- Stored when you register or login
- Included in all authenticated requests with the `Authorization: Bearer <token>` header
- Retrieved from localStorage for each protected request

## Error Handling

The API client throws errors with meaningful messages:
- **Network errors**: Connection failures
- **API errors**: Returns the `detail` field from backend response
- **Validation errors**: Invalid input or missing required fields
- **Authentication errors**: Invalid or expired token (401)

Always wrap API calls in try-catch blocks to handle errors gracefully:

```typescript
try {
  const result = await apiClient.post("/api/v1/some-endpoint", data)
} catch (error) {
  if (error instanceof Error) {
    // Display user-friendly error message
    toast.error(error.message)
  }
}
```

## Environment Variables

Set the backend URL in your `.env.local` file:

```bash
NEXT_PUBLIC_BACKEND_URL=http://localhost:8000
```

If not set, it defaults to `http://localhost:8000`.

## Summary

**Registration Flow:**
1. User fills out registration form with name, email, password
2. Form validates password match and minimum length
3. Call `apiClient.register(email, password, name)`
4. Store tokens with `setAuthData(access_token, refresh_token, user)`
5. Redirect to dashboard

**Login Flow:**
1. User enters email and password
2. Call `apiClient.login(email, password)`
3. Store tokens with `setAuthData(access_token, refresh_token, user)`
4. Redirect to dashboard

**Using Protected Features:**
1. Check authentication with `isAuthenticated()`
2. Make API calls to letter generation, chatbot, or bias detection endpoints
3. API client automatically includes the Bearer token
4. Handle success/error responses appropriately