getAuthenticationToken static method
Future<MaybeError<String> >
getAuthenticationToken({
- required String username,
- required String password,
- bool setCookie = false,
- UriProductHelper uriHelper = uriHelperFoodProd,
Authentication: provide username/password and get a bearer token in return.
username
: Open Food Facts user_id (not email)
password
: user password (clear text, but HTTPS encrypted)
A token is returned. If the set_cookie parameter is set to 1, the token is also set as a cookie named "session" in the response.
To authenticate, you can either:
- use the Authorization header with the Bearer scheme, e.g.: "Authorization: bearer token"
- use the session cookie, e.g.: "Cookie: session=token"
Implementation
static Future<MaybeError<String>> getAuthenticationToken({
required final String username,
required final String password,
final bool setCookie = false,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
final Uri uri = uriHelper.getUri(
path: '/api/v1/auth${setCookie ? '?set_cookie=1' : ''}',
forcedHost: _getHost(uriHelper),
);
final Response response = await post(
uri,
body: <String, String>{
'username': username,
'password': password,
},
);
if (response.statusCode == 200) {
try {
final dynamic decodedResponse = HttpHelper().jsonDecodeUtf8(response);
return MaybeError<String>.value(decodedResponse['access_token']);
} catch (e) {
//
}
}
return MaybeError<String>.responseError(response);
}