Skip to content

Commit

Permalink
Added Restrr#createUser
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlessenich committed May 3, 2024
1 parent cbbca0e commit ef6be98
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.10
- Implemented User registration (RestrrBuilder#register)
- Added Restrr#createUser

## 0.9.1
- Fixed use of `EntityId<E>`
Expand Down
3 changes: 3 additions & 0 deletions lib/src/api/restrr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ abstract class Restrr {
/// Retrieves the currently authenticated user.
Future<User> retrieveSelf({bool forceRetrieve = false});

/// Creates a new user.
Future<User> createUser({required String username, required String password, String? displayName, String? email});

/* Sessions */

Future<PartialSession> retrieveCurrentSession({bool forceRetrieve = false});
Expand Down
17 changes: 2 additions & 15 deletions lib/src/api/restrr_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,11 @@ class RestrrBuilder {
Future<Restrr> register({required String username, required String password, String? displayName, String? email, String? sessionName}) async {
return _handleAuthProcess(authFunction: (apiImpl) async {
// register user first
final RestResponse<User> userResponse = await apiImpl.requestHandler.apiRequest(
route: UserRoutes.create.compile(),
body: {
'username': username,
'password': password,
if (displayName != null) 'display_name': displayName,
if (email != null) 'email': email,
},
noAuth: true,
mapper: (json) => apiImpl.entityBuilder.buildUser(json)
);
if (userResponse.hasError) {
throw userResponse.error!;
}
final User user = await apiImpl.createUser(username: username, password: password, email: email);
return apiImpl.requestHandler.apiRequest(
route: SessionRoutes.create.compile(),
body: {
'username': username,
'username': user.username,
'password': password,
if (sessionName != null) 'session_name': sessionName,
},
Expand Down
15 changes: 15 additions & 0 deletions lib/src/internal/restrr_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ class RestrrImpl implements Restrr {
return UserIdImpl(api: this, value: session.user.id.value).retrieve(forceRetrieve: forceRetrieve);
}

@override
Future<User> createUser({required String username, required String password, String? displayName, String? email}) async {
final RestResponse<User> response = await requestHandler
.apiRequest(route: UserRoutes.create.compile(), mapper: (json) => entityBuilder.buildUser(json), noAuth: true, body: {
'username': username,
'password': password,
if (displayName != null) 'display_name': displayName,
if (email != null) 'email': email,
});
if (response.hasError) {
throw response.error!;
}
return response.data!;
}

/* Sessions */

@override
Expand Down
2 changes: 2 additions & 0 deletions test/restrr_entity_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const String transactionJson = '''
void main() {
late RestrrImpl api;

// TODO: mock the api instance instead of actually logging in

setUp(() async {
// log in, get api instance
api = (await RestrrBuilder(uri: _validUri).login(username: 'admin', password: 'Financrr123')) as RestrrImpl;
Expand Down

0 comments on commit ef6be98

Please sign in to comment.