Skip to content

Commit 39f21b3

Browse files
authored
Merge pull request #10 from PocketBaseExtended/feat/refactor-2026
Feat/refactor 2026
2 parents c3e1fff + aeeee33 commit 39f21b3

13 files changed

Lines changed: 1424 additions & 658 deletions

.gitignore

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
.vscode
21
test.ino
32
pocketbasearduino.ino
4-
pb_data
5-
pb_migrations
3+
pb_data/
4+
pb_migrations/
65
pocketbase.exe
7-
CHANGELOG.md
8-
LICENSE.md
9-
pocketbase_0.20.5_windows_amd64.zip
6+
pocketbase_*.zip
7+
build/
8+
*.o
9+
*.elf
10+
*.bin
11+
*.hex
12+
.DS_Store
13+
Thumbs.db
14+
15+
# Editor / tool config
16+
.vscode
17+
.claude

PocketbaseAuth.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)