Fixed creation of auth-type records in `createRecord()`

This commit is contained in:
Tristan B. Velloza Kildaire 2022-12-30 16:09:44 +02:00
parent e44fbf3189
commit 4db1d40759
3 changed files with 116 additions and 4 deletions

View File

@ -97,6 +97,8 @@ assert(person.obj["bruh"].integer() == 1);
### Record management
#### Normal collections
Below we have a few calls like create and delete:
```d
@ -140,6 +142,35 @@ foreach(Person returnedPerson; returnedPeople)
}
```
#### `auth` collections
Auth collections require that certain calls, such as `createRecord(table, record, isAuthCollection)` have the last argument se to `true`.
```d
import core.thread : Thread, dur;
import std.string : cmp;
PocketBase pb = new PocketBase();
struct Person
{
string id;
string email;
string username;
string password;
string passwordConfirm;
}
Person p1;
p1.email = "deavmi@redxen.eu";
p1.username = "deavmi";
p1.password = "bigbruh1111";
p1.passwordConfirm = "bigbruh1111";
p1 = pb.createRecord("dummy_auth", p1, true);
pb.deleteRecord("dummy_auth", p1);
```
## Development
### Unit tests

View File

@ -1,4 +1,5 @@
{
[
{
"id": "iir9gleipa4n6lf",
"name": "dummy",
"type": "base",
@ -36,5 +37,41 @@
"updateRule": "",
"deleteRule": "",
"options": {}
},
{
"id": "fw99z50dwcfjwn7",
"name": "dummy_auth",
"type": "auth",
"system": false,
"schema": [
{
"id": "psy7unkl",
"name": "bruh",
"type": "text",
"system": false,
"required": false,
"unique": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
}
],
"listRule": "",
"viewRule": "",
"createRule": "",
"updateRule": "",
"deleteRule": "",
"options": {
"allowEmailAuth": true,
"allowOAuth2Auth": false,
"allowUsernameAuth": true,
"exceptEmailDomains": null,
"manageRule": null,
"minPasswordLength": 8,
"onlyEmailDomains": null,
"requireEmail": false
}
}
}
]

View File

@ -4,6 +4,7 @@ import std.json;
import std.stdio;
import std.net.curl;
import std.conv : to;
import std.string : cmp;
public class PBException : Exception
{
@ -122,7 +123,7 @@ public class PocketBase
*
* Returns: An instance of the created <code>RecordType</code>
*/
public RecordType createRecord(string, RecordType)(string table, RecordType item)
public RecordType createRecord(string, RecordType)(string table, RecordType item, bool isAuthCollection = false)
{
idAbleCheck(item);
@ -138,7 +139,23 @@ public class PocketBase
{
string responseData = cast(string)post(pocketBaseURL~"collections/"~table~"/records", serialized.toString(), httpSettings);
JSONValue responseJSON = parseJSON(responseData);
// On creation of a record in an "auth" collection the email visibility
// will initially be false, therefore fill in a blank for it temporarily
// now as to not make `fromJSON` crash when it sees an email field in
// a struct and tries to look the the JSON key "email" when it isn't present
//
// A password is never returned (so `password` and `passwordConfirm` will be left out)
//
// The above are all assumed to be strings, if not then a runtime error will occur
// See (issue #3)
if(isAuthCollection)
{
responseJSON["email"] = "";
responseJSON["password"] = "";
responseJSON["passwordConfirm"] = "";
}
recordOut = fromJSON!(RecordType)(responseJSON);
return recordOut;
@ -165,6 +182,7 @@ public class PocketBase
}
catch(JSONException e)
{
writeln(e);
throw new PocketBaseParsingException();
}
}
@ -682,3 +700,29 @@ unittest
assert(false);
}
}
unittest
{
import core.thread : Thread, dur;
import std.string : cmp;
PocketBase pb = new PocketBase();
struct Person
{
string id;
string email;
string username;
string password;
string passwordConfirm;
}
Person p1;
p1.email = "deavmi@redxen.eu";
p1.username = "deavmi";
p1.password = "bigbruh1111";
p1.passwordConfirm = "bigbruh1111";
p1 = pb.createRecord("dummy_auth", p1, true);
pb.deleteRecord("dummy_auth", p1);
}