diff --git a/README.md b/README.md index 9292c4b..3a9ad0a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dummy.json b/dummy.json index 6c93205..bb3ab4e 100644 --- a/dummy.json +++ b/dummy.json @@ -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 + } } -} +] \ No newline at end of file diff --git a/source/libpb.d b/source/libpb.d index 6bc31a1..5b27e62 100644 --- a/source/libpb.d +++ b/source/libpb.d @@ -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 RecordType */ - 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); +}