mirror of https://github.com/Hax-io/libpb
Fixed creation of auth-type records in `createRecord()`
This commit is contained in:
parent
e44fbf3189
commit
4db1d40759
31
README.md
31
README.md
|
@ -97,6 +97,8 @@ assert(person.obj["bruh"].integer() == 1);
|
||||||
|
|
||||||
### Record management
|
### Record management
|
||||||
|
|
||||||
|
#### Normal collections
|
||||||
|
|
||||||
Below we have a few calls like create and delete:
|
Below we have a few calls like create and delete:
|
||||||
|
|
||||||
```d
|
```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
|
## Development
|
||||||
|
|
||||||
### Unit tests
|
### Unit tests
|
||||||
|
|
41
dummy.json
41
dummy.json
|
@ -1,4 +1,5 @@
|
||||||
{
|
[
|
||||||
|
{
|
||||||
"id": "iir9gleipa4n6lf",
|
"id": "iir9gleipa4n6lf",
|
||||||
"name": "dummy",
|
"name": "dummy",
|
||||||
"type": "base",
|
"type": "base",
|
||||||
|
@ -36,5 +37,41 @@
|
||||||
"updateRule": "",
|
"updateRule": "",
|
||||||
"deleteRule": "",
|
"deleteRule": "",
|
||||||
"options": {}
|
"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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
]
|
|
@ -4,6 +4,7 @@ import std.json;
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import std.net.curl;
|
import std.net.curl;
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
|
import std.string : cmp;
|
||||||
|
|
||||||
public class PBException : Exception
|
public class PBException : Exception
|
||||||
{
|
{
|
||||||
|
@ -122,7 +123,7 @@ public class PocketBase
|
||||||
*
|
*
|
||||||
* Returns: An instance of the created <code>RecordType</code>
|
* 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);
|
idAbleCheck(item);
|
||||||
|
|
||||||
|
@ -138,7 +139,23 @@ public class PocketBase
|
||||||
{
|
{
|
||||||
string responseData = cast(string)post(pocketBaseURL~"collections/"~table~"/records", serialized.toString(), httpSettings);
|
string responseData = cast(string)post(pocketBaseURL~"collections/"~table~"/records", serialized.toString(), httpSettings);
|
||||||
JSONValue responseJSON = parseJSON(responseData);
|
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);
|
recordOut = fromJSON!(RecordType)(responseJSON);
|
||||||
|
|
||||||
return recordOut;
|
return recordOut;
|
||||||
|
@ -165,6 +182,7 @@ public class PocketBase
|
||||||
}
|
}
|
||||||
catch(JSONException e)
|
catch(JSONException e)
|
||||||
{
|
{
|
||||||
|
writeln(e);
|
||||||
throw new PocketBaseParsingException();
|
throw new PocketBaseParsingException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -682,3 +700,29 @@ unittest
|
||||||
assert(false);
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue