|
| 1 | +// PBAuth.cpp |
| 2 | +// Authentication, token management, and health check. |
| 3 | + |
| 4 | +#include "PocketbaseExtended.h" |
| 5 | + |
| 6 | +// --------------------------------------------------------------------------- |
| 7 | +// Token management |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | + |
| 10 | +void PocketbaseExtended::setAuthToken(const String& token) { _authToken = token; } |
| 11 | +String PocketbaseExtended::getAuthToken() const { return _authToken; } |
| 12 | +void PocketbaseExtended::clearAuthToken() { _authToken = ""; } |
| 13 | + |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | +// Auth endpoints |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | + |
| 18 | +PBResponse PocketbaseExtended::authWithPassword(const char* identity, const char* password) { |
| 19 | + if (_collection.length() == 0) { |
| 20 | + PBResponse r; r.ok = false; r.statusCode = 0; |
| 21 | + r.error = "No collection set. Call collection() first."; |
| 22 | + return r; |
| 23 | + } |
| 24 | + |
| 25 | + String url = _baseUrl + "collections/" + _collection + "/auth-with-password"; |
| 26 | + String body = "{\"identity\":\""; |
| 27 | + body += identity; |
| 28 | + body += "\",\"password\":\""; |
| 29 | + body += password; |
| 30 | + body += "\"}"; |
| 31 | + |
| 32 | + PBResponse resp = _request("POST", url, body); |
| 33 | + |
| 34 | + // Auto-extract token from response body (no ArduinoJson dependency) |
| 35 | + if (resp.ok) { |
| 36 | + int idx = resp.body.indexOf("\"token\":\""); |
| 37 | + if (idx != -1) { |
| 38 | + idx += 9; // skip past `"token":"` |
| 39 | + int end = resp.body.indexOf("\"", idx); |
| 40 | + if (end != -1) { |
| 41 | + _authToken = resp.body.substring(idx, end); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return resp; |
| 47 | +} |
| 48 | + |
| 49 | +PBResponse PocketbaseExtended::authRefresh() { |
| 50 | + if (_collection.length() == 0) { |
| 51 | + PBResponse r; r.ok = false; r.statusCode = 0; |
| 52 | + r.error = "No collection set. Call collection() first."; |
| 53 | + return r; |
| 54 | + } |
| 55 | + if (_authToken.length() == 0) { |
| 56 | + PBResponse r; r.ok = false; r.statusCode = 0; |
| 57 | + r.error = "No token to refresh. Call authWithPassword() first."; |
| 58 | + return r; |
| 59 | + } |
| 60 | + |
| 61 | + String url = _baseUrl + "collections/" + _collection + "/auth-refresh"; |
| 62 | + PBResponse resp = _request("POST", url); |
| 63 | + |
| 64 | + // Auto-update stored token on success |
| 65 | + if (resp.ok) { |
| 66 | + int idx = resp.body.indexOf("\"token\":\""); |
| 67 | + if (idx != -1) { |
| 68 | + idx += 9; |
| 69 | + int end = resp.body.indexOf("\"", idx); |
| 70 | + if (end != -1) { |
| 71 | + _authToken = resp.body.substring(idx, end); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return resp; |
| 77 | +} |
| 78 | + |
| 79 | +// --------------------------------------------------------------------------- |
| 80 | +// Health check |
| 81 | +// --------------------------------------------------------------------------- |
| 82 | + |
| 83 | +PBResponse PocketbaseExtended::checkHealth() { |
| 84 | + String url = _baseUrl + "health"; |
| 85 | + return _request("GET", url); |
| 86 | +} |
0 commit comments