Skip to content

Commit cfe1b99

Browse files
author
Freek van de Ven
committed
fix: add apiPrefix to rest_user_repository to allow for anything in the path before /auth/token and the other endpoints
1 parent 71fc338 commit cfe1b99

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

packages/rest_user_repository/lib/rest_user_repository.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ class RestUserRepository implements UserRepositoryInterface {
2727
/// Creates an instance of the REST user repository.
2828
///
2929
/// Requires the [baseUrl] for the API endpoints.
30-
RestUserRepository({required String baseUrl}) {
30+
RestUserRepository({
31+
required String baseUrl,
32+
this.apiPrefix = "",
33+
}) {
3134
_authService = _TokenAuthService();
3235
_apiService = HttpApiService(
3336
baseUrl: Uri.parse(baseUrl),
@@ -42,6 +45,9 @@ class RestUserRepository implements UserRepositoryInterface {
4245
late final HttpApiService _apiService;
4346
late final _TokenAuthService _authService;
4447

48+
/// The prefix for the API endpoints.
49+
final String apiPrefix;
50+
4551
/// Logs in a user with the provided [email] and [password].
4652
///
4753
/// On a successful login, it returns an [AuthResponse] and stores the
@@ -57,8 +63,9 @@ class RestUserRepository implements UserRepositoryInterface {
5763
deserialize: (json) => AuthResponse(userObject: json["user"]),
5864
serialize: (body) => body,
5965
);
60-
var endpoint =
61-
_apiService.endpoint("/auth/login").withConverter(converter);
66+
var endpoint = _apiService
67+
.endpoint(_prefixedPath("/auth/token"))
68+
.withConverter(converter);
6269

6370
var response = await endpoint.post(
6471
requestModel: {"email": email, "password": password},
@@ -88,7 +95,7 @@ class RestUserRepository implements UserRepositoryInterface {
8895
serialize: (body) => body,
8996
);
9097
var endpoint =
91-
_apiService.endpoint("/auth/register").withConverter(converter);
98+
_apiService.endpoint(_prefixedPath("/user")).withConverter(converter);
9299

93100
var response = await endpoint.post(requestModel: values);
94101

@@ -118,7 +125,7 @@ class RestUserRepository implements UserRepositoryInterface {
118125
serialize: (body) => body,
119126
);
120127
var endpoint = _apiService
121-
.endpoint("/auth/request-password-change")
128+
.endpoint(_prefixedPath("/user/password-reset"))
122129
.withConverter(converter);
123130

124131
var response = await endpoint.post(requestModel: {"email": email});
@@ -135,7 +142,8 @@ class RestUserRepository implements UserRepositoryInterface {
135142
@override
136143
Future getLoggedInUser() async {
137144
try {
138-
var endpoint = _apiService.endpoint("/users/me").authenticate();
145+
var endpoint =
146+
_apiService.endpoint(_prefixedPath("/users/me")).authenticate();
139147
var response = await endpoint.get();
140148
return jsonDecode(response.inner.body);
141149
} on ApiException catch (e) {
@@ -175,4 +183,11 @@ class RestUserRepository implements UserRepositoryInterface {
175183
return null;
176184
}
177185
}
186+
187+
/// Helper method to prepend the configured API prefix to a path.
188+
/// Ensures the resulting path has a single leading slash.
189+
String _prefixedPath(String path) {
190+
var fullPath = "$apiPrefix/$path";
191+
return fullPath.replaceAll(RegExp("/+"), "/");
192+
}
178193
}

0 commit comments

Comments
 (0)