Skip to content

Commit 924613b

Browse files
authored
Merge pull request #708 from eyJhb/api-token
Adds ability to use API Key to "sign in"
2 parents a194145 + 9a05c6a commit 924613b

File tree

11 files changed

+495
-211
lines changed

11 files changed

+495
-211
lines changed

lib/l10n/app_en.arb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@
5353
"@invalidUsername": {
5454
"description": "Error message when the user enters an invalid username"
5555
},
56+
"useApiToken": "Use API Token",
57+
"useUsernameAndPassword": "Use username and password",
58+
"apiToken": "API Token",
59+
"@apiToken": {},
60+
"invalidApiToken": "Please enter a valid API key",
61+
"@invalidApiToken": {
62+
"description": "Error message when the user enters an invalid API key"
63+
},
64+
"apiTokenValidChars": "An API key may only contain the letters a-f, numbers 0-9 and be exactly 40 characters long",
65+
"@apiTokenValidChars": {
66+
"description": "Error message when the user tries to input a API key with forbidden characters"
67+
},
5668
"customServerUrl": "URL of the wger instance",
5769
"@customServerUrl": {
5870
"description": "Label in the form where the users can enter their own wger instance"

lib/providers/auth.dart

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class AuthProvider with ChangeNotifier {
5858
static const SERVER_VERSION_URL = 'version';
5959
static const REGISTRATION_URL = 'register';
6060
static const LOGIN_URL = 'login';
61+
static const TEST_URL = 'userprofile';
6162

6263
late http.Client client;
6364

@@ -134,29 +135,52 @@ class AuthProvider with ChangeNotifier {
134135
throw WgerHttpException(response.body);
135136
}
136137

137-
return login(username, password, serverUrl);
138+
return login(username, password, serverUrl, null);
138139
}
139140

140141
/// Authenticates a user
141142
Future<LoginActions> login(
142143
String username,
143144
String password,
144145
String serverUrl,
146+
String? apiToken,
145147
) async {
146148
await logout(shouldNotify: false);
147149

148-
final response = await client.post(
149-
makeUri(serverUrl, LOGIN_URL),
150-
headers: {
151-
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
152-
HttpHeaders.userAgentHeader: getAppNameHeader(),
153-
},
154-
body: json.encode({'username': username, 'password': password}),
155-
);
156-
final responseData = json.decode(response.body);
150+
// Login using the API token
151+
if (apiToken != null && apiToken.isNotEmpty) {
152+
final response = await client.get(
153+
makeUri(serverUrl, TEST_URL),
154+
headers: {
155+
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
156+
HttpHeaders.userAgentHeader: getAppNameHeader(),
157+
HttpHeaders.authorizationHeader: 'Token $apiToken',
158+
},
159+
);
157160

158-
if (response.statusCode >= 400) {
159-
throw WgerHttpException(response.body);
161+
if (response.statusCode != 200) {
162+
throw WgerHttpException(response.body);
163+
}
164+
165+
token = apiToken;
166+
167+
// Login using password
168+
} else {
169+
final response = await client.post(
170+
makeUri(serverUrl, LOGIN_URL),
171+
headers: {
172+
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
173+
HttpHeaders.userAgentHeader: getAppNameHeader(),
174+
},
175+
body: json.encode({'username': username, 'password': password}),
176+
);
177+
final responseData = json.decode(response.body);
178+
179+
if (response.statusCode >= 400) {
180+
throw WgerHttpException(response.body);
181+
}
182+
183+
token = responseData['token'];
160184
}
161185

162186
await initVersions(serverUrl);
@@ -168,7 +192,6 @@ class AuthProvider with ChangeNotifier {
168192
}
169193

170194
// Log user in
171-
token = responseData['token'];
172195
state = AuthState.loggedIn;
173196
notifyListeners();
174197

0 commit comments

Comments
 (0)