Fix sensors list API: add missing auth headers and credentials method
- Add baseUrl and legacyApiUrl as class properties in ApiService - Add getLegacyCredentials() method for device operations - Add Authorization header to getDevicesForBeneficiary() - Add Authorization header to attachDeviceToBeneficiary() These changes fix the sensors list functionality allowing users to view sensors for any beneficiary. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
8a633a0f6b
commit
102a562f9d
@ -48,6 +48,10 @@ function formatTimeAgo(date: Date): string {
|
||||
}
|
||||
|
||||
class ApiService {
|
||||
// API URLs as instance properties for consistency
|
||||
private readonly baseUrl = WELLNUO_API_URL;
|
||||
private readonly legacyApiUrl = API_BASE_URL;
|
||||
|
||||
// Public method to get the access token (used by AuthContext)
|
||||
async getToken(): Promise<string | null> {
|
||||
try {
|
||||
@ -1535,6 +1539,16 @@ class ApiService {
|
||||
return this.DEMO_DEPLOYMENT_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Legacy API credentials for device operations
|
||||
* Uses the same credentials as getLegacyWebViewCredentials but returns only what's needed
|
||||
*/
|
||||
async getLegacyCredentials(): Promise<{ userName: string; token: string } | null> {
|
||||
const creds = await this.getLegacyWebViewCredentials();
|
||||
if (!creds) return null;
|
||||
return { userName: creds.userName, token: creds.token };
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// WP SENSORS / DEVICES MANAGEMENT
|
||||
// ============================================================================
|
||||
@ -1545,8 +1559,17 @@ class ApiService {
|
||||
*/
|
||||
async getDevicesForBeneficiary(beneficiaryId: string) {
|
||||
try {
|
||||
// Get auth token for WellNuo API
|
||||
const token = await this.getToken();
|
||||
if (!token) return { ok: false, error: 'Not authenticated' };
|
||||
|
||||
// Get beneficiary's deployment_id from PostgreSQL
|
||||
const response = await fetch(`${this.baseUrl}/me/beneficiaries/${beneficiaryId}`);
|
||||
const response = await fetch(`${this.baseUrl}/me/beneficiaries/${beneficiaryId}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to get beneficiary');
|
||||
|
||||
const beneficiary = await response.json();
|
||||
@ -1679,8 +1702,17 @@ class ApiService {
|
||||
password: string
|
||||
) {
|
||||
try {
|
||||
// Get auth token for WellNuo API
|
||||
const token = await this.getToken();
|
||||
if (!token) throw new Error('Not authenticated');
|
||||
|
||||
// Get beneficiary details
|
||||
const response = await fetch(`${this.baseUrl}/me/beneficiaries/${beneficiaryId}`);
|
||||
const response = await fetch(`${this.baseUrl}/me/beneficiaries/${beneficiaryId}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to get beneficiary');
|
||||
|
||||
const beneficiary = await response.json();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user