-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-me.html
More file actions
630 lines (580 loc) · 21.4 KB
/
api-me.html
File metadata and controls
630 lines (580 loc) · 21.4 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API /me - Circuit Auction API</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
body { padding-top: 70px; background-color: #f8f9fa; }
.navbar-brand { font-weight: bold; }
.main-content { padding: 2rem; }
pre { background: #f4f4f4; padding: 1rem; border-radius: 0.375rem; position: relative; overflow-x: auto; }
.copy-btn { position: absolute; top: 0.5rem; right: 0.5rem; font-size: 0.8rem; }
.endpoint { font-family: 'Courier New', monospace; background: #e9ecef; padding: 0.2rem 0.4rem; border-radius: 0.25rem; }
.method-badge { font-weight: bold; padding: 0.25rem 0.5rem; border-radius: 0.25rem; }
.method-get { background-color: #61affe; color: white; }
.param-table th { background-color: #f8f9fa; }
.section-title { border-bottom: 2px solid #0d6efd; padding-bottom: 0.5rem; margin-bottom: 1.5rem; }
.field-path { font-family: 'Courier New', monospace; color: #d63384; font-size: 0.9em; }
</style>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">Circuit Auction API v1.0</a>
</div>
</nav>
<div class="container main-content">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.html">API</a></li>
<li class="breadcrumb-item active" aria-current="page">API /me</li>
</ol>
</nav>
<h1 class="section-title">
<span class="method-badge method-get">GET</span> /api/me
</h1>
<p class="lead">Retrieve current user information and application configuration.</p>
<!-- Overview -->
<div class="card mb-4">
<div class="card-header">
<h4 class="mb-0">Overview</h4>
</div>
<div class="card-body">
<p>The <code>/api/me</code> endpoint returns detailed information about the currently authenticated user, including:</p>
<ul>
<li>User profile information (ID, name, email, roles)</li>
<li>Application configuration settings</li>
<li>Pusher/WebSocket connection details</li>
<li>Accounting and business settings</li>
<li>Staff list, task types, and activity types</li>
<li>Currency and regional settings</li>
</ul>
<div class="alert alert-info">
<strong>Authentication Required:</strong> This endpoint requires a valid access token obtained from <code>/api/login-token</code>.
</div>
</div>
</div>
<!-- Endpoint Details -->
<div class="card mb-4">
<div class="card-header">
<h4 class="mb-0">Endpoint Details</h4>
</div>
<div class="card-body">
<table class="table">
<tr>
<th style="width: 150px;">URL</th>
<td><code class="endpoint">/api/me</code></td>
</tr>
<tr>
<th>Method</th>
<td><span class="method-badge method-get">GET</span></td>
</tr>
<tr>
<th>Authentication</th>
<td>Required (access token)</td>
</tr>
<tr>
<th>Content-Type</th>
<td>application/json</td>
</tr>
</table>
</div>
</div>
<!-- Request -->
<div class="card mb-4">
<div class="card-header">
<h4 class="mb-0">Request</h4>
</div>
<div class="card-body">
<h5>Query Parameters</h5>
<div class="table-responsive">
<table class="table table-sm param-table">
<thead>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Required</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>access_token</code></td>
<td>string</td>
<td>Yes</td>
<td>Your API access token obtained from <code>/api/login-token</code></td>
</tr>
</tbody>
</table>
</div>
<h5 class="mt-4">Example Request</h5>
<pre><code class="language-bash">curl -X GET "https://backoffice.ddev.site/api/me?access_token=YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json"</code></pre>
<h5 class="mt-4">JavaScript Example</h5>
<pre><code class="language-javascript">const API_BASE_URL = 'https://backoffice.ddev.site';
const ACCESS_TOKEN = localStorage.getItem('access_token');
fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => response.json())
.then(data => {
console.log('User info:', data.data[0]);
console.log('Config:', data.data[0].config);
})
.catch(error => {
console.error('Error fetching user data:', error);
});</code></pre>
</div>
</div>
<!-- Response -->
<div class="card mb-4">
<div class="card-header">
<h4 class="mb-0">Response</h4>
</div>
<div class="card-body">
<h5>Response Structure</h5>
<p>The response contains a <code>data</code> array with a single user object containing profile and configuration information.</p>
<h5 class="mt-4">Key Response Fields</h5>
<div class="table-responsive">
<table class="table table-sm param-table">
<thead>
<tr>
<th>Field Path</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="field-path">data[0].id</span></td>
<td>string</td>
<td>User ID</td>
</tr>
<tr>
<td><span class="field-path">data[0].label</span></td>
<td>string</td>
<td>Username/label</td>
</tr>
<tr>
<td><span class="field-path">data[0].mail</span></td>
<td>string</td>
<td>User email address</td>
</tr>
<tr>
<td><span class="field-path">data[0].roles</span></td>
<td>array</td>
<td>User roles (e.g., "administrator", "clerk")</td>
</tr>
<tr>
<td><span class="field-path">data[0].full_name</span></td>
<td>string</td>
<td>Full name of the user</td>
</tr>
<tr>
<td><span class="field-path">data[0].sale_context</span></td>
<td>string</td>
<td>Current sale context ID</td>
</tr>
<tr>
<td><span class="field-path">data[0].config</span></td>
<td>object</td>
<td>Configuration object (see below)</td>
</tr>
</tbody>
</table>
</div>
<h5 class="mt-4">Configuration Object Fields</h5>
<div class="table-responsive">
<table class="table table-sm param-table">
<thead>
<tr>
<th>Field Path</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="field-path">config.pusherConfig</span></td>
<td>object</td>
<td><strong>Pusher Configuration</strong> - Contains key, cluster, and privateChannel for Pusher</td>
</tr>
<tr>
<td><span class="field-path">config.pusherConfig.key</span></td>
<td>string</td>
<td><strong>Pusher App Key</strong> - Your Pusher application key</td>
</tr>
<tr>
<td><span class="field-path">config.pusherConfig.cluster</span></td>
<td>string</td>
<td><strong>Pusher Cluster</strong> - Pusher cluster region (e.g., "eu", "us2")</td>
</tr>
<tr>
<td><span class="field-path">config.pusherConfig.privateChannel</span></td>
<td>string</td>
<td><strong>Private Channel Name</strong> - The private channel to subscribe to</td>
</tr>
<tr>
<td><span class="field-path">config.bidServer</span></td>
<td>string</td>
<td>WebSocket server URL for real-time events</td>
</tr>
<tr>
<td><span class="field-path">config.bidServerAccessToken</span></td>
<td>string</td>
<td>Bid server access token (for bidding operations, not used for Pusher)</td>
</tr>
<tr>
<td><span class="field-path">config.websiteBasePath</span></td>
<td>string</td>
<td>Base URL for the website/backoffice</td>
</tr>
<tr>
<td><span class="field-path">config.accountingCountry</span></td>
<td>string</td>
<td>Accounting country code (e.g., "DE", "US")</td>
</tr>
<tr>
<td><span class="field-path">config.staff</span></td>
<td>array</td>
<td>List of staff members with ID, name, email, UUID</td>
</tr>
<tr>
<td><span class="field-path">config.task_types</span></td>
<td>array</td>
<td>Available task types for the system</td>
</tr>
<tr>
<td><span class="field-path">config.activity_types</span></td>
<td>array</td>
<td>Available activity types for tracking</td>
</tr>
<tr>
<td><span class="field-path">config.currency</span></td>
<td>object</td>
<td>Currency settings (symbol, format, decimal places)</td>
</tr>
<tr>
<td><span class="field-path">config.contentLanguages</span></td>
<td>object</td>
<td>Available content languages</td>
</tr>
<tr>
<td><span class="field-path">config.itemPageSettings</span></td>
<td>object</td>
<td>Item page configuration and display settings</td>
</tr>
</tbody>
</table>
</div>
<h5 class="mt-4">Example Response (Abbreviated)</h5>
<pre><code class="language-json">{
"data": [{
"id": "1",
"label": "admin",
"mail": "admin@circuitauction.com",
"roles": [
"authenticated user",
"administrator",
"clerk"
],
"picture": null,
"full_name": "Administrator",
"sale_context": "309",
"config": {
"bidServer": "https://circuit-bid.ddev.site:4443",
"bidServerAccessToken": "7wESvKVQW5EljtvylmW2eNlS8nrVgdJt0uRXQAoNya4",
"websiteBasePath": "https://backoffice.ddev.site",
"accountingCountry": "DE",
"pusherConfig": {
"key": "e589860bfd6885f493bf",
"cluster": "eu",
"privateChannel": "private-general@backoffice-hk-eu-local"
},
"currency": {
"key": "eur",
"symbol": "€",
"template": "@amount @symbol",
"decimal_sign": ",",
"decimal_amount": 2,
"thousands_sign": "."
},
"contentLanguages": {
"en": "en",
"de": "de"
},
"staff": [
{
"id": "30",
"label": "John Doe",
"mail": "john@example.com",
"value": "30",
"uuid": "efed71c0-fba6-42e2-bd3d-8287638df2b1"
}
],
"task_types": [
{
"id": 3922,
"label": "Call customer",
"weight": "0",
"value": "3922"
}
],
"activity_types": [
{
"id": 90,
"label": "Sent to buyer",
"weight": "0"
}
]
}
}]
}</code></pre>
</div>
</div>
<!-- Use Cases -->
<div class="card mb-4">
<div class="card-header">
<h4 class="mb-0">Common Use Cases</h4>
</div>
<div class="card-body">
<h5>1. Get Pusher Configuration</h5>
<p>Extract WebSocket/Pusher settings for real-time event subscriptions:</p>
<pre><code class="language-javascript">const ACCESS_TOKEN = localStorage.getItem('access_token'); // From /api/login-token
fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => response.json())
.then(data => {
const config = data.data[0].config;
const pusherKey = config.pusherConfig.key;
const pusherCluster = config.pusherConfig.cluster;
const privateChannel = config.pusherConfig.privateChannel;
// Use these for Pusher initialization
console.log('Pusher Key:', pusherKey);
console.log('Pusher Cluster:', pusherCluster);
console.log('Private Channel:', privateChannel);
// Note: Use ACCESS_TOKEN (from /api/login-token) for Pusher auth
});</code></pre>
<h5 class="mt-3">2. Display User Profile</h5>
<pre><code class="language-javascript">fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => response.json())
.then(data => {
const user = data.data[0];
console.log('User:', user.full_name);
console.log('Email:', user.mail);
console.log('Roles:', user.roles.join(', '));
});</code></pre>
<h5 class="mt-3">3. Get Currency Settings</h5>
<pre><code class="language-javascript">fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => response.json())
.then(data => {
const currency = data.data[0].config.currency;
console.log('Currency:', currency.key);
console.log('Symbol:', currency.symbol);
console.log('Format:', currency.template);
// Format a price
const price = 1234.56;
const formatted = price.toFixed(currency.decimal_amount)
.replace('.', currency.decimal_sign)
.replace(/\B(?=(\d{3})+(?!\d))/g, currency.thousands_sign);
console.log('Formatted price:', `${formatted} ${currency.symbol}`);
// Output: "1.234,56 €"
});</code></pre>
<h5 class="mt-3">4. Load Staff List for Dropdowns</h5>
<pre><code class="language-javascript">fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => response.json())
.then(data => {
const staff = data.data[0].config.staff;
// Populate a select dropdown
const select = document.getElementById('staffSelect');
staff.forEach(member => {
const option = document.createElement('option');
option.value = member.id;
option.textContent = member.label;
select.appendChild(option);
});
});</code></pre>
<h5 class="mt-3">5. Check User Roles</h5>
<pre><code class="language-javascript">fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => response.json())
.then(data => {
const roles = data.data[0].roles;
// Check if user is administrator
const isAdmin = roles.includes('administrator');
console.log('Is administrator:', isAdmin);
// Show/hide admin features
if (isAdmin) {
document.getElementById('adminPanel').style.display = 'block';
}
});</code></pre>
</div>
</div>
<!-- Integration with Pusher -->
<div class="card mb-4 border-info">
<div class="card-header bg-info text-white">
<h4 class="mb-0"><i class="fas fa-plug"></i> Integration with Pusher Events</h4>
</div>
<div class="card-body">
<p>The <code>/api/me</code> endpoint provides the necessary configuration to connect to Pusher for real-time events.</p>
<h5>Complete Example: Initialize Pusher with /api/me</h5>
<pre><code class="language-javascript">const API_BASE_URL = 'https://backoffice.ddev.site';
const ACCESS_TOKEN = localStorage.getItem('access_token'); // From /api/login-token
// Step 1: Get configuration from /api/me
fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => response.json())
.then(data => {
const config = data.data[0].config;
// Step 2: Extract Pusher configuration from pusherConfig object
const PUSHER_KEY = config.pusherConfig.key;
const PUSHER_CLUSTER = config.pusherConfig.cluster;
const PRIVATE_CHANNEL = config.pusherConfig.privateChannel;
// Step 3: Initialize Pusher with regular access token
const pusher = new Pusher(PUSHER_KEY, {
cluster: PUSHER_CLUSTER,
encrypted: true,
forceTLS: true,
authEndpoint: `${API_BASE_URL}/api/v1.0/pusher_auth?access_token=${ACCESS_TOKEN}`
});
// Step 4: Subscribe to channel and bind events
const channel = pusher.subscribe(PRIVATE_CHANNEL);
channel.bind('entity_update', function(eventData) {
console.log('Entity updated:', eventData);
});
channel.bind('advanced_queue__post_execute', function(eventData) {
console.log('Queue task completed:', eventData);
});
})
.catch(error => {
console.error('Error initializing Pusher:', error);
});</code></pre>
<div class="alert alert-info mt-3">
<strong>See Also:</strong> <a href="pusher-events.html">Pusher Events Documentation</a> for complete details on real-time event handling.
</div>
</div>
</div>
<!-- Error Responses -->
<div class="card mb-4 border-danger">
<div class="card-header bg-danger text-white">
<h4 class="mb-0"><i class="fas fa-exclamation-circle"></i> Error Responses</h4>
</div>
<div class="card-body">
<h5>401 Unauthorized</h5>
<p>Returned when the access token is invalid or expired.</p>
<pre><code class="language-json">{
"error": "Unauthorized",
"message": "Invalid or expired access token"
}</code></pre>
<h5 class="mt-3">403 Forbidden</h5>
<p>Returned when the user doesn't have permission to access this endpoint.</p>
<pre><code class="language-json">{
"error": "Forbidden",
"message": "You do not have permission to access this resource"
}</code></pre>
<h5 class="mt-3">Handling Errors</h5>
<pre><code class="language-javascript">fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => {
if (!response.ok) {
if (response.status === 401) {
// Token expired, redirect to login
window.location.href = '/login';
throw new Error('Unauthorized');
}
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
// Process data
console.log('User data:', data);
})
.catch(error => {
console.error('Error fetching user data:', error);
});</code></pre>
</div>
</div>
<!-- Best Practices -->
<div class="card mb-4 border-success">
<div class="card-header bg-success text-white">
<h4 class="mb-0"><i class="fas fa-check-circle"></i> Best Practices</h4>
</div>
<div class="card-body">
<h5>1. Cache the Response</h5>
<p>Cache the <code>/api/me</code> response to avoid repeated API calls:</p>
<pre><code class="language-javascript">// Cache user data in localStorage or sessionStorage
const cacheKey = 'user_config';
const cacheExpiry = 3600000; // 1 hour in milliseconds
function getUserConfig() {
const cached = localStorage.getItem(cacheKey);
if (cached) {
const data = JSON.parse(cached);
if (Date.now() - data.timestamp < cacheExpiry) {
return Promise.resolve(data.value);
}
}
// Fetch fresh data
return fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`)
.then(response => response.json())
.then(data => {
localStorage.setItem(cacheKey, JSON.stringify({
value: data,
timestamp: Date.now()
}));
return data;
});
}</code></pre>
<h5 class="mt-3">2. Load Configuration on App Startup</h5>
<p>Fetch user configuration once when the application starts:</p>
<pre><code class="language-javascript">// In your app initialization
async function initializeApp() {
try {
const response = await fetch(`${API_BASE_URL}/api/me?access_token=${ACCESS_TOKEN}`);
const data = await response.json();
// Store globally or in state management
window.appConfig = data.data[0].config;
window.currentUser = data.data[0];
// Initialize Pusher, load UI, etc.
initializePusher(window.appConfig);
loadUserInterface(window.currentUser);
} catch (error) {
console.error('Failed to initialize app:', error);
}
}
// Call on page load
initializeApp();</code></pre>
<h5 class="mt-3">3. Refresh on Token Change</h5>
<p>Re-fetch <code>/api/me</code> when the user's access token changes:</p>
<pre><code class="language-javascript">// When user logs in or token refreshes
function onAccessTokenChange(newToken) {
localStorage.setItem('access_token', newToken);
// Clear cached config
localStorage.removeItem('user_config');
// Fetch fresh configuration
getUserConfig().then(data => {
console.log('Configuration refreshed');
});
}</code></pre>
</div>
</div>
<a href="pusher-events.html" class="btn btn-primary mb-5">View Pusher Events Documentation</a>
</div>
<script src="assets/js/sidebar.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<script>
document.querySelectorAll('pre code').forEach(block => {
const button = document.createElement('button');
button.className = 'btn btn-sm btn-outline-secondary copy-btn';
button.textContent = 'Copy';
button.onclick = () => {
navigator.clipboard.writeText(block.textContent);
button.textContent = 'Copied!';
setTimeout(() => button.textContent = 'Copy', 2000);
};
block.parentElement.appendChild(button);
});
</script>
</body>
</html>